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};
7 use syntax::{source_map::DUMMY_SP, symbol::Symbol};
9 use crate::interpret::{intern_const_alloc_recursive, ConstValue, InterpCx};
16 pub use eval_queries::*;
19 /// Extracts a field of a (variant of a) const.
20 // this function uses `unwrap` copiously, because an already validated constant must have valid
21 // fields and can thus never fail outside of compiler bugs
22 pub(crate) fn const_field<'tcx>(
24 param_env: ty::ParamEnv<'tcx>,
25 variant: Option<VariantIdx>,
27 value: &'tcx ty::Const<'tcx>,
28 ) -> &'tcx ty::Const<'tcx> {
29 trace!("const_field: {:?}, {:?}", field, value);
30 let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
31 // get the operand again
32 let op = ecx.eval_const_to_op(value, None).unwrap();
34 let down = match variant {
36 Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
39 let field = ecx.operand_field(down, field.index() as u64).unwrap();
40 // and finally move back to the const world, always normalizing because
41 // this is not called for statics.
42 op_to_const(&ecx, field)
45 pub(crate) fn const_caller_location<'tcx>(
47 (file, line, col): (Symbol, u32, u32),
48 ) -> &'tcx ty::Const<'tcx> {
49 trace!("const_caller_location: {}:{}:{}", file, line, col);
50 let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
52 let loc_ty = tcx.caller_location_ty();
53 let loc_place = ecx.alloc_caller_location(file, line, col);
54 intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap();
55 let loc_const = ty::Const {
57 val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
60 tcx.mk_const(loc_const)
63 // this function uses `unwrap` copiously, because an already validated constant must have valid
64 // fields and can thus never fail outside of compiler bugs
65 pub(crate) fn const_variant_index<'tcx>(
67 param_env: ty::ParamEnv<'tcx>,
68 val: &'tcx ty::Const<'tcx>,
70 trace!("const_variant_index: {:?}", val);
71 let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
72 let op = ecx.eval_const_to_op(val, None).unwrap();
73 ecx.read_discriminant(op).unwrap().1