]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval.rs
Auto merge of #67312 - cuviper:clone-box-slice, r=SimonSapin
[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 use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
7
8 use crate::interpret::{intern_const_alloc_recursive, ConstValue, InterpCx};
9
10 mod error;
11 mod eval_queries;
12 mod machine;
13
14 pub use error::*;
15 pub use eval_queries::*;
16 pub use machine::*;
17
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>(
22     tcx: TyCtxt<'tcx>,
23     param_env: ty::ParamEnv<'tcx>,
24     variant: Option<VariantIdx>,
25     field: mir::Field,
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();
32     // downcast
33     let down = match variant {
34         None => op,
35         Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
36     };
37     // then project
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)
42 }
43
44 pub(crate) fn const_caller_location<'tcx>(
45     tcx: TyCtxt<'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);
50
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 {
55         ty: loc_ty,
56         val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
57     };
58
59     tcx.mk_const(loc_const)
60 }
61
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>(
65     tcx: TyCtxt<'tcx>,
66     param_env: ty::ParamEnv<'tcx>,
67     val: &'tcx ty::Const<'tcx>,
68 ) -> VariantIdx {
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
73 }