]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval/mod.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / librustc_mir / const_eval / mod.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 ) -> ConstValue<'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(
47     tcx: TyCtxt<'tcx>,
48     (file, line, col): (Symbol, u32, u32),
49 ) -> ConstValue<'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_place = ecx.alloc_caller_location(file, line, col);
54     intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
55     ConstValue::Scalar(loc_place.ptr)
56 }
57
58 // this function uses `unwrap` copiously, because an already validated constant
59 // must have valid fields and can thus never fail outside of compiler bugs
60 pub(crate) fn destructure_const<'tcx>(
61     tcx: TyCtxt<'tcx>,
62     param_env: ty::ParamEnv<'tcx>,
63     val: &'tcx ty::Const<'tcx>,
64 ) -> mir::DestructuredConst<'tcx> {
65     trace!("destructure_const: {:?}", val);
66     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
67     let op = ecx.eval_const_to_op(val, None).unwrap();
68
69     let variant = ecx.read_discriminant(op).unwrap().1;
70
71     let field_count = match val.ty.kind {
72         ty::Array(_, len) => len.eval_usize(tcx, param_env),
73         ty::Adt(def, _) => def.variants[variant].fields.len() as u64,
74         ty::Tuple(substs) => substs.len() as u64,
75         _ => bug!("cannot destructure constant {:?}", val),
76     };
77
78     let down = ecx.operand_downcast(op, variant).unwrap();
79     let fields_iter = (0..field_count).map(|i| {
80         let field_op = ecx.operand_field(down, i).unwrap();
81         let val = op_to_const(&ecx, field_op);
82         ty::Const::from_value(tcx, val, field_op.layout.ty)
83     });
84     let fields = tcx.arena.alloc_from_iter(fields_iter);
85
86     mir::DestructuredConst { variant, fields }
87 }