]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval.rs
Rollup merge of #67531 - RalfJung:tame-promotion, r=nikomatsakis
[rust.git] / src / librustc_mir / const_eval.rs
1 // Not in interpret to make sure we do not use private implementation details
2
3 use rustc::mir;
4 use rustc::ty::layout::VariantIdx;
5 use rustc::ty::{self, TyCtxt};
6
7 use syntax::{source_map::DUMMY_SP, symbol::Symbol};
8
9 use crate::interpret::{intern_const_alloc_recursive, ConstValue, InterpCx};
10
11 mod error;
12 mod eval_queries;
13 mod machine;
14
15 pub use error::*;
16 pub use eval_queries::*;
17 pub use machine::*;
18
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>(
23     tcx: TyCtxt<'tcx>,
24     param_env: ty::ParamEnv<'tcx>,
25     variant: Option<VariantIdx>,
26     field: mir::Field,
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();
33     // downcast
34     let down = match variant {
35         None => op,
36         Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
37     };
38     // then project
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)
43 }
44
45 pub(crate) fn const_caller_location<'tcx>(
46     tcx: TyCtxt<'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);
51
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 {
56         ty: loc_ty,
57         val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
58     };
59
60     tcx.mk_const(loc_const)
61 }
62
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>(
66     tcx: TyCtxt<'tcx>,
67     param_env: ty::ParamEnv<'tcx>,
68     val: &'tcx ty::Const<'tcx>,
69 ) -> VariantIdx {
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
74 }