• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Not in interpret to make sure we do not use private implementation details
2 
3 use crate::errors::MaxNumNodesInConstErr;
4 use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, Scalar};
5 use rustc_middle::mir;
6 use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
7 use rustc_middle::ty::{self, Ty, TyCtxt};
8 use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
9 
10 mod error;
11 mod eval_queries;
12 mod fn_queries;
13 mod machine;
14 mod valtrees;
15 
16 pub use error::*;
17 pub use eval_queries::*;
18 pub use fn_queries::*;
19 pub use machine::*;
20 pub(crate) use valtrees::{const_to_valtree_inner, valtree_to_const_value};
21 
const_caller_location( tcx: TyCtxt<'_>, (file, line, col): (Symbol, u32, u32), ) -> ConstValue<'_>22 pub(crate) fn const_caller_location(
23     tcx: TyCtxt<'_>,
24     (file, line, col): (Symbol, u32, u32),
25 ) -> ConstValue<'_> {
26     trace!("const_caller_location: {}:{}:{}", file, line, col);
27     let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), CanAccessStatics::No);
28 
29     let loc_place = ecx.alloc_caller_location(file, line, col);
30     if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
31         bug!("intern_const_alloc_recursive should not error in this case")
32     }
33     ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr, &tcx))
34 }
35 
36 // We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
37 const VALTREE_MAX_NODES: usize = 100000;
38 
39 pub(crate) enum ValTreeCreationError {
40     NodesOverflow,
41     NonSupportedType,
42     Other,
43 }
44 pub(crate) type ValTreeCreationResult<'tcx> = Result<ty::ValTree<'tcx>, ValTreeCreationError>;
45 
46 /// Evaluates a constant and turns it into a type-level constant value.
eval_to_valtree<'tcx>( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, cid: GlobalId<'tcx>, ) -> EvalToValTreeResult<'tcx>47 pub(crate) fn eval_to_valtree<'tcx>(
48     tcx: TyCtxt<'tcx>,
49     param_env: ty::ParamEnv<'tcx>,
50     cid: GlobalId<'tcx>,
51 ) -> EvalToValTreeResult<'tcx> {
52     let const_alloc = tcx.eval_to_allocation_raw(param_env.and(cid))?;
53 
54     // FIXME Need to provide a span to `eval_to_valtree`
55     let ecx = mk_eval_cx(
56         tcx,
57         DUMMY_SP,
58         param_env,
59         // It is absolutely crucial for soundness that
60         // we do not read from static items or other mutable memory.
61         CanAccessStatics::No,
62     );
63     let place = ecx.raw_const_to_mplace(const_alloc).unwrap();
64     debug!(?place);
65 
66     let mut num_nodes = 0;
67     let valtree_result = const_to_valtree_inner(&ecx, &place, &mut num_nodes);
68 
69     match valtree_result {
70         Ok(valtree) => Ok(Some(valtree)),
71         Err(err) => {
72             let did = cid.instance.def_id();
73             let global_const_id = cid.display(tcx);
74             match err {
75                 ValTreeCreationError::NodesOverflow => {
76                     let span = tcx.hir().span_if_local(did);
77                     tcx.sess.emit_err(MaxNumNodesInConstErr { span, global_const_id });
78 
79                     Ok(None)
80                 }
81                 ValTreeCreationError::NonSupportedType | ValTreeCreationError::Other => Ok(None),
82             }
83         }
84     }
85 }
86 
87 #[instrument(skip(tcx), level = "debug")]
try_destructure_mir_constant_for_diagnostics<'tcx>( tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>, ) -> Option<mir::DestructuredConstant<'tcx>>88 pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
89     tcx: TyCtxt<'tcx>,
90     val: ConstValue<'tcx>,
91     ty: Ty<'tcx>,
92 ) -> Option<mir::DestructuredConstant<'tcx>> {
93     let param_env = ty::ParamEnv::reveal_all();
94     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
95     let op = ecx.const_val_to_op(val, ty, None).ok()?;
96 
97     // We go to `usize` as we cannot allocate anything bigger anyway.
98     let (field_count, variant, down) = match ty.kind() {
99         ty::Array(_, len) => (len.eval_target_usize(tcx, param_env) as usize, None, op),
100         ty::Adt(def, _) if def.variants().is_empty() => {
101             return None;
102         }
103         ty::Adt(def, _) => {
104             let variant = ecx.read_discriminant(&op).ok()?.1;
105             let down = ecx.operand_downcast(&op, variant).ok()?;
106             (def.variants()[variant].fields.len(), Some(variant), down)
107         }
108         ty::Tuple(substs) => (substs.len(), None, op),
109         _ => bug!("cannot destructure mir constant {:?}", val),
110     };
111 
112     let fields_iter = (0..field_count)
113         .map(|i| {
114             let field_op = ecx.operand_field(&down, i).ok()?;
115             let val = op_to_const(&ecx, &field_op);
116             Some((val, field_op.layout.ty))
117         })
118         .collect::<Option<Vec<_>>>()?;
119     let fields = tcx.arena.alloc_from_iter(fields_iter);
120 
121     Some(mir::DestructuredConstant { variant, fields })
122 }
123