1 // Not in interpret to make sure we do not use private implementation details
4 use rustc::ty::layout::VariantIdx;
5 use rustc::ty::{self, TyCtxt};
6 use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
8 use crate::interpret::{intern_const_alloc_recursive, ConstValue, InterpCx};
15 pub use eval_queries::*;
18 /// Extracts a field of a (variant of a) const.
19 // this function uses `unwrap` copiously, because an already validated constant must have valid
20 // fields and can thus never fail outside of compiler bugs
21 pub(crate) fn const_field<'tcx>(
23 param_env: ty::ParamEnv<'tcx>,
24 variant: Option<VariantIdx>,
26 value: &'tcx ty::Const<'tcx>,
27 ) -> &'tcx ty::Const<'tcx> {
28 trace!("const_field: {:?}, {:?}", field, value);
29 let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
30 // get the operand again
31 let op = ecx.eval_const_to_op(value, None).unwrap();
33 let down = match variant {
35 Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
38 let field = ecx.operand_field(down, field.index() as u64).unwrap();
39 // and finally move back to the const world, always normalizing because
40 // this is not called for statics.
41 op_to_const(&ecx, field)
44 pub(crate) fn const_caller_location<'tcx>(
46 (file, line, col): (Symbol, u32, u32),
47 ) -> &'tcx ty::Const<'tcx> {
48 trace!("const_caller_location: {}:{}:{}", file, line, col);
49 let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
51 let loc_ty = tcx.caller_location_ty();
52 let loc_place = ecx.alloc_caller_location(file, line, col);
53 intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap();
54 let loc_const = ty::Const {
56 val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
59 tcx.mk_const(loc_const)
62 // this function uses `unwrap` copiously, because an already validated constant must have valid
63 // fields and can thus never fail outside of compiler bugs
64 pub(crate) fn const_variant_index<'tcx>(
66 param_env: ty::ParamEnv<'tcx>,
67 val: &'tcx ty::Const<'tcx>,
69 trace!("const_variant_index: {:?}", val);
70 let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
71 let op = ecx.eval_const_to_op(val, None).unwrap();
72 ecx.read_discriminant(op).unwrap().1