]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/const_eval/eval_queries.rs
Auto merge of #86827 - camsteffen:hash-lint-resolved, r=oli-obk
[rust.git] / compiler / rustc_mir / src / const_eval / eval_queries.rs
1 use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, MemoryExtra};
2 use crate::interpret::eval_nullary_intrinsic;
3 use crate::interpret::{
4     intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
5     Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
6     ScalarMaybeUninit, StackPopCleanup,
7 };
8 use crate::util::pretty::display_allocation;
9
10 use rustc_errors::ErrorReported;
11 use rustc_hir::def::DefKind;
12 use rustc_middle::mir;
13 use rustc_middle::mir::interpret::ErrorHandled;
14 use rustc_middle::traits::Reveal;
15 use rustc_middle::ty::print::with_no_trimmed_paths;
16 use rustc_middle::ty::{self, subst::Subst, TyCtxt};
17 use rustc_span::source_map::Span;
18 use rustc_target::abi::{Abi, LayoutOf};
19 use std::borrow::Cow;
20 use std::convert::TryInto;
21
22 pub fn note_on_undefined_behavior_error() -> &'static str {
23     "The rules on what exactly is undefined behavior aren't clear, \
24      so this check might be overzealous. Please open an issue on the rustc \
25      repository if you believe it should not be considered undefined behavior."
26 }
27
28 // Returns a pointer to where the result lives
29 fn eval_body_using_ecx<'mir, 'tcx>(
30     ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
31     cid: GlobalId<'tcx>,
32     body: &'mir mir::Body<'tcx>,
33 ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
34     debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
35     let tcx = *ecx.tcx;
36     assert!(
37         cid.promoted.is_some()
38             || matches!(
39                 ecx.tcx.def_kind(cid.instance.def_id()),
40                 DefKind::Const
41                     | DefKind::Static
42                     | DefKind::ConstParam
43                     | DefKind::AnonConst
44                     | DefKind::AssocConst
45             ),
46         "Unexpected DefKind: {:?}",
47         ecx.tcx.def_kind(cid.instance.def_id())
48     );
49     let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
50     assert!(!layout.is_unsized());
51     let ret = ecx.allocate(layout, MemoryKind::Stack)?;
52
53     let name =
54         with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));
55     let prom = cid.promoted.map_or_else(String::new, |p| format!("::promoted[{:?}]", p));
56     trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
57
58     ecx.push_stack_frame(
59         cid.instance,
60         body,
61         Some(&ret.into()),
62         StackPopCleanup::None { cleanup: false },
63     )?;
64
65     // The main interpreter loop.
66     ecx.run()?;
67
68     // Intern the result
69     let intern_kind = if cid.promoted.is_some() {
70         InternKind::Promoted
71     } else {
72         match tcx.static_mutability(cid.instance.def_id()) {
73             Some(m) => InternKind::Static(m),
74             None => InternKind::Constant,
75         }
76     };
77     intern_const_alloc_recursive(ecx, intern_kind, &ret)?;
78
79     debug!("eval_body_using_ecx done: {:?}", *ret);
80     Ok(ret)
81 }
82
83 /// The `InterpCx` is only meant to be used to do field and index projections into constants for
84 /// `simd_shuffle` and const patterns in match arms.
85 ///
86 /// The function containing the `match` that is currently being analyzed may have generic bounds
87 /// that inform us about the generic bounds of the constant. E.g., using an associated constant
88 /// of a function's generic parameter will require knowledge about the bounds on the generic
89 /// parameter. These bounds are passed to `mk_eval_cx` via the `ParamEnv` argument.
90 pub(super) fn mk_eval_cx<'mir, 'tcx>(
91     tcx: TyCtxt<'tcx>,
92     root_span: Span,
93     param_env: ty::ParamEnv<'tcx>,
94     can_access_statics: bool,
95 ) -> CompileTimeEvalContext<'mir, 'tcx> {
96     debug!("mk_eval_cx: {:?}", param_env);
97     InterpCx::new(
98         tcx,
99         root_span,
100         param_env,
101         CompileTimeInterpreter::new(tcx.const_eval_limit()),
102         MemoryExtra { can_access_statics },
103     )
104 }
105
106 /// This function converts an interpreter value into a constant that is meant for use in the
107 /// type system.
108 pub(super) fn op_to_const<'tcx>(
109     ecx: &CompileTimeEvalContext<'_, 'tcx>,
110     op: &OpTy<'tcx>,
111 ) -> ConstValue<'tcx> {
112     // We do not have value optimizations for everything.
113     // Only scalars and slices, since they are very common.
114     // Note that further down we turn scalars of uninitialized bits back to `ByRef`. These can result
115     // from scalar unions that are initialized with one of their zero sized variants. We could
116     // instead allow `ConstValue::Scalar` to store `ScalarMaybeUninit`, but that would affect all
117     // the usual cases of extracting e.g. a `usize`, without there being a real use case for the
118     // `Undef` situation.
119     let try_as_immediate = match op.layout.abi {
120         Abi::Scalar(..) => true,
121         Abi::ScalarPair(..) => match op.layout.ty.kind() {
122             ty::Ref(_, inner, _) => match *inner.kind() {
123                 ty::Slice(elem) => elem == ecx.tcx.types.u8,
124                 ty::Str => true,
125                 _ => false,
126             },
127             _ => false,
128         },
129         _ => false,
130     };
131     let immediate = if try_as_immediate {
132         Err(ecx.read_immediate(op).expect("normalization works on validated constants"))
133     } else {
134         // It is guaranteed that any non-slice scalar pair is actually ByRef here.
135         // When we come back from raw const eval, we are always by-ref. The only way our op here is
136         // by-val is if we are in destructure_const, i.e., if this is (a field of) something that we
137         // "tried to make immediate" before. We wouldn't do that for non-slice scalar pairs or
138         // structs containing such.
139         op.try_as_mplace(ecx)
140     };
141
142     let to_const_value = |mplace: &MPlaceTy<'_>| match mplace.ptr {
143         Scalar::Ptr(ptr) => {
144             let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
145             ConstValue::ByRef { alloc, offset: ptr.offset }
146         }
147         Scalar::Int(int) => {
148             assert!(mplace.layout.is_zst());
149             assert_eq!(
150                 int.assert_bits(ecx.tcx.data_layout.pointer_size)
151                     % u128::from(mplace.layout.align.abi.bytes()),
152                 0,
153                 "this MPlaceTy must come from a validated constant, thus we can assume the \
154                 alignment is correct",
155             );
156             ConstValue::Scalar(Scalar::ZST)
157         }
158     };
159     match immediate {
160         Ok(ref mplace) => to_const_value(mplace),
161         // see comment on `let try_as_immediate` above
162         Err(imm) => match *imm {
163             Immediate::Scalar(x) => match x {
164                 ScalarMaybeUninit::Scalar(s) => ConstValue::Scalar(s),
165                 ScalarMaybeUninit::Uninit => to_const_value(&op.assert_mem_place(ecx)),
166             },
167             Immediate::ScalarPair(a, b) => {
168                 let (data, start) = match a.check_init().unwrap() {
169                     Scalar::Ptr(ptr) => {
170                         (ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
171                     }
172                     Scalar::Int { .. } => (
173                         ecx.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
174                             b"" as &[u8],
175                         )),
176                         0,
177                     ),
178                 };
179                 let len = b.to_machine_usize(ecx).unwrap();
180                 let start = start.try_into().unwrap();
181                 let len: usize = len.try_into().unwrap();
182                 ConstValue::Slice { data, start, end: start + len }
183             }
184         },
185     }
186 }
187
188 fn turn_into_const_value<'tcx>(
189     tcx: TyCtxt<'tcx>,
190     constant: ConstAlloc<'tcx>,
191     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
192 ) -> ConstValue<'tcx> {
193     let cid = key.value;
194     let def_id = cid.instance.def.def_id();
195     let is_static = tcx.is_static(def_id);
196     let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env, is_static);
197
198     let mplace = ecx.raw_const_to_mplace(constant).expect(
199         "can only fail if layout computation failed, \
200         which should have given a good error before ever invoking this function",
201     );
202     assert!(
203         !is_static || cid.promoted.is_some(),
204         "the `eval_to_const_value_raw` query should not be used for statics, use `eval_to_allocation` instead"
205     );
206     // Turn this into a proper constant.
207     op_to_const(&ecx, &mplace.into())
208 }
209
210 pub fn eval_to_const_value_raw_provider<'tcx>(
211     tcx: TyCtxt<'tcx>,
212     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
213 ) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
214     // see comment in eval_to_allocation_raw_provider for what we're doing here
215     if key.param_env.reveal() == Reveal::All {
216         let mut key = key;
217         key.param_env = key.param_env.with_user_facing();
218         match tcx.eval_to_const_value_raw(key) {
219             // try again with reveal all as requested
220             Err(ErrorHandled::TooGeneric) => {}
221             // deduplicate calls
222             other => return other,
223         }
224     }
225
226     // We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
227     // Catch such calls and evaluate them instead of trying to load a constant's MIR.
228     if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
229         let ty = key.value.instance.ty(tcx, key.param_env);
230         let substs = match ty.kind() {
231             ty::FnDef(_, substs) => substs,
232             _ => bug!("intrinsic with type {:?}", ty),
233         };
234         return eval_nullary_intrinsic(tcx, key.param_env, def_id, substs).map_err(|error| {
235             let span = tcx.def_span(def_id);
236             let error = ConstEvalErr { error: error.into_kind(), stacktrace: vec![], span };
237             error.report_as_error(tcx.at(span), "could not evaluate nullary intrinsic")
238         });
239     }
240
241     tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
242 }
243
244 pub fn eval_to_allocation_raw_provider<'tcx>(
245     tcx: TyCtxt<'tcx>,
246     key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
247 ) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
248     // Because the constant is computed twice (once per value of `Reveal`), we are at risk of
249     // reporting the same error twice here. To resolve this, we check whether we can evaluate the
250     // constant in the more restrictive `Reveal::UserFacing`, which most likely already was
251     // computed. For a large percentage of constants that will already have succeeded. Only
252     // associated constants of generic functions will fail due to not enough monomorphization
253     // information being available.
254
255     // In case we fail in the `UserFacing` variant, we just do the real computation.
256     if key.param_env.reveal() == Reveal::All {
257         let mut key = key;
258         key.param_env = key.param_env.with_user_facing();
259         match tcx.eval_to_allocation_raw(key) {
260             // try again with reveal all as requested
261             Err(ErrorHandled::TooGeneric) => {}
262             // deduplicate calls
263             other => return other,
264         }
265     }
266     if cfg!(debug_assertions) {
267         // Make sure we format the instance even if we do not print it.
268         // This serves as a regression test against an ICE on printing.
269         // The next two lines concatenated contain some discussion:
270         // https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
271         // subject/anon_const_instance_printing/near/135980032
272         let instance = with_no_trimmed_paths(|| key.value.instance.to_string());
273         trace!("const eval: {:?} ({})", key, instance);
274     }
275
276     let cid = key.value;
277     let def = cid.instance.def.with_opt_param();
278
279     if let Some(def) = def.as_local() {
280         if tcx.has_typeck_results(def.did) {
281             if let Some(error_reported) = tcx.typeck_opt_const_arg(def).tainted_by_errors {
282                 return Err(ErrorHandled::Reported(error_reported));
283             }
284         }
285         if !tcx.is_mir_available(def.did) {
286             tcx.sess.delay_span_bug(
287                 tcx.def_span(def.did),
288                 &format!("no MIR body is available for {:?}", def.did),
289             );
290             return Err(ErrorHandled::Reported(ErrorReported {}));
291         }
292         if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
293             return Err(ErrorHandled::Reported(error_reported));
294         }
295     }
296
297     let is_static = tcx.is_static(def.did);
298
299     let mut ecx = InterpCx::new(
300         tcx,
301         tcx.def_span(def.did),
302         key.param_env,
303         CompileTimeInterpreter::new(tcx.const_eval_limit()),
304         // Statics (and promoteds inside statics) may access other statics, because unlike consts
305         // they do not have to behave "as if" they were evaluated at runtime.
306         MemoryExtra { can_access_statics: is_static },
307     );
308
309     let res = ecx.load_mir(cid.instance.def, cid.promoted);
310     match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body)) {
311         Err(error) => {
312             let err = ConstEvalErr::new(&ecx, error, None);
313             // Some CTFE errors raise just a lint, not a hard error; see
314             // <https://github.com/rust-lang/rust/issues/71800>.
315             let is_hard_err = if let Some(def) = def.as_local() {
316                 // (Associated) consts only emit a lint, since they might be unused.
317                 !matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
318                     // check if the inner InterpError is hard
319                     || err.error.is_hard_err()
320             } else {
321                 // use of broken constant from other crate: always an error
322                 true
323             };
324
325             if is_hard_err {
326                 let msg = if is_static {
327                     Cow::from("could not evaluate static initializer")
328                 } else {
329                     // If the current item has generics, we'd like to enrich the message with the
330                     // instance and its substs: to show the actual compile-time values, in addition to
331                     // the expression, leading to the const eval error.
332                     let instance = &key.value.instance;
333                     if !instance.substs.is_empty() {
334                         let instance = with_no_trimmed_paths(|| instance.to_string());
335                         let msg = format!("evaluation of `{}` failed", instance);
336                         Cow::from(msg)
337                     } else {
338                         Cow::from("evaluation of constant value failed")
339                     }
340                 };
341
342                 Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
343             } else {
344                 let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
345                 Err(err.report_as_lint(
346                     tcx.at(tcx.def_span(def.did)),
347                     "any use of this value will cause an error",
348                     hir_id,
349                     Some(err.span),
350                 ))
351             }
352         }
353         Ok(mplace) => {
354             // Since evaluation had no errors, validate the resulting constant.
355             // This is a separate `try` block to provide more targeted error reporting.
356             let validation = try {
357                 let mut ref_tracking = RefTracking::new(mplace);
358                 let mut inner = false;
359                 while let Some((mplace, path)) = ref_tracking.todo.pop() {
360                     let mode = match tcx.static_mutability(cid.instance.def_id()) {
361                         Some(_) if cid.promoted.is_some() => {
362                             // Promoteds in statics are allowed to point to statics.
363                             CtfeValidationMode::Const { inner, allow_static_ptrs: true }
364                         }
365                         Some(_) => CtfeValidationMode::Regular, // a `static`
366                         None => CtfeValidationMode::Const { inner, allow_static_ptrs: false },
367                     };
368                     ecx.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?;
369                     inner = true;
370                 }
371             };
372             if let Err(error) = validation {
373                 // Validation failed, report an error. This is always a hard error.
374                 let err = ConstEvalErr::new(&ecx, error, None);
375                 Err(err.struct_error(
376                     ecx.tcx,
377                     "it is undefined behavior to use this value",
378                     |mut diag| {
379                         diag.note(note_on_undefined_behavior_error());
380                         diag.note(&format!(
381                             "the raw bytes of the constant ({}",
382                             display_allocation(
383                                 *ecx.tcx,
384                                 ecx.tcx
385                                     .global_alloc(mplace.ptr.assert_ptr().alloc_id)
386                                     .unwrap_memory()
387                             )
388                         ));
389                         diag.emit();
390                     },
391                 ))
392             } else {
393                 // Convert to raw constant
394                 Ok(ConstAlloc { alloc_id: mplace.ptr.assert_ptr().alloc_id, ty: mplace.layout.ty })
395             }
396         }
397     }
398 }