2018-08-24 14:39:25 +00:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
2022-08-20 19:27:41 +00:00
|
|
|
use crate::errors::MaxNumNodesInConstErr;
|
2023-06-28 13:22:02 +00:00
|
|
|
use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, Scalar};
|
2022-04-05 14:33:42 +00:00
|
|
|
use rustc_middle::mir;
|
2022-04-12 16:14:28 +00:00
|
|
|
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
|
2023-06-28 09:23:34 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
mod error;
|
2019-12-23 11:55:16 +00:00
|
|
|
mod eval_queries;
|
2020-01-01 17:06:00 +00:00
|
|
|
mod fn_queries;
|
2019-12-25 00:28:30 +00:00
|
|
|
mod machine;
|
2022-04-05 14:33:42 +00:00
|
|
|
mod valtrees;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
|
|
|
pub use error::*;
|
2019-12-23 11:55:16 +00:00
|
|
|
pub use eval_queries::*;
|
2020-01-01 17:06:00 +00:00
|
|
|
pub use fn_queries::*;
|
2019-12-25 00:28:30 +00:00
|
|
|
pub use machine::*;
|
2022-04-12 16:14:28 +00:00
|
|
|
pub(crate) use valtrees::{const_to_valtree_inner, valtree_to_const_value};
|
2018-09-20 09:57:45 +00:00
|
|
|
|
2020-03-06 23:56:32 +00:00
|
|
|
pub(crate) fn const_caller_location(
|
2021-12-14 03:34:51 +00:00
|
|
|
tcx: TyCtxt<'_>,
|
2019-10-09 15:25:41 +00:00
|
|
|
(file, line, col): (Symbol, u32, u32),
|
2021-12-14 03:34:51 +00:00
|
|
|
) -> ConstValue<'_> {
|
2019-10-09 15:25:41 +00:00
|
|
|
trace!("const_caller_location: {}:{}:{}", file, line, col);
|
2023-06-24 20:40:40 +00:00
|
|
|
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), CanAccessStatics::No);
|
2019-10-09 15:25:41 +00:00
|
|
|
|
2019-11-29 10:29:30 +00:00
|
|
|
let loc_place = ecx.alloc_caller_location(file, line, col);
|
2021-02-15 00:00:00 +00:00
|
|
|
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
|
2020-11-04 16:53:43 +00:00
|
|
|
bug!("intern_const_alloc_recursive should not error in this case")
|
|
|
|
}
|
2022-04-18 16:47:38 +00:00
|
|
|
ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr, &tcx))
|
2019-10-09 15:25:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 17:42:29 +00:00
|
|
|
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
|
|
|
|
const VALTREE_MAX_NODES: usize = 100000;
|
|
|
|
|
|
|
|
pub(crate) enum ValTreeCreationError {
|
|
|
|
NodesOverflow,
|
|
|
|
NonSupportedType,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
pub(crate) type ValTreeCreationResult<'tcx> = Result<ty::ValTree<'tcx>, ValTreeCreationError>;
|
|
|
|
|
2022-04-12 16:14:28 +00:00
|
|
|
/// Evaluates a constant and turns it into a type-level constant value.
|
|
|
|
pub(crate) fn eval_to_valtree<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
cid: GlobalId<'tcx>,
|
|
|
|
) -> EvalToValTreeResult<'tcx> {
|
|
|
|
let const_alloc = tcx.eval_to_allocation_raw(param_env.and(cid))?;
|
2022-02-16 09:56:01 +00:00
|
|
|
|
|
|
|
// FIXME Need to provide a span to `eval_to_valtree`
|
2022-04-12 16:14:28 +00:00
|
|
|
let ecx = mk_eval_cx(
|
2023-06-24 20:40:40 +00:00
|
|
|
tcx,
|
|
|
|
DUMMY_SP,
|
|
|
|
param_env,
|
2022-04-12 16:14:28 +00:00
|
|
|
// It is absolutely crucial for soundness that
|
|
|
|
// we do not read from static items or other mutable memory.
|
2023-06-24 20:40:40 +00:00
|
|
|
CanAccessStatics::No,
|
2022-04-12 16:14:28 +00:00
|
|
|
);
|
|
|
|
let place = ecx.raw_const_to_mplace(const_alloc).unwrap();
|
|
|
|
debug!(?place);
|
|
|
|
|
2022-06-02 17:42:29 +00:00
|
|
|
let mut num_nodes = 0;
|
|
|
|
let valtree_result = const_to_valtree_inner(&ecx, &place, &mut num_nodes);
|
2022-04-12 16:14:28 +00:00
|
|
|
|
|
|
|
match valtree_result {
|
|
|
|
Ok(valtree) => Ok(Some(valtree)),
|
2022-06-02 17:42:29 +00:00
|
|
|
Err(err) => {
|
|
|
|
let did = cid.instance.def_id();
|
2022-08-22 15:14:49 +00:00
|
|
|
let global_const_id = cid.display(tcx);
|
2022-06-02 17:42:29 +00:00
|
|
|
match err {
|
|
|
|
ValTreeCreationError::NodesOverflow => {
|
2023-05-17 10:30:14 +00:00
|
|
|
let span = tcx.hir().span_if_local(did);
|
|
|
|
tcx.sess.emit_err(MaxNumNodesInConstErr { span, global_const_id });
|
2022-06-02 17:42:29 +00:00
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
ValTreeCreationError::NonSupportedType | ValTreeCreationError::Other => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 16:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
2023-07-05 15:51:52 +00:00
|
|
|
pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
|
2022-04-12 16:14:28 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-06-28 09:23:34 +00:00
|
|
|
val: ConstValue<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
2023-06-28 13:22:02 +00:00
|
|
|
) -> Option<mir::DestructuredConstant<'tcx>> {
|
2023-06-28 09:23:34 +00:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2023-06-24 20:40:40 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
|
2023-06-28 13:22:02 +00:00
|
|
|
let op = ecx.const_val_to_op(val, ty, None).ok()?;
|
2022-04-12 16:14:28 +00:00
|
|
|
|
|
|
|
// We go to `usize` as we cannot allocate anything bigger anyway.
|
2023-06-28 09:23:34 +00:00
|
|
|
let (field_count, variant, down) = match ty.kind() {
|
2023-02-14 08:51:19 +00:00
|
|
|
ty::Array(_, len) => (len.eval_target_usize(tcx, param_env) as usize, None, op),
|
2022-04-12 16:14:28 +00:00
|
|
|
ty::Adt(def, _) if def.variants().is_empty() => {
|
2023-06-28 13:22:02 +00:00
|
|
|
return None;
|
2022-04-12 16:14:28 +00:00
|
|
|
}
|
|
|
|
ty::Adt(def, _) => {
|
2023-06-28 13:22:02 +00:00
|
|
|
let variant = ecx.read_discriminant(&op).ok()?.1;
|
2023-07-24 09:44:58 +00:00
|
|
|
let down = ecx.project_downcast(&op, variant).ok()?;
|
2022-04-12 16:14:28 +00:00
|
|
|
(def.variants()[variant].fields.len(), Some(variant), down)
|
|
|
|
}
|
2023-07-11 21:35:29 +00:00
|
|
|
ty::Tuple(args) => (args.len(), None, op),
|
2022-04-12 16:14:28 +00:00
|
|
|
_ => bug!("cannot destructure mir constant {:?}", val),
|
|
|
|
};
|
|
|
|
|
|
|
|
let fields_iter = (0..field_count)
|
|
|
|
.map(|i| {
|
2023-07-24 09:44:58 +00:00
|
|
|
let field_op = ecx.project_field(&down, i).ok()?;
|
2022-04-12 16:14:28 +00:00
|
|
|
let val = op_to_const(&ecx, &field_op);
|
2023-06-28 13:22:02 +00:00
|
|
|
Some((val, field_op.layout.ty))
|
2022-04-12 16:14:28 +00:00
|
|
|
})
|
2023-06-28 13:22:02 +00:00
|
|
|
.collect::<Option<Vec<_>>>()?;
|
2022-04-12 16:14:28 +00:00
|
|
|
let fields = tcx.arena.alloc_from_iter(fields_iter);
|
|
|
|
|
2023-06-28 13:22:02 +00:00
|
|
|
Some(mir::DestructuredConstant { variant, fields })
|
2022-04-12 16:14:28 +00:00
|
|
|
}
|