]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval.rs
rustc_span: return an impl Iterator instead of a Vec from macro_backtrace.
[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, InternKind, InterpCx};
9
10 mod error;
11 mod eval_queries;
12 mod fn_queries;
13 mod machine;
14
15 pub use error::*;
16 pub use eval_queries::*;
17 pub use fn_queries::*;
18 pub use machine::*;
19
20 /// Extracts a field of a (variant of a) const.
21 // this function uses `unwrap` copiously, because an already validated constant must have valid
22 // fields and can thus never fail outside of compiler bugs
23 pub(crate) fn const_field<'tcx>(
24     tcx: TyCtxt<'tcx>,
25     param_env: ty::ParamEnv<'tcx>,
26     variant: Option<VariantIdx>,
27     field: mir::Field,
28     value: &'tcx ty::Const<'tcx>,
29 ) -> &'tcx ty::Const<'tcx> {
30     trace!("const_field: {:?}, {:?}", field, value);
31     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
32     // get the operand again
33     let op = ecx.eval_const_to_op(value, None).unwrap();
34     // downcast
35     let down = match variant {
36         None => op,
37         Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
38     };
39     // then project
40     let field = ecx.operand_field(down, field.index() as u64).unwrap();
41     // and finally move back to the const world, always normalizing because
42     // this is not called for statics.
43     op_to_const(&ecx, field)
44 }
45
46 pub(crate) fn const_caller_location<'tcx>(
47     tcx: TyCtxt<'tcx>,
48     (file, line, col): (Symbol, u32, u32),
49 ) -> &'tcx ty::Const<'tcx> {
50     trace!("const_caller_location: {}:{}:{}", file, line, col);
51     let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
52
53     let loc_ty = tcx.caller_location_ty();
54     let loc_place = ecx.alloc_caller_location(file, line, col);
55     intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
56     let loc_const = ty::Const {
57         ty: loc_ty,
58         val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
59     };
60
61     tcx.mk_const(loc_const)
62 }
63
64 // this function uses `unwrap` copiously, because an already validated constant
65 // must have valid fields and can thus never fail outside of compiler bugs
66 pub(crate) fn destructure_const<'tcx>(
67     tcx: TyCtxt<'tcx>,
68     param_env: ty::ParamEnv<'tcx>,
69     val: &'tcx ty::Const<'tcx>,
70 ) -> mir::DestructuredConst<'tcx> {
71     trace!("destructure_const: {:?}", val);
72     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
73     let op = ecx.eval_const_to_op(val, None).unwrap();
74
75     let variant = ecx.read_discriminant(op).unwrap().1;
76
77     let field_count = match val.ty.kind {
78         ty::Array(_, len) => len.eval_usize(tcx, param_env),
79         ty::Adt(def, _) => def.variants[variant].fields.len() as u64,
80         ty::Tuple(substs) => substs.len() as u64,
81         _ => bug!("cannot destructure constant {:?}", val),
82     };
83
84     let down = ecx.operand_downcast(op, variant).unwrap();
85     let fields_iter = (0..field_count).map(|i| {
86         let field_op = ecx.operand_field(down, i).unwrap();
87         op_to_const(&ecx, field_op)
88     });
89     let fields = tcx.arena.alloc_from_iter(fields_iter);
90
91     mir::DestructuredConst { variant, fields }
92 }