]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/qualify_consts.rs
rustc_mir: double-check const-promotion candidates for sanity.
[rust.git] / src / librustc_mir / transform / qualify_consts.rs
1 //! A pass that qualifies constness of temporaries in constants,
2 //! static initializers and functions and also drives promotion.
3 //!
4 //! The Qualif flags below can be used to also provide better
5 //! diagnostics as to why a constant rvalue wasn't promoted.
6
7 use rustc_index::bit_set::BitSet;
8 use rustc_index::vec::IndexVec;
9 use rustc_data_structures::fx::FxHashSet;
10 use rustc_target::spec::abi::Abi;
11 use rustc::hir;
12 use rustc::hir::def_id::DefId;
13 use rustc::traits::{self, TraitEngine};
14 use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
15 use rustc::ty::cast::CastTy;
16 use rustc::ty::query::Providers;
17 use rustc::mir::*;
18 use rustc::mir::interpret::ConstValue;
19 use rustc::mir::traversal::ReversePostorder;
20 use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext, NonMutatingUseContext};
21 use rustc::middle::lang_items;
22 use rustc::session::config::nightly_options;
23 use syntax::ast::LitKind;
24 use syntax::feature_gate::{emit_feature_err, GateIssue};
25 use syntax::symbol::sym;
26 use syntax_pos::{Span, DUMMY_SP};
27
28 use std::borrow::Cow;
29 use std::cell::Cell;
30 use std::fmt;
31 use std::ops::{Deref, Index, IndexMut};
32 use std::usize;
33
34 use rustc::hir::HirId;
35 use crate::transform::{MirPass, MirSource};
36 use super::promote_consts::{self, Candidate, TempState};
37 use crate::transform::check_consts::ops::{self, NonConstOp};
38
39 /// What kind of item we are in.
40 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
41 enum Mode {
42     /// A `static` item.
43     Static,
44     /// A `static mut` item.
45     StaticMut,
46     /// A `const fn` item.
47     ConstFn,
48     /// A `const` item or an anonymous constant (e.g. in array lengths).
49     Const,
50     /// Other type of `fn`.
51     NonConstFn,
52 }
53
54 impl Mode {
55     /// Determine whether we have to do full const-checking because syntactically, we
56     /// are required to be "const".
57     #[inline]
58     fn requires_const_checking(self) -> bool {
59         self != Mode::NonConstFn
60     }
61 }
62
63 impl fmt::Display for Mode {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         match *self {
66             Mode::Const => write!(f, "constant"),
67             Mode::Static | Mode::StaticMut => write!(f, "static"),
68             Mode::ConstFn => write!(f, "constant function"),
69             Mode::NonConstFn => write!(f, "function")
70         }
71     }
72 }
73
74 const QUALIF_COUNT: usize = 4;
75
76 // FIXME(eddyb) once we can use const generics, replace this array with
77 // something like `IndexVec` but for fixed-size arrays (`IndexArray`?).
78 #[derive(Copy, Clone, Default)]
79 struct PerQualif<T>([T; QUALIF_COUNT]);
80
81 impl<T: Clone> PerQualif<T> {
82     fn new(x: T) -> Self {
83         PerQualif([x.clone(), x.clone(), x.clone(), x])
84     }
85 }
86
87 impl<T> PerQualif<T> {
88     fn as_mut(&mut self) -> PerQualif<&mut T> {
89         let [x0, x1, x2, x3] = &mut self.0;
90         PerQualif([x0, x1, x2, x3])
91     }
92
93     fn zip<U>(self, other: PerQualif<U>) -> PerQualif<(T, U)> {
94         let [x0, x1, x2, x3] = self.0;
95         let [y0, y1, y2, y3] = other.0;
96         PerQualif([(x0, y0), (x1, y1), (x2, y2), (x3, y3)])
97     }
98 }
99
100 impl PerQualif<bool> {
101     fn encode_to_bits(self) -> u8 {
102         self.0.iter().enumerate().fold(0, |bits, (i, &qualif)| {
103             bits | ((qualif as u8) << i)
104         })
105     }
106
107     fn decode_from_bits(bits: u8) -> Self {
108         let mut qualifs = Self::default();
109         for (i, qualif) in qualifs.0.iter_mut().enumerate() {
110             *qualif = (bits & (1 << i)) != 0;
111         }
112         qualifs
113     }
114 }
115
116 impl<Q: Qualif, T> Index<Q> for PerQualif<T> {
117     type Output = T;
118
119     fn index(&self, _: Q) -> &T {
120         &self.0[Q::IDX]
121     }
122 }
123
124 impl<Q: Qualif, T> IndexMut<Q> for PerQualif<T> {
125     fn index_mut(&mut self, _: Q) -> &mut T {
126         &mut self.0[Q::IDX]
127     }
128 }
129
130 struct ConstCx<'a, 'tcx> {
131     tcx: TyCtxt<'tcx>,
132     param_env: ty::ParamEnv<'tcx>,
133     mode: Mode,
134     body: &'a Body<'tcx>,
135
136     per_local: PerQualif<BitSet<Local>>,
137 }
138
139 impl<'a, 'tcx> ConstCx<'a, 'tcx> {
140     fn is_const_panic_fn(&self, def_id: DefId) -> bool {
141         Some(def_id) == self.tcx.lang_items().panic_fn() ||
142         Some(def_id) == self.tcx.lang_items().begin_panic_fn()
143     }
144 }
145
146 #[derive(Copy, Clone, Debug)]
147 enum ValueSource<'a, 'tcx> {
148     Rvalue(&'a Rvalue<'tcx>),
149     DropAndReplace(&'a Operand<'tcx>),
150     Call {
151         callee: &'a Operand<'tcx>,
152         args: &'a [Operand<'tcx>],
153         return_ty: Ty<'tcx>,
154     },
155 }
156
157 /// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
158 /// code for promotion or prevent it from evaluating at compile time. So `return true` means
159 /// "I found something bad, no reason to go on searching". `false` is only returned if we
160 /// definitely cannot find anything bad anywhere.
161 ///
162 /// The default implementations proceed structurally.
163 trait Qualif {
164     const IDX: usize;
165
166     /// Return the qualification that is (conservatively) correct for any value
167     /// of the type, or `None` if the qualification is not value/type-based.
168     fn in_any_value_of_ty(_cx: &ConstCx<'_, 'tcx>, _ty: Ty<'tcx>) -> Option<bool> {
169         None
170     }
171
172     /// Return a mask for the qualification, given a type. This is `false` iff
173     /// no value of that type can have the qualification.
174     fn mask_for_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
175         Self::in_any_value_of_ty(cx, ty).unwrap_or(true)
176     }
177
178     fn in_local(cx: &ConstCx<'_, '_>, local: Local) -> bool {
179         cx.per_local.0[Self::IDX].contains(local)
180     }
181
182     fn in_static(_cx: &ConstCx<'_, 'tcx>, _static: &Static<'tcx>) -> bool {
183         // FIXME(eddyb) should we do anything here for value properties?
184         false
185     }
186
187     fn in_projection_structurally(
188         cx: &ConstCx<'_, 'tcx>,
189         place: PlaceRef<'_, 'tcx>,
190     ) -> bool {
191         if let [proj_base @ .., elem] = place.projection {
192             let base_qualif = Self::in_place(cx, PlaceRef {
193                 base: place.base,
194                 projection: proj_base,
195             });
196             let qualif = base_qualif && Self::mask_for_ty(
197                 cx,
198                 Place::ty_from(place.base, proj_base, cx.body, cx.tcx)
199                     .projection_ty(cx.tcx, elem)
200                     .ty,
201             );
202             match elem {
203                 ProjectionElem::Deref |
204                 ProjectionElem::Subslice { .. } |
205                 ProjectionElem::Field(..) |
206                 ProjectionElem::ConstantIndex { .. } |
207                 ProjectionElem::Downcast(..) => qualif,
208
209                 // FIXME(eddyb) shouldn't this be masked *after* including the
210                 // index local? Then again, it's `usize` which is neither
211                 // `HasMutInterior` nor `NeedsDrop`.
212                 ProjectionElem::Index(local) => qualif || Self::in_local(cx, *local),
213             }
214         } else {
215             bug!("This should be called if projection is not empty");
216         }
217     }
218
219     fn in_projection(
220         cx: &ConstCx<'_, 'tcx>,
221         place: PlaceRef<'_, 'tcx>,
222     ) -> bool {
223         Self::in_projection_structurally(cx, place)
224     }
225
226     fn in_place(cx: &ConstCx<'_, 'tcx>, place: PlaceRef<'_, 'tcx>) -> bool {
227         match place {
228             PlaceRef {
229                 base: PlaceBase::Local(local),
230                 projection: [],
231             } => Self::in_local(cx, *local),
232             PlaceRef {
233                 base: PlaceBase::Static(box Static {
234                     kind: StaticKind::Promoted(..),
235                     ..
236                 }),
237                 projection: [],
238             } => bug!("qualifying already promoted MIR"),
239             PlaceRef {
240                 base: PlaceBase::Static(static_),
241                 projection: [],
242             } => {
243                 Self::in_static(cx, static_)
244             },
245             PlaceRef {
246                 base: _,
247                 projection: [.., _],
248             } => Self::in_projection(cx, place),
249         }
250     }
251
252     fn in_operand(cx: &ConstCx<'_, 'tcx>, operand: &Operand<'tcx>) -> bool {
253         match *operand {
254             Operand::Copy(ref place) |
255             Operand::Move(ref place) => Self::in_place(cx, place.as_ref()),
256
257             Operand::Constant(ref constant) => {
258                 if let ConstValue::Unevaluated(def_id, _) = constant.literal.val {
259                     // Don't peek inside trait associated constants.
260                     if cx.tcx.trait_of_item(def_id).is_some() {
261                         Self::in_any_value_of_ty(cx, constant.literal.ty).unwrap_or(false)
262                     } else {
263                         let (bits, _) = cx.tcx.at(constant.span).mir_const_qualif(def_id);
264
265                         let qualif = PerQualif::decode_from_bits(bits).0[Self::IDX];
266
267                         // Just in case the type is more specific than
268                         // the definition, e.g., impl associated const
269                         // with type parameters, take it into account.
270                         qualif && Self::mask_for_ty(cx, constant.literal.ty)
271                     }
272                 } else {
273                     false
274                 }
275             }
276         }
277     }
278
279     fn in_rvalue_structurally(cx: &ConstCx<'_, 'tcx>, rvalue: &Rvalue<'tcx>) -> bool {
280         match *rvalue {
281             Rvalue::NullaryOp(..) => false,
282
283             Rvalue::Discriminant(ref place) |
284             Rvalue::Len(ref place) => Self::in_place(cx, place.as_ref()),
285
286             Rvalue::Use(ref operand) |
287             Rvalue::Repeat(ref operand, _) |
288             Rvalue::UnaryOp(_, ref operand) |
289             Rvalue::Cast(_, ref operand, _) => Self::in_operand(cx, operand),
290
291             Rvalue::BinaryOp(_, ref lhs, ref rhs) |
292             Rvalue::CheckedBinaryOp(_, ref lhs, ref rhs) => {
293                 Self::in_operand(cx, lhs) || Self::in_operand(cx, rhs)
294             }
295
296             Rvalue::Ref(_, _, ref place) => {
297                 // Special-case reborrows to be more like a copy of the reference.
298                 if let &[ref proj_base @ .., elem] = place.projection.as_ref() {
299                     if ProjectionElem::Deref == elem {
300                         let base_ty = Place::ty_from(&place.base, proj_base, cx.body, cx.tcx).ty;
301                         if let ty::Ref(..) = base_ty.kind {
302                             return Self::in_place(cx, PlaceRef {
303                                 base: &place.base,
304                                 projection: proj_base,
305                             });
306                         }
307                     }
308                 }
309
310                 Self::in_place(cx, place.as_ref())
311             }
312
313             Rvalue::Aggregate(_, ref operands) => {
314                 operands.iter().any(|o| Self::in_operand(cx, o))
315             }
316         }
317     }
318
319     fn in_rvalue(cx: &ConstCx<'_, 'tcx>, rvalue: &Rvalue<'tcx>) -> bool {
320         Self::in_rvalue_structurally(cx, rvalue)
321     }
322
323     fn in_call(
324         cx: &ConstCx<'_, 'tcx>,
325         _callee: &Operand<'tcx>,
326         _args: &[Operand<'tcx>],
327         return_ty: Ty<'tcx>,
328     ) -> bool {
329         // Be conservative about the returned value of a const fn.
330         Self::in_any_value_of_ty(cx, return_ty).unwrap_or(false)
331     }
332
333     fn in_value(cx: &ConstCx<'_, 'tcx>, source: ValueSource<'_, 'tcx>) -> bool {
334         match source {
335             ValueSource::Rvalue(rvalue) => Self::in_rvalue(cx, rvalue),
336             ValueSource::DropAndReplace(source) => Self::in_operand(cx, source),
337             ValueSource::Call { callee, args, return_ty } => {
338                 Self::in_call(cx, callee, args, return_ty)
339             }
340         }
341     }
342 }
343
344 /// Constant containing interior mutability (`UnsafeCell<T>`).
345 /// This must be ruled out to make sure that evaluating the constant at compile-time
346 /// and at *any point* during the run-time would produce the same result. In particular,
347 /// promotion of temporaries must not change program behavior; if the promoted could be
348 /// written to, that would be a problem.
349 struct HasMutInterior;
350
351 impl Qualif for HasMutInterior {
352     const IDX: usize = 0;
353
354     fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> Option<bool> {
355         Some(!ty.is_freeze(cx.tcx, cx.param_env, DUMMY_SP))
356     }
357
358     fn in_rvalue(cx: &ConstCx<'_, 'tcx>, rvalue: &Rvalue<'tcx>) -> bool {
359         match *rvalue {
360             // Returning `true` for `Rvalue::Ref` indicates the borrow isn't
361             // allowed in constants (and the `Checker` will error), and/or it
362             // won't be promoted, due to `&mut ...` or interior mutability.
363             Rvalue::Ref(_, kind, ref place) => {
364                 let ty = place.ty(cx.body, cx.tcx).ty;
365
366                 if let BorrowKind::Mut { .. } = kind {
367                     // In theory, any zero-sized value could be borrowed
368                     // mutably without consequences. However, only &mut []
369                     // is allowed right now, and only in functions.
370                     if cx.mode == Mode::StaticMut {
371                         // Inside a `static mut`, &mut [...] is also allowed.
372                         match ty.kind {
373                             ty::Array(..) | ty::Slice(_) => {}
374                             _ => return true,
375                         }
376                     } else if let ty::Array(_, len) = ty.kind {
377                         // FIXME(eddyb) the `cx.mode == Mode::NonConstFn` condition
378                         // seems unnecessary, given that this is merely a ZST.
379                         match len.try_eval_usize(cx.tcx, cx.param_env) {
380                             Some(0) if cx.mode == Mode::NonConstFn => {},
381                             _ => return true,
382                         }
383                     } else {
384                         return true;
385                     }
386                 }
387             }
388
389             Rvalue::Aggregate(ref kind, _) => {
390                 if let AggregateKind::Adt(def, ..) = **kind {
391                     if Some(def.did) == cx.tcx.lang_items().unsafe_cell_type() {
392                         let ty = rvalue.ty(cx.body, cx.tcx);
393                         assert_eq!(Self::in_any_value_of_ty(cx, ty), Some(true));
394                         return true;
395                     }
396                 }
397             }
398
399             _ => {}
400         }
401
402         Self::in_rvalue_structurally(cx, rvalue)
403     }
404 }
405
406 /// Constant containing an ADT that implements `Drop`.
407 /// This must be ruled out (a) because we cannot run `Drop` during compile-time
408 /// as that might not be a `const fn`, and (b) because implicit promotion would
409 /// remove side-effects that occur as part of dropping that value.
410 struct NeedsDrop;
411
412 impl Qualif for NeedsDrop {
413     const IDX: usize = 1;
414
415     fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> Option<bool> {
416         Some(ty.needs_drop(cx.tcx, cx.param_env))
417     }
418
419     fn in_rvalue(cx: &ConstCx<'_, 'tcx>, rvalue: &Rvalue<'tcx>) -> bool {
420         if let Rvalue::Aggregate(ref kind, _) = *rvalue {
421             if let AggregateKind::Adt(def, ..) = **kind {
422                 if def.has_dtor(cx.tcx) {
423                     return true;
424                 }
425             }
426         }
427
428         Self::in_rvalue_structurally(cx, rvalue)
429     }
430 }
431
432 /// Not promotable at all - non-`const fn` calls, `asm!`,
433 /// pointer comparisons, ptr-to-int casts, etc.
434 /// Inside a const context all constness rules apply, so promotion simply has to follow the regular
435 /// constant rules (modulo interior mutability or `Drop` rules which are handled `HasMutInterior`
436 /// and `NeedsDrop` respectively). Basically this duplicates the checks that the const-checking
437 /// visitor enforces by emitting errors when working in const context.
438 struct IsNotPromotable;
439
440 impl Qualif for IsNotPromotable {
441     const IDX: usize = 2;
442
443     fn in_static(cx: &ConstCx<'_, 'tcx>, static_: &Static<'tcx>) -> bool {
444         match static_.kind {
445             StaticKind::Promoted(_, _) => unreachable!(),
446             StaticKind::Static => {
447                 // Only allow statics (not consts) to refer to other statics.
448                 // FIXME(eddyb) does this matter at all for promotion?
449                 let allowed = cx.mode == Mode::Static || cx.mode == Mode::StaticMut;
450
451                 !allowed ||
452                     cx.tcx.get_attrs(static_.def_id).iter().any(
453                         |attr| attr.check_name(sym::thread_local)
454                     )
455             }
456         }
457     }
458
459     fn in_projection(
460         cx: &ConstCx<'_, 'tcx>,
461         place: PlaceRef<'_, 'tcx>,
462     ) -> bool {
463         if let [proj_base @ .., elem] = place.projection {
464             match elem {
465                 ProjectionElem::Deref |
466                 ProjectionElem::Downcast(..) => return true,
467
468                 ProjectionElem::ConstantIndex {..} |
469                 ProjectionElem::Subslice {..} |
470                 ProjectionElem::Index(_) => {}
471
472                 ProjectionElem::Field(..) => {
473                     if cx.mode == Mode::NonConstFn {
474                         let base_ty = Place::ty_from(place.base, proj_base, cx.body, cx.tcx).ty;
475                         if let Some(def) = base_ty.ty_adt_def() {
476                             // No promotion of union field accesses.
477                             if def.is_union() {
478                                 return true;
479                             }
480                         }
481                     }
482                 }
483             }
484
485             Self::in_projection_structurally(cx, place)
486         } else {
487             bug!("This should be called if projection is not empty");
488         }
489     }
490
491     fn in_rvalue(cx: &ConstCx<'_, 'tcx>, rvalue: &Rvalue<'tcx>) -> bool {
492         match *rvalue {
493             Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) if cx.mode == Mode::NonConstFn => {
494                 let operand_ty = operand.ty(cx.body, cx.tcx);
495                 let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
496                 let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
497                 match (cast_in, cast_out) {
498                     (CastTy::Ptr(_), CastTy::Int(_)) |
499                     (CastTy::FnPtr, CastTy::Int(_)) => {
500                         // in normal functions, mark such casts as not promotable
501                         return true;
502                     }
503                     _ => {}
504                 }
505             }
506
507             Rvalue::BinaryOp(op, ref lhs, _) if cx.mode == Mode::NonConstFn => {
508                 if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(cx.body, cx.tcx).kind {
509                     assert!(op == BinOp::Eq || op == BinOp::Ne ||
510                             op == BinOp::Le || op == BinOp::Lt ||
511                             op == BinOp::Ge || op == BinOp::Gt ||
512                             op == BinOp::Offset);
513
514                     // raw pointer operations are not allowed inside promoteds
515                     return true;
516                 }
517             }
518
519             Rvalue::NullaryOp(NullOp::Box, _) => return true,
520
521             _ => {}
522         }
523
524         Self::in_rvalue_structurally(cx, rvalue)
525     }
526
527     fn in_call(
528         cx: &ConstCx<'_, 'tcx>,
529         callee: &Operand<'tcx>,
530         args: &[Operand<'tcx>],
531         _return_ty: Ty<'tcx>,
532     ) -> bool {
533         let fn_ty = callee.ty(cx.body, cx.tcx);
534         match fn_ty.kind {
535             ty::FnDef(def_id, _) => {
536                 match cx.tcx.fn_sig(def_id).abi() {
537                     Abi::RustIntrinsic |
538                     Abi::PlatformIntrinsic => {
539                         assert!(!cx.tcx.is_const_fn(def_id));
540                         match &cx.tcx.item_name(def_id).as_str()[..] {
541                             | "size_of"
542                             | "min_align_of"
543                             | "needs_drop"
544                             | "type_id"
545                             | "bswap"
546                             | "bitreverse"
547                             | "ctpop"
548                             | "cttz"
549                             | "cttz_nonzero"
550                             | "ctlz"
551                             | "ctlz_nonzero"
552                             | "wrapping_add"
553                             | "wrapping_sub"
554                             | "wrapping_mul"
555                             | "unchecked_shl"
556                             | "unchecked_shr"
557                             | "rotate_left"
558                             | "rotate_right"
559                             | "add_with_overflow"
560                             | "sub_with_overflow"
561                             | "mul_with_overflow"
562                             | "saturating_add"
563                             | "saturating_sub"
564                             | "transmute"
565                             | "simd_insert"
566                             | "simd_extract"
567                             => return true,
568
569                             _ => {}
570                         }
571                     }
572                     _ => {
573                         let is_const_fn =
574                             cx.tcx.is_const_fn(def_id) ||
575                             cx.tcx.is_unstable_const_fn(def_id).is_some() ||
576                             cx.is_const_panic_fn(def_id);
577                         if !is_const_fn {
578                             return true;
579                         }
580                     }
581                 }
582             }
583             _ => return true,
584         }
585
586         Self::in_operand(cx, callee) || args.iter().any(|arg| Self::in_operand(cx, arg))
587     }
588 }
589
590 /// Refers to temporaries which cannot be promoted *implicitly*.
591 /// Explicit promotion happens e.g. for constant arguments declared via `rustc_args_required_const`.
592 /// Implicit promotion has almost the same rules, except that disallows `const fn` except for
593 /// those marked `#[rustc_promotable]`. This is to avoid changing a legitimate run-time operation
594 /// into a failing compile-time operation e.g. due to addresses being compared inside the function.
595 struct IsNotImplicitlyPromotable;
596
597 impl Qualif for IsNotImplicitlyPromotable {
598     const IDX: usize = 3;
599
600     fn in_call(
601         cx: &ConstCx<'_, 'tcx>,
602         callee: &Operand<'tcx>,
603         args: &[Operand<'tcx>],
604         _return_ty: Ty<'tcx>,
605     ) -> bool {
606         if cx.mode == Mode::NonConstFn {
607             if let ty::FnDef(def_id, _) = callee.ty(cx.body, cx.tcx).kind {
608                 // Never promote runtime `const fn` calls of
609                 // functions without `#[rustc_promotable]`.
610                 if !cx.tcx.is_promotable_const_fn(def_id) {
611                     return true;
612                 }
613             }
614         }
615
616         Self::in_operand(cx, callee) || args.iter().any(|arg| Self::in_operand(cx, arg))
617     }
618 }
619
620 // Ensure the `IDX` values are sequential (`0..QUALIF_COUNT`).
621 macro_rules! static_assert_seq_qualifs {
622     ($i:expr => $first:ident $(, $rest:ident)*) => {
623         static_assert!({
624             static_assert_seq_qualifs!($i + 1 => $($rest),*);
625
626             $first::IDX == $i
627         });
628     };
629     ($i:expr =>) => {
630         static_assert!(QUALIF_COUNT == $i);
631     };
632 }
633 static_assert_seq_qualifs!(
634     0 => HasMutInterior, NeedsDrop, IsNotPromotable, IsNotImplicitlyPromotable
635 );
636
637 impl ConstCx<'_, 'tcx> {
638     fn qualifs_in_any_value_of_ty(&self, ty: Ty<'tcx>) -> PerQualif<bool> {
639         let mut qualifs = PerQualif::default();
640         qualifs[HasMutInterior] = HasMutInterior::in_any_value_of_ty(self, ty).unwrap_or(false);
641         qualifs[NeedsDrop] = NeedsDrop::in_any_value_of_ty(self, ty).unwrap_or(false);
642         qualifs[IsNotPromotable] = IsNotPromotable::in_any_value_of_ty(self, ty).unwrap_or(false);
643         qualifs[IsNotImplicitlyPromotable] =
644             IsNotImplicitlyPromotable::in_any_value_of_ty(self, ty).unwrap_or(false);
645         qualifs
646     }
647
648     fn qualifs_in_local(&self, local: Local) -> PerQualif<bool> {
649         let mut qualifs = PerQualif::default();
650         qualifs[HasMutInterior] = HasMutInterior::in_local(self, local);
651         qualifs[NeedsDrop] = NeedsDrop::in_local(self, local);
652         qualifs[IsNotPromotable] = IsNotPromotable::in_local(self, local);
653         qualifs[IsNotImplicitlyPromotable] = IsNotImplicitlyPromotable::in_local(self, local);
654         qualifs
655     }
656
657     fn qualifs_in_value(&self, source: ValueSource<'_, 'tcx>) -> PerQualif<bool> {
658         let mut qualifs = PerQualif::default();
659         qualifs[HasMutInterior] = HasMutInterior::in_value(self, source);
660         qualifs[NeedsDrop] = NeedsDrop::in_value(self, source);
661         qualifs[IsNotPromotable] = IsNotPromotable::in_value(self, source);
662         qualifs[IsNotImplicitlyPromotable] = IsNotImplicitlyPromotable::in_value(self, source);
663         qualifs
664     }
665 }
666
667 /// Checks MIR for being admissible as a compile-time constant, using `ConstCx`
668 /// for value qualifications, and accumulates writes of
669 /// rvalue/call results to locals, in `local_qualif`.
670 /// It also records candidates for promotion in `promotion_candidates`,
671 /// both in functions and const/static items.
672 struct Checker<'a, 'tcx> {
673     cx: ConstCx<'a, 'tcx>,
674
675     span: Span,
676     def_id: DefId,
677     rpo: ReversePostorder<'a, 'tcx>,
678
679     temp_promotion_state: IndexVec<Local, TempState>,
680     promotion_candidates: Vec<Candidate>,
681     unchecked_promotion_candidates: Vec<Candidate>,
682
683     /// If `true`, do not emit errors to the user, merely collect them in `errors`.
684     suppress_errors: bool,
685     errors: Vec<(Span, String)>,
686 }
687
688 macro_rules! unleash_miri {
689     ($this:expr) => {{
690         if $this.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
691             if $this.mode.requires_const_checking() && !$this.suppress_errors {
692                 $this.tcx.sess.span_warn($this.span, "skipping const checks");
693             }
694             return;
695         }
696     }}
697 }
698
699 impl Deref for Checker<'a, 'tcx> {
700     type Target = ConstCx<'a, 'tcx>;
701
702     fn deref(&self) -> &Self::Target {
703         &self.cx
704     }
705 }
706
707 impl<'a, 'tcx> Checker<'a, 'tcx> {
708     fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>, mode: Mode) -> Self {
709         assert!(def_id.is_local());
710         let mut rpo = traversal::reverse_postorder(body);
711         let (temps, unchecked_promotion_candidates) =
712             promote_consts::collect_temps_and_candidates(tcx, body, &mut rpo);
713         rpo.reset();
714
715         let param_env = tcx.param_env(def_id);
716
717         let mut cx = ConstCx {
718             tcx,
719             param_env,
720             mode,
721             body,
722             per_local: PerQualif::new(BitSet::new_empty(body.local_decls.len())),
723         };
724
725         for (local, decl) in body.local_decls.iter_enumerated() {
726             if let LocalKind::Arg = body.local_kind(local) {
727                 let qualifs = cx.qualifs_in_any_value_of_ty(decl.ty);
728                 for (per_local, qualif) in &mut cx.per_local.as_mut().zip(qualifs).0 {
729                     if *qualif {
730                         per_local.insert(local);
731                     }
732                 }
733             }
734             if !temps[local].is_promotable() {
735                 cx.per_local[IsNotPromotable].insert(local);
736             }
737             if let LocalKind::Var = body.local_kind(local) {
738                 // Sanity check to prevent implicit and explicit promotion of
739                 // named locals
740                 assert!(cx.per_local[IsNotPromotable].contains(local));
741             }
742         }
743
744         Checker {
745             cx,
746             span: body.span,
747             def_id,
748             rpo,
749             temp_promotion_state: temps,
750             promotion_candidates: vec![],
751             unchecked_promotion_candidates,
752             errors: vec![],
753             suppress_errors: false,
754         }
755     }
756
757     // FIXME(eddyb) we could split the errors into meaningful
758     // categories, but enabling full miri would make that
759     // slightly pointless (even with feature-gating).
760     fn not_const(&mut self, op: impl NonConstOp) {
761         unleash_miri!(self);
762         if self.mode.requires_const_checking() && !self.suppress_errors {
763             self.record_error(op);
764             let mut err = struct_span_err!(
765                 self.tcx.sess,
766                 self.span,
767                 E0019,
768                 "{} contains unimplemented expression type",
769                 self.mode
770             );
771             if self.tcx.sess.teach(&err.get_code().unwrap()) {
772                 err.note("A function call isn't allowed in the const's initialization expression \
773                           because the expression's value must be known at compile-time.");
774                 err.note("Remember: you can't use a function call inside a const's initialization \
775                           expression! However, you can use it anywhere else.");
776             }
777             err.emit();
778         }
779     }
780
781     fn record_error(&mut self, op: impl NonConstOp) {
782         self.record_error_spanned(op, self.span);
783     }
784
785     fn record_error_spanned(&mut self, op: impl NonConstOp, span: Span) {
786         self.errors.push((span, format!("{:?}", op)));
787     }
788
789     /// Assigns an rvalue/call qualification to the given destination.
790     fn assign(&mut self, dest: &Place<'tcx>, source: ValueSource<'_, 'tcx>, location: Location) {
791         trace!("assign: {:?} <- {:?}", dest, source);
792
793         let mut qualifs = self.qualifs_in_value(source);
794
795         match source {
796             ValueSource::Rvalue(&Rvalue::Ref(_, kind, ref place)) => {
797                 // Getting `true` from `HasMutInterior::in_rvalue` means
798                 // the borrowed place is disallowed from being borrowed,
799                 // due to either a mutable borrow (with some exceptions),
800                 // or an shared borrow of a value with interior mutability.
801                 // Then `HasMutInterior` is replaced with `IsNotPromotable`,
802                 // to avoid duplicate errors (e.g. from reborrowing).
803                 if qualifs[HasMutInterior] {
804                     qualifs[HasMutInterior] = false;
805                     qualifs[IsNotPromotable] = true;
806
807                     debug!("suppress_errors: {}", self.suppress_errors);
808                     if self.mode.requires_const_checking() && !self.suppress_errors {
809                         if !self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
810                             self.record_error(ops::MutBorrow(kind));
811                             if let BorrowKind::Mut { .. } = kind {
812                                 let mut err = struct_span_err!(self.tcx.sess,  self.span, E0017,
813                                                                "references in {}s may only refer \
814                                                                 to immutable values", self.mode);
815                                 err.span_label(self.span, format!("{}s require immutable values",
816                                                                     self.mode));
817                                 if self.tcx.sess.teach(&err.get_code().unwrap()) {
818                                     err.note("References in statics and constants may only refer \
819                                               to immutable values.\n\n\
820                                               Statics are shared everywhere, and if they refer to \
821                                               mutable data one might violate memory safety since \
822                                               holding multiple mutable references to shared data \
823                                               is not allowed.\n\n\
824                                               If you really want global mutable state, try using \
825                                               static mut or a global UnsafeCell.");
826                                 }
827                                 err.emit();
828                             } else {
829                                 span_err!(self.tcx.sess, self.span, E0492,
830                                           "cannot borrow a constant which may contain \
831                                            interior mutability, create a static instead");
832                             }
833                         }
834                     }
835                 } else if let BorrowKind::Mut { .. } | BorrowKind::Shared = kind {
836                     // Don't promote BorrowKind::Shallow borrows, as they don't
837                     // reach codegen.
838                     // FIXME(eddyb) the two other kinds of borrow (`Shallow` and `Unique`)
839                     // aren't promoted here but *could* be promoted as part of a larger
840                     // value because `IsNotPromotable` isn't being set for them,
841                     // need to figure out what is the intended behavior.
842
843                     // We might have a candidate for promotion.
844                     let candidate = Candidate::Ref(location);
845                     // Start by traversing to the "base", with non-deref projections removed.
846                     let deref_proj =
847                         place.projection.iter().rev().find(|&elem| *elem == ProjectionElem::Deref);
848
849                     debug!(
850                         "qualify_consts: promotion candidate: place={:?} {:?}",
851                         place.base, deref_proj
852                     );
853                     // We can only promote interior borrows of promotable temps (non-temps
854                     // don't get promoted anyway).
855                     // (If we bailed out of the loop due to a `Deref` above, we will definitely
856                     // not enter the conditional here.)
857                     if let (PlaceBase::Local(local), None) = (&place.base, deref_proj) {
858                         if self.body.local_kind(*local) == LocalKind::Temp {
859                             debug!("qualify_consts: promotion candidate: local={:?}", local);
860                             // The borrowed place doesn't have `HasMutInterior`
861                             // (from `in_rvalue`), so we can safely ignore
862                             // `HasMutInterior` from the local's qualifications.
863                             // This allows borrowing fields which don't have
864                             // `HasMutInterior`, from a type that does, e.g.:
865                             // `let _: &'static _ = &(Cell::new(1), 2).1;`
866                             let mut local_qualifs = self.qualifs_in_local(*local);
867                             // Any qualifications, except HasMutInterior (see above), disqualify
868                             // from promotion.
869                             // This is, in particular, the "implicit promotion" version of
870                             // the check making sure that we don't run drop glue during const-eval.
871                             local_qualifs[HasMutInterior] = false;
872                             if !local_qualifs.0.iter().any(|&qualif| qualif) {
873                                 debug!("qualify_consts: promotion candidate: {:?}", candidate);
874                                 self.promotion_candidates.push(candidate);
875                             }
876                         }
877                     }
878                 }
879             },
880             ValueSource::Rvalue(&Rvalue::Repeat(ref operand, _)) => {
881                 let candidate = Candidate::Repeat(location);
882                 let not_promotable = IsNotImplicitlyPromotable::in_operand(self, operand) ||
883                                      IsNotPromotable::in_operand(self, operand);
884                 debug!("assign: self.def_id={:?} operand={:?}", self.def_id, operand);
885                 if !not_promotable && self.tcx.features().const_in_array_repeat_expressions {
886                     debug!("assign: candidate={:?}", candidate);
887                     self.promotion_candidates.push(candidate);
888                 }
889             },
890             _ => {},
891         }
892
893         let mut dest_projection = &dest.projection[..];
894         let index = loop {
895             match (&dest.base, dest_projection) {
896                 // We treat all locals equal in constants
897                 (&PlaceBase::Local(index), []) => break index,
898                 // projections are transparent for assignments
899                 // we qualify the entire destination at once, even if just a field would have
900                 // stricter qualification
901                 (base, [proj_base @ .., _]) => {
902                     // Catch more errors in the destination. `visit_place` also checks various
903                     // projection rules like union field access and raw pointer deref
904                     let context = PlaceContext::MutatingUse(MutatingUseContext::Store);
905                     self.visit_place_base(base, context, location);
906                     self.visit_projection(base, dest_projection, context, location);
907                     dest_projection = proj_base;
908                 },
909                 (&PlaceBase::Static(box Static {
910                     kind: StaticKind::Promoted(..),
911                     ..
912                 }), []) => bug!("promoteds don't exist yet during promotion"),
913                 (&PlaceBase::Static(box Static{ kind: _, .. }), []) => {
914                     // Catch more errors in the destination. `visit_place` also checks that we
915                     // do not try to access statics from constants or try to mutate statics
916                     let context = PlaceContext::MutatingUse(MutatingUseContext::Store);
917                     self.visit_place_base(&dest.base, context, location);
918                     return;
919                 }
920             }
921         };
922
923         let kind = self.body.local_kind(index);
924         debug!("store to {:?} {:?}", kind, index);
925
926         // Only handle promotable temps in non-const functions.
927         if self.mode == Mode::NonConstFn {
928             if kind != LocalKind::Temp ||
929                !self.temp_promotion_state[index].is_promotable() {
930                 return;
931             }
932         }
933
934         // this is overly restrictive, because even full assignments do not clear the qualif
935         // While we could special case full assignments, this would be inconsistent with
936         // aggregates where we overwrite all fields via assignments, which would not get
937         // that feature.
938         for (per_local, qualif) in &mut self.cx.per_local.as_mut().zip(qualifs).0 {
939             if *qualif {
940                 per_local.insert(index);
941             }
942         }
943
944         // Ensure the `IsNotPromotable` qualification is preserved.
945         // NOTE(eddyb) this is actually unnecessary right now, as
946         // we never replace the local's qualif, but we might in
947         // the future, and so it serves to catch changes that unset
948         // important bits (in which case, asserting `contains` could
949         // be replaced with calling `insert` to re-set the bit).
950         if kind == LocalKind::Temp {
951             if !self.temp_promotion_state[index].is_promotable() {
952                 assert!(self.cx.per_local[IsNotPromotable].contains(index));
953             }
954         }
955     }
956
957     /// Check a whole const, static initializer or const fn.
958     fn check_const(&mut self) -> (u8, &'tcx BitSet<Local>) {
959         use crate::transform::check_consts as new_checker;
960
961         debug!("const-checking {} {:?}", self.mode, self.def_id);
962
963         // FIXME: Also use the new validator when features that require it (e.g. `const_if`) are
964         // enabled.
965         let use_new_validator = self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you;
966         if use_new_validator {
967             debug!("Using dataflow-based const validator");
968         }
969
970         let item = new_checker::Item::new(self.tcx, self.def_id, self.body);
971         let mut_borrowed_locals = new_checker::validation::compute_indirectly_mutable_locals(&item);
972         let mut validator = new_checker::validation::Validator::new(&item, &mut_borrowed_locals);
973
974         validator.suppress_errors = !use_new_validator;
975         self.suppress_errors = use_new_validator;
976
977         let body = self.body;
978
979         let mut seen_blocks = BitSet::new_empty(body.basic_blocks().len());
980         let mut bb = START_BLOCK;
981         let mut has_controlflow_error = false;
982         loop {
983             seen_blocks.insert(bb.index());
984
985             self.visit_basic_block_data(bb, &body[bb]);
986             validator.visit_basic_block_data(bb, &body[bb]);
987
988             let target = match body[bb].terminator().kind {
989                 TerminatorKind::Goto { target } |
990                 TerminatorKind::FalseUnwind { real_target: target, .. } |
991                 TerminatorKind::Drop { target, .. } |
992                 TerminatorKind::DropAndReplace { target, .. } |
993                 TerminatorKind::Assert { target, .. } |
994                 TerminatorKind::Call { destination: Some((_, target)), .. } => {
995                     Some(target)
996                 }
997
998                 // Non-terminating calls cannot produce any value.
999                 TerminatorKind::Call { destination: None, .. } => {
1000                     break;
1001                 }
1002
1003                 TerminatorKind::SwitchInt {..} |
1004                 TerminatorKind::Resume |
1005                 TerminatorKind::Abort |
1006                 TerminatorKind::GeneratorDrop |
1007                 TerminatorKind::Yield { .. } |
1008                 TerminatorKind::Unreachable |
1009                 TerminatorKind::FalseEdges { .. } => None,
1010
1011                 TerminatorKind::Return => {
1012                     break;
1013                 }
1014             };
1015
1016             match target {
1017                 // No loops allowed.
1018                 Some(target) if !seen_blocks.contains(target.index()) => {
1019                     bb = target;
1020                 }
1021                 _ => {
1022                     has_controlflow_error = true;
1023                     self.not_const(ops::Loop);
1024                     validator.check_op(ops::Loop);
1025                     break;
1026                 }
1027             }
1028         }
1029
1030         // The new validation pass should agree with the old when running on simple const bodies
1031         // (e.g. no `if` or `loop`).
1032         if !use_new_validator {
1033             let mut new_errors = validator.take_errors();
1034
1035             // FIXME: each checker sometimes emits the same error with the same span twice in a row.
1036             self.errors.dedup();
1037             new_errors.dedup();
1038
1039             if self.errors != new_errors {
1040                 validator_mismatch(
1041                     self.tcx,
1042                     body,
1043                     std::mem::replace(&mut self.errors, vec![]),
1044                     new_errors,
1045                 );
1046             }
1047         }
1048
1049         // Collect all the temps we need to promote.
1050         let mut promoted_temps = BitSet::new_empty(self.temp_promotion_state.len());
1051
1052         // HACK(eddyb) don't try to validate promotion candidates if any
1053         // parts of the control-flow graph were skipped due to an error.
1054         let promotion_candidates = if has_controlflow_error {
1055             let unleash_miri = self
1056                 .tcx
1057                 .sess
1058                 .opts
1059                 .debugging_opts
1060                 .unleash_the_miri_inside_of_you;
1061             if !unleash_miri {
1062                 self.tcx.sess.delay_span_bug(
1063                     body.span,
1064                     "check_const: expected control-flow error(s)",
1065                 );
1066             }
1067             self.promotion_candidates.clone()
1068         } else {
1069             self.valid_promotion_candidates()
1070         };
1071         debug!("qualify_const: promotion_candidates={:?}", promotion_candidates);
1072         for candidate in promotion_candidates {
1073             match candidate {
1074                 Candidate::Repeat(Location { block: bb, statement_index: stmt_idx }) => {
1075                     if let StatementKind::Assign(box(_, Rvalue::Repeat(
1076                         Operand::Move(place),
1077                         _
1078                     ))) = &self.body[bb].statements[stmt_idx].kind {
1079                         if let Some(index) = place.as_local() {
1080                             promoted_temps.insert(index);
1081                         }
1082                     }
1083                 }
1084                 Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
1085                     if let StatementKind::Assign(
1086                         box(
1087                             _,
1088                             Rvalue::Ref(_, _, place)
1089                         )
1090                     ) = &self.body[bb].statements[stmt_idx].kind {
1091                         if let Some(index) = place.as_local() {
1092                             promoted_temps.insert(index);
1093                         }
1094                     }
1095                 }
1096                 Candidate::Argument { .. } => {}
1097             }
1098         }
1099
1100         let mut qualifs = self.qualifs_in_local(RETURN_PLACE);
1101
1102         // Account for errors in consts by using the
1103         // conservative type qualification instead.
1104         if qualifs[IsNotPromotable] {
1105             qualifs = self.qualifs_in_any_value_of_ty(body.return_ty());
1106         }
1107
1108         (qualifs.encode_to_bits(), self.tcx.arena.alloc(promoted_temps))
1109     }
1110
1111     /// Get the subset of `unchecked_promotion_candidates` that are eligible
1112     /// for promotion.
1113     // FIXME(eddyb) replace the old candidate gathering with this.
1114     fn valid_promotion_candidates(&self) -> Vec<Candidate> {
1115         // Sanity-check the promotion candidates.
1116         let candidates = promote_consts::validate_candidates(
1117             self.tcx,
1118             self.body,
1119             self.def_id,
1120             &self.temp_promotion_state,
1121             &self.per_local.0[HasMutInterior::IDX],
1122             &self.per_local.0[NeedsDrop::IDX],
1123             &self.unchecked_promotion_candidates,
1124         );
1125
1126         if candidates != self.promotion_candidates {
1127             let report = |msg, candidate| {
1128                 let span = match candidate {
1129                     Candidate::Ref(loc) |
1130                     Candidate::Repeat(loc) => self.body.source_info(loc).span,
1131                     Candidate::Argument { bb, .. } => {
1132                         self.body[bb].terminator().source_info.span
1133                     }
1134                 };
1135                 self.tcx.sess.span_err(span, &format!("{}: {:?}", msg, candidate));
1136             };
1137
1138             for &c in &self.promotion_candidates {
1139                 if !candidates.contains(&c) {
1140                     report("invalidated old candidate", c);
1141                 }
1142             }
1143
1144             for &c in &candidates {
1145                 if !self.promotion_candidates.contains(&c) {
1146                     report("extra new candidate", c);
1147                 }
1148             }
1149
1150             bug!("promotion candidate validation mismatches (see above)");
1151         }
1152
1153         candidates
1154     }
1155 }
1156
1157 impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
1158     fn visit_place_base(
1159         &mut self,
1160         place_base: &PlaceBase<'tcx>,
1161         context: PlaceContext,
1162         location: Location,
1163     ) {
1164         self.super_place_base(place_base, context, location);
1165         match place_base {
1166             PlaceBase::Local(_) => {}
1167             PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_, _), .. }) => {
1168                 unreachable!()
1169             }
1170             PlaceBase::Static(box Static{ kind: StaticKind::Static, def_id, .. }) => {
1171                 if self.tcx
1172                         .get_attrs(*def_id)
1173                         .iter()
1174                         .any(|attr| attr.check_name(sym::thread_local)) {
1175                     if self.mode.requires_const_checking() && !self.suppress_errors {
1176                         self.record_error(ops::ThreadLocalAccess);
1177                         span_err!(self.tcx.sess, self.span, E0625,
1178                                     "thread-local statics cannot be \
1179                                     accessed at compile-time");
1180                     }
1181                     return;
1182                 }
1183
1184                 // Only allow statics (not consts) to refer to other statics.
1185                 if self.mode == Mode::Static || self.mode == Mode::StaticMut {
1186                     if self.mode == Mode::Static
1187                         && context.is_mutating_use()
1188                         && !self.suppress_errors
1189                     {
1190                         // this is not strictly necessary as miri will also bail out
1191                         // For interior mutability we can't really catch this statically as that
1192                         // goes through raw pointers and intermediate temporaries, so miri has
1193                         // to catch this anyway
1194                         self.tcx.sess.span_err(
1195                             self.span,
1196                             "cannot mutate statics in the initializer of another static",
1197                         );
1198                     }
1199                     return;
1200                 }
1201                 unleash_miri!(self);
1202
1203                 if self.mode.requires_const_checking() && !self.suppress_errors {
1204                     self.record_error(ops::StaticAccess);
1205                     let mut err = struct_span_err!(self.tcx.sess, self.span, E0013,
1206                                                     "{}s cannot refer to statics, use \
1207                                                     a constant instead", self.mode);
1208                     if self.tcx.sess.teach(&err.get_code().unwrap()) {
1209                         err.note(
1210                             "Static and const variables can refer to other const variables. \
1211                                 But a const variable cannot refer to a static variable."
1212                         );
1213                         err.help(
1214                             "To fix this, the value can be extracted as a const and then used."
1215                         );
1216                     }
1217                     err.emit()
1218                 }
1219             }
1220         }
1221     }
1222
1223     fn visit_projection_elem(
1224         &mut self,
1225         place_base: &PlaceBase<'tcx>,
1226         proj_base: &[PlaceElem<'tcx>],
1227         elem: &PlaceElem<'tcx>,
1228         context: PlaceContext,
1229         location: Location,
1230     ) {
1231         debug!(
1232             "visit_projection_elem: place_base={:?} proj_base={:?} elem={:?} \
1233             context={:?} location={:?}",
1234             place_base,
1235             proj_base,
1236             elem,
1237             context,
1238             location,
1239         );
1240
1241         self.super_projection_elem(place_base, proj_base, elem, context, location);
1242
1243         match elem {
1244             ProjectionElem::Deref => {
1245                 if context.is_mutating_use() {
1246                     // `not_const` errors out in const contexts
1247                     self.not_const(ops::MutDeref)
1248                 }
1249                 let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1250                 match self.mode {
1251                     Mode::NonConstFn => {}
1252                     _ if self.suppress_errors => {}
1253                     _ => {
1254                         if let ty::RawPtr(_) = base_ty.kind {
1255                             if !self.tcx.features().const_raw_ptr_deref {
1256                                 self.record_error(ops::RawPtrDeref);
1257                                 emit_feature_err(
1258                                     &self.tcx.sess.parse_sess, sym::const_raw_ptr_deref,
1259                                     self.span, GateIssue::Language,
1260                                     &format!(
1261                                         "dereferencing raw pointers in {}s is unstable",
1262                                         self.mode,
1263                                     ),
1264                                 );
1265                             }
1266                         }
1267                     }
1268                 }
1269             }
1270
1271             ProjectionElem::ConstantIndex {..} |
1272             ProjectionElem::Subslice {..} |
1273             ProjectionElem::Field(..) |
1274             ProjectionElem::Index(_) => {
1275                 let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1276                 if let Some(def) = base_ty.ty_adt_def() {
1277                     if def.is_union() {
1278                         match self.mode {
1279                             Mode::ConstFn => {
1280                                 if !self.tcx.features().const_fn_union
1281                                     && !self.suppress_errors
1282                                 {
1283                                     self.record_error(ops::UnionAccess);
1284                                     emit_feature_err(
1285                                         &self.tcx.sess.parse_sess, sym::const_fn_union,
1286                                         self.span, GateIssue::Language,
1287                                         "unions in const fn are unstable",
1288                                     );
1289                                 }
1290                             },
1291
1292                             | Mode::NonConstFn
1293                             | Mode::Static
1294                             | Mode::StaticMut
1295                             | Mode::Const
1296                             => {},
1297                         }
1298                     }
1299                 }
1300             }
1301
1302             ProjectionElem::Downcast(..) => {
1303                 self.not_const(ops::Downcast)
1304             }
1305         }
1306     }
1307
1308     fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
1309         debug!("visit_operand: operand={:?} location={:?}", operand, location);
1310         self.super_operand(operand, location);
1311
1312         match *operand {
1313             Operand::Move(ref place) => {
1314                 // Mark the consumed locals to indicate later drops are noops.
1315                 if let Some(local) = place.as_local() {
1316                     self.cx.per_local[NeedsDrop].remove(local);
1317                 }
1318             }
1319             Operand::Copy(_) |
1320             Operand::Constant(_) => {}
1321         }
1322     }
1323
1324     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
1325         debug!("visit_rvalue: rvalue={:?} location={:?}", rvalue, location);
1326
1327         // Check nested operands and places.
1328         if let Rvalue::Ref(_, kind, ref place) = *rvalue {
1329             // Special-case reborrows.
1330             let mut reborrow_place = None;
1331             if let &[ref proj_base @ .., elem] = place.projection.as_ref() {
1332                 if elem == ProjectionElem::Deref {
1333                     let base_ty = Place::ty_from(&place.base, proj_base, self.body, self.tcx).ty;
1334                     if let ty::Ref(..) = base_ty.kind {
1335                         reborrow_place = Some(proj_base);
1336                     }
1337                 }
1338             }
1339
1340             if let Some(proj) = reborrow_place {
1341                 let ctx = match kind {
1342                     BorrowKind::Shared => PlaceContext::NonMutatingUse(
1343                         NonMutatingUseContext::SharedBorrow,
1344                     ),
1345                     BorrowKind::Shallow => PlaceContext::NonMutatingUse(
1346                         NonMutatingUseContext::ShallowBorrow,
1347                     ),
1348                     BorrowKind::Unique => PlaceContext::NonMutatingUse(
1349                         NonMutatingUseContext::UniqueBorrow,
1350                     ),
1351                     BorrowKind::Mut { .. } => PlaceContext::MutatingUse(
1352                         MutatingUseContext::Borrow,
1353                     ),
1354                 };
1355                 self.visit_place_base(&place.base, ctx, location);
1356                 self.visit_projection(&place.base, proj, ctx, location);
1357             } else {
1358                 self.super_rvalue(rvalue, location);
1359             }
1360         } else {
1361             self.super_rvalue(rvalue, location);
1362         }
1363
1364         match *rvalue {
1365             Rvalue::Use(_) |
1366             Rvalue::Repeat(..) |
1367             Rvalue::UnaryOp(UnOp::Neg, _) |
1368             Rvalue::UnaryOp(UnOp::Not, _) |
1369             Rvalue::NullaryOp(NullOp::SizeOf, _) |
1370             Rvalue::CheckedBinaryOp(..) |
1371             Rvalue::Cast(CastKind::Pointer(_), ..) |
1372             Rvalue::Discriminant(..) |
1373             Rvalue::Len(_) |
1374             Rvalue::Ref(..) |
1375             Rvalue::Aggregate(..) => {}
1376
1377             Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
1378                 let operand_ty = operand.ty(self.body, self.tcx);
1379                 let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
1380                 let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
1381                 match (cast_in, cast_out) {
1382                     (CastTy::Ptr(_), CastTy::Int(_)) |
1383                     (CastTy::FnPtr, CastTy::Int(_)) if self.mode != Mode::NonConstFn => {
1384                         unleash_miri!(self);
1385                         if !self.tcx.features().const_raw_ptr_to_usize_cast
1386                             && !self.suppress_errors
1387                         {
1388                             // in const fn and constants require the feature gate
1389                             // FIXME: make it unsafe inside const fn and constants
1390                             self.record_error(ops::RawPtrToIntCast);
1391                             emit_feature_err(
1392                                 &self.tcx.sess.parse_sess, sym::const_raw_ptr_to_usize_cast,
1393                                 self.span, GateIssue::Language,
1394                                 &format!(
1395                                     "casting pointers to integers in {}s is unstable",
1396                                     self.mode,
1397                                 ),
1398                             );
1399                         }
1400                     }
1401                     _ => {}
1402                 }
1403             }
1404
1405             Rvalue::BinaryOp(op, ref lhs, _) => {
1406                 if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.body, self.tcx).kind {
1407                     assert!(op == BinOp::Eq || op == BinOp::Ne ||
1408                             op == BinOp::Le || op == BinOp::Lt ||
1409                             op == BinOp::Ge || op == BinOp::Gt ||
1410                             op == BinOp::Offset);
1411
1412                     unleash_miri!(self);
1413                     if self.mode.requires_const_checking() &&
1414                         !self.tcx.features().const_compare_raw_pointers &&
1415                         !self.suppress_errors
1416                     {
1417                         self.record_error(ops::RawPtrComparison);
1418                         // require the feature gate inside constants and const fn
1419                         // FIXME: make it unsafe to use these operations
1420                         emit_feature_err(
1421                             &self.tcx.sess.parse_sess,
1422                             sym::const_compare_raw_pointers,
1423                             self.span,
1424                             GateIssue::Language,
1425                             &format!("comparing raw pointers inside {}", self.mode),
1426                         );
1427                     }
1428                 }
1429             }
1430
1431             Rvalue::NullaryOp(NullOp::Box, _) => {
1432                 unleash_miri!(self);
1433                 if self.mode.requires_const_checking() && !self.suppress_errors {
1434                     self.record_error(ops::HeapAllocation);
1435                     let mut err = struct_span_err!(self.tcx.sess, self.span, E0010,
1436                                                    "allocations are not allowed in {}s", self.mode);
1437                     err.span_label(self.span, format!("allocation not allowed in {}s", self.mode));
1438                     if self.tcx.sess.teach(&err.get_code().unwrap()) {
1439                         err.note(
1440                             "The value of statics and constants must be known at compile time, \
1441                              and they live for the entire lifetime of a program. Creating a boxed \
1442                              value allocates memory on the heap at runtime, and therefore cannot \
1443                              be done at compile time."
1444                         );
1445                     }
1446                     err.emit();
1447                 }
1448             }
1449         }
1450     }
1451
1452     fn visit_terminator_kind(&mut self,
1453                              kind: &TerminatorKind<'tcx>,
1454                              location: Location) {
1455         debug!("visit_terminator_kind: kind={:?} location={:?}", kind, location);
1456         if let TerminatorKind::Call { ref func, ref args, ref destination, .. } = *kind {
1457             if let Some((ref dest, _)) = *destination {
1458                 self.assign(dest, ValueSource::Call {
1459                     callee: func,
1460                     args,
1461                     return_ty: dest.ty(self.body, self.tcx).ty,
1462                 }, location);
1463             }
1464
1465             let fn_ty = func.ty(self.body, self.tcx);
1466             let mut callee_def_id = None;
1467             let mut is_shuffle = false;
1468             match fn_ty.kind {
1469                 ty::FnDef(def_id, _) => {
1470                     callee_def_id = Some(def_id);
1471                     match self.tcx.fn_sig(def_id).abi() {
1472                         Abi::RustIntrinsic |
1473                         Abi::PlatformIntrinsic => {
1474                             assert!(!self.tcx.is_const_fn(def_id));
1475                             match &self.tcx.item_name(def_id).as_str()[..] {
1476                                 // special intrinsic that can be called diretly without an intrinsic
1477                                 // feature gate needs a language feature gate
1478                                 "transmute" => {
1479                                     if self.mode.requires_const_checking()
1480                                         && !self.suppress_errors
1481                                     {
1482                                         // const eval transmute calls only with the feature gate
1483                                         if !self.tcx.features().const_transmute {
1484                                             self.record_error(ops::Transmute);
1485                                             emit_feature_err(
1486                                                 &self.tcx.sess.parse_sess, sym::const_transmute,
1487                                                 self.span, GateIssue::Language,
1488                                                 &format!("The use of std::mem::transmute() \
1489                                                 is gated in {}s", self.mode));
1490                                         }
1491                                     }
1492                                 }
1493
1494                                 name if name.starts_with("simd_shuffle") => {
1495                                     is_shuffle = true;
1496                                 }
1497
1498                                 // no need to check feature gates, intrinsics are only callable
1499                                 // from the libstd or with forever unstable feature gates
1500                                 _ => {}
1501                             }
1502                         }
1503                         _ => {
1504                             // In normal functions no calls are feature-gated.
1505                             if self.mode.requires_const_checking() {
1506                                 let unleash_miri = self
1507                                     .tcx
1508                                     .sess
1509                                     .opts
1510                                     .debugging_opts
1511                                     .unleash_the_miri_inside_of_you;
1512                                 if self.tcx.is_const_fn(def_id)
1513                                     || unleash_miri
1514                                     || self.suppress_errors
1515                                 {
1516                                     // stable const fns or unstable const fns
1517                                     // with their feature gate active
1518                                     // FIXME(eddyb) move stability checks from `is_const_fn` here.
1519                                 } else if self.is_const_panic_fn(def_id) {
1520                                     // Check the const_panic feature gate.
1521                                     // FIXME: cannot allow this inside `allow_internal_unstable`
1522                                     // because that would make `panic!` insta stable in constants,
1523                                     // since the macro is marked with the attribute.
1524                                     if !self.tcx.features().const_panic {
1525                                         // Don't allow panics in constants without the feature gate.
1526                                         self.record_error(ops::Panic);
1527                                         emit_feature_err(
1528                                             &self.tcx.sess.parse_sess,
1529                                             sym::const_panic,
1530                                             self.span,
1531                                             GateIssue::Language,
1532                                             &format!("panicking in {}s is unstable", self.mode),
1533                                         );
1534                                     }
1535                                 } else if let Some(feature)
1536                                               = self.tcx.is_unstable_const_fn(def_id) {
1537                                     // Check `#[unstable]` const fns or `#[rustc_const_unstable]`
1538                                     // functions without the feature gate active in this crate in
1539                                     // order to report a better error message than the one below.
1540                                     if !self.span.allows_unstable(feature) {
1541                                         self.record_error(ops::FnCallUnstable(def_id, feature));
1542                                         let mut err = self.tcx.sess.struct_span_err(self.span,
1543                                             &format!("`{}` is not yet stable as a const fn",
1544                                                     self.tcx.def_path_str(def_id)));
1545                                         if nightly_options::is_nightly_build() {
1546                                             help!(&mut err,
1547                                                   "add `#![feature({})]` to the \
1548                                                    crate attributes to enable",
1549                                                   feature);
1550                                         }
1551                                         err.emit();
1552                                     }
1553                                 } else {
1554                                     self.record_error(ops::FnCallNonConst(def_id));
1555                                     let mut err = struct_span_err!(
1556                                         self.tcx.sess,
1557                                         self.span,
1558                                         E0015,
1559                                         "calls in {}s are limited to constant functions, \
1560                                          tuple structs and tuple variants",
1561                                         self.mode,
1562                                     );
1563                                     err.emit();
1564                                 }
1565                             }
1566                         }
1567                     }
1568                 }
1569                 ty::FnPtr(_) => {
1570                     unleash_miri!(self);
1571                     if self.mode.requires_const_checking() && !self.suppress_errors {
1572                         self.record_error(ops::FnCallIndirect);
1573                         let mut err = self.tcx.sess.struct_span_err(
1574                             self.span,
1575                             "function pointers are not allowed in const fn"
1576                         );
1577                         err.emit();
1578                     }
1579                 }
1580                 _ => {
1581                     self.not_const(ops::FnCallOther);
1582                 }
1583             }
1584
1585             // No need to do anything in constants and statics, as everything is "constant" anyway
1586             // so promotion would be useless.
1587             if self.mode != Mode::Static && self.mode != Mode::Const {
1588                 let constant_args = callee_def_id.and_then(|id| {
1589                     args_required_const(self.tcx, id)
1590                 }).unwrap_or_default();
1591                 for (i, arg) in args.iter().enumerate() {
1592                     if !(is_shuffle && i == 2 || constant_args.contains(&i)) {
1593                         continue;
1594                     }
1595
1596                     let candidate = Candidate::Argument { bb: location.block, index: i };
1597                     // Since the argument is required to be constant,
1598                     // we care about constness, not promotability.
1599                     // If we checked for promotability, we'd miss out on
1600                     // the results of function calls (which are never promoted
1601                     // in runtime code).
1602                     // This is not a problem, because the argument explicitly
1603                     // requests constness, in contrast to regular promotion
1604                     // which happens even without the user requesting it.
1605                     // We can error out with a hard error if the argument is not
1606                     // constant here.
1607                     if !IsNotPromotable::in_operand(self, arg) {
1608                         debug!("visit_terminator_kind: candidate={:?}", candidate);
1609                         self.promotion_candidates.push(candidate);
1610                     } else {
1611                         if is_shuffle {
1612                             span_err!(self.tcx.sess, self.span, E0526,
1613                                       "shuffle indices are not constant");
1614                         } else {
1615                             self.tcx.sess.span_err(self.span,
1616                                 &format!("argument {} is required to be a constant",
1617                                          i + 1));
1618                         }
1619                     }
1620                 }
1621             }
1622
1623             // Check callee and argument operands.
1624             self.visit_operand(func, location);
1625             for arg in args {
1626                 self.visit_operand(arg, location);
1627             }
1628         } else if let TerminatorKind::Drop {
1629             location: ref place, ..
1630         } | TerminatorKind::DropAndReplace {
1631             location: ref place, ..
1632         } = *kind {
1633             match *kind {
1634                 TerminatorKind::DropAndReplace { .. } => {}
1635                 _ => self.super_terminator_kind(kind, location),
1636             }
1637
1638             // Deny *any* live drops anywhere other than functions.
1639             if self.mode.requires_const_checking() && !self.suppress_errors {
1640                 unleash_miri!(self);
1641                 // HACK(eddyb): emulate a bit of dataflow analysis,
1642                 // conservatively, that drop elaboration will do.
1643                 let needs_drop = if let Some(local) = place.as_local() {
1644                     if NeedsDrop::in_local(self, local) {
1645                         Some(self.body.local_decls[local].source_info.span)
1646                     } else {
1647                         None
1648                     }
1649                 } else {
1650                     Some(self.span)
1651                 };
1652
1653                 if let Some(span) = needs_drop {
1654                     // Double-check the type being dropped, to minimize false positives.
1655                     let ty = place.ty(self.body, self.tcx).ty;
1656                     if ty.needs_drop(self.tcx, self.param_env) {
1657                         self.record_error_spanned(ops::LiveDrop, span);
1658                         struct_span_err!(self.tcx.sess, span, E0493,
1659                                          "destructors cannot be evaluated at compile-time")
1660                             .span_label(span, format!("{}s cannot evaluate destructors",
1661                                                       self.mode))
1662                             .emit();
1663                     }
1664                 }
1665             }
1666
1667             match *kind {
1668                 TerminatorKind::DropAndReplace { ref value, .. } => {
1669                     self.assign(place, ValueSource::DropAndReplace(value), location);
1670                     self.visit_operand(value, location);
1671                 }
1672                 _ => {}
1673             }
1674         } else {
1675             // Qualify any operands inside other terminators.
1676             self.super_terminator_kind(kind, location);
1677         }
1678     }
1679
1680     fn visit_assign(&mut self,
1681                     dest: &Place<'tcx>,
1682                     rvalue: &Rvalue<'tcx>,
1683                     location: Location) {
1684         debug!("visit_assign: dest={:?} rvalue={:?} location={:?}", dest, rvalue, location);
1685         self.assign(dest, ValueSource::Rvalue(rvalue), location);
1686
1687         self.visit_rvalue(rvalue, location);
1688     }
1689
1690     fn visit_source_info(&mut self, source_info: &SourceInfo) {
1691         debug!("visit_source_info: source_info={:?}", source_info);
1692         self.span = source_info.span;
1693     }
1694
1695     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
1696         debug!("visit_statement: statement={:?} location={:?}", statement, location);
1697         match statement.kind {
1698             StatementKind::Assign(..) => {
1699                 self.super_statement(statement, location);
1700             }
1701             StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {
1702                 self.not_const(ops::IfOrMatch);
1703             }
1704             // FIXME(eddyb) should these really do nothing?
1705             StatementKind::FakeRead(..) |
1706             StatementKind::SetDiscriminant { .. } |
1707             StatementKind::StorageLive(_) |
1708             StatementKind::StorageDead(_) |
1709             StatementKind::InlineAsm {..} |
1710             StatementKind::Retag { .. } |
1711             StatementKind::AscribeUserType(..) |
1712             StatementKind::Nop => {}
1713         }
1714     }
1715 }
1716
1717 pub fn provide(providers: &mut Providers<'_>) {
1718     *providers = Providers {
1719         mir_const_qualif,
1720         ..*providers
1721     };
1722 }
1723
1724 // FIXME(eddyb) this is only left around for the validation logic
1725 // in `promote_consts`, see the comment in `validate_operand`.
1726 pub(super) const QUALIF_ERROR_BIT: u8 = 1 << IsNotPromotable::IDX;
1727
1728 fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> (u8, &BitSet<Local>) {
1729     // N.B., this `borrow()` is guaranteed to be valid (i.e., the value
1730     // cannot yet be stolen), because `mir_validated()`, which steals
1731     // from `mir_const(), forces this query to execute before
1732     // performing the steal.
1733     let body = &tcx.mir_const(def_id).borrow();
1734
1735     if body.return_ty().references_error() {
1736         tcx.sess.delay_span_bug(body.span, "mir_const_qualif: MIR had errors");
1737         return (QUALIF_ERROR_BIT, tcx.arena.alloc(BitSet::new_empty(0)));
1738     }
1739
1740     Checker::new(tcx, def_id, body, Mode::Const).check_const()
1741 }
1742
1743 pub struct QualifyAndPromoteConstants<'tcx> {
1744     pub promoted: Cell<IndexVec<Promoted, Body<'tcx>>>,
1745 }
1746
1747 impl<'tcx> Default for QualifyAndPromoteConstants<'tcx> {
1748     fn default() -> Self {
1749         QualifyAndPromoteConstants {
1750             promoted: Cell::new(IndexVec::new()),
1751         }
1752     }
1753 }
1754
1755 impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1756     fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
1757         // There's not really any point in promoting errorful MIR.
1758         if body.return_ty().references_error() {
1759             tcx.sess.delay_span_bug(body.span, "QualifyAndPromoteConstants: MIR had errors");
1760             return;
1761         }
1762
1763         if src.promoted.is_some() {
1764             return;
1765         }
1766
1767         let def_id = src.def_id();
1768         let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
1769
1770         let mode = determine_mode(tcx, hir_id, def_id);
1771
1772         debug!("run_pass: mode={:?}", mode);
1773         if let Mode::NonConstFn | Mode::ConstFn = mode {
1774             // This is ugly because Checker holds onto mir,
1775             // which can't be mutated until its scope ends.
1776             let (temps, candidates) = {
1777                 let mut checker = Checker::new(tcx, def_id, body, mode);
1778                 if let Mode::ConstFn = mode {
1779                     let use_min_const_fn_checks =
1780                         !tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you &&
1781                         tcx.is_min_const_fn(def_id);
1782                     if use_min_const_fn_checks {
1783                         // Enforce `min_const_fn` for stable `const fn`s.
1784                         use super::qualify_min_const_fn::is_min_const_fn;
1785                         if let Err((span, err)) = is_min_const_fn(tcx, def_id, body) {
1786                             error_min_const_fn_violation(tcx, span, err);
1787                             return;
1788                         }
1789
1790                         // `check_const` should not produce any errors, but better safe than sorry
1791                         // FIXME(#53819)
1792                         // NOTE(eddyb) `check_const` is actually needed for promotion inside
1793                         // `min_const_fn` functions.
1794                     }
1795
1796                     // Enforce a constant-like CFG for `const fn`.
1797                     checker.check_const();
1798                 } else {
1799                     while let Some((bb, data)) = checker.rpo.next() {
1800                         checker.visit_basic_block_data(bb, data);
1801                     }
1802                 }
1803
1804                 let promotion_candidates = checker.valid_promotion_candidates();
1805                 (checker.temp_promotion_state, promotion_candidates)
1806             };
1807
1808             // Do the actual promotion, now that we know what's viable.
1809             self.promoted.set(
1810                 promote_consts::promote_candidates(def_id, body, tcx, temps, candidates)
1811             );
1812         } else {
1813             check_short_circuiting_in_const_local(tcx, body, mode);
1814
1815             let promoted_temps = match mode {
1816                 Mode::Const => tcx.mir_const_qualif(def_id).1,
1817                 _ => Checker::new(tcx, def_id, body, mode).check_const().1,
1818             };
1819             remove_drop_and_storage_dead_on_promoted_locals(body, promoted_temps);
1820         }
1821
1822         if mode == Mode::Static && !tcx.has_attr(def_id, sym::thread_local) {
1823             // `static`s (not `static mut`s) which are not `#[thread_local]` must be `Sync`.
1824             check_static_is_sync(tcx, body, hir_id);
1825         }
1826     }
1827 }
1828
1829 fn determine_mode(tcx: TyCtxt<'_>, hir_id: HirId, def_id: DefId) -> Mode {
1830     match tcx.hir().body_owner_kind(hir_id) {
1831         hir::BodyOwnerKind::Closure => Mode::NonConstFn,
1832         hir::BodyOwnerKind::Fn if tcx.is_const_fn(def_id) => Mode::ConstFn,
1833         hir::BodyOwnerKind::Fn => Mode::NonConstFn,
1834         hir::BodyOwnerKind::Const => Mode::Const,
1835         hir::BodyOwnerKind::Static(hir::MutImmutable) => Mode::Static,
1836         hir::BodyOwnerKind::Static(hir::MutMutable) => Mode::StaticMut,
1837     }
1838 }
1839
1840 fn error_min_const_fn_violation(tcx: TyCtxt<'_>, span: Span, msg: Cow<'_, str>) {
1841     struct_span_err!(tcx.sess, span, E0723, "{}", msg)
1842         .note("for more information, see issue https://github.com/rust-lang/rust/issues/57563")
1843         .help("add `#![feature(const_fn)]` to the crate attributes to enable")
1844         .emit();
1845 }
1846
1847 fn check_short_circuiting_in_const_local(tcx: TyCtxt<'_>, body: &mut Body<'tcx>, mode: Mode) {
1848     if body.control_flow_destroyed.is_empty() {
1849         return;
1850     }
1851
1852     let mut locals = body.vars_iter();
1853     if let Some(local) = locals.next() {
1854         let span = body.local_decls[local].source_info.span;
1855         let mut error = tcx.sess.struct_span_err(
1856             span,
1857             &format!(
1858                 "new features like let bindings are not permitted in {}s \
1859                 which also use short circuiting operators",
1860                 mode,
1861             ),
1862         );
1863         for (span, kind) in body.control_flow_destroyed.iter() {
1864             error.span_note(
1865                 *span,
1866                 &format!("use of {} here does not actually short circuit due to \
1867                 the const evaluator presently not being able to do control flow. \
1868                 See https://github.com/rust-lang/rust/issues/49146 for more \
1869                 information.", kind),
1870             );
1871         }
1872         for local in locals {
1873             let span = body.local_decls[local].source_info.span;
1874             error.span_note(span, "more locals defined here");
1875         }
1876         error.emit();
1877     }
1878 }
1879
1880 /// In `const` and `static` everything without `StorageDead`
1881 /// is `'static`, we don't have to create promoted MIR fragments,
1882 /// just remove `Drop` and `StorageDead` on "promoted" locals.
1883 fn remove_drop_and_storage_dead_on_promoted_locals(
1884     body: &mut Body<'tcx>,
1885     promoted_temps: &BitSet<Local>,
1886 ) {
1887     debug!("run_pass: promoted_temps={:?}", promoted_temps);
1888
1889     for block in body.basic_blocks_mut() {
1890         block.statements.retain(|statement| {
1891             match statement.kind {
1892                 StatementKind::StorageDead(index) => !promoted_temps.contains(index),
1893                 _ => true
1894             }
1895         });
1896         let terminator = block.terminator_mut();
1897         match &terminator.kind {
1898             TerminatorKind::Drop {
1899                 location,
1900                 target,
1901                 ..
1902             } => {
1903                 if let Some(index) = location.as_local() {
1904                     if promoted_temps.contains(index) {
1905                         terminator.kind = TerminatorKind::Goto { target: *target };
1906                     }
1907                 }
1908             }
1909             _ => {}
1910         }
1911     }
1912 }
1913
1914 fn check_static_is_sync(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, hir_id: HirId) {
1915     let ty = body.return_ty();
1916     tcx.infer_ctxt().enter(|infcx| {
1917         let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
1918         let mut fulfillment_cx = traits::FulfillmentContext::new();
1919         let sync_def_id = tcx.require_lang_item(lang_items::SyncTraitLangItem, Some(body.span));
1920         fulfillment_cx.register_bound(&infcx, ty::ParamEnv::empty(), ty, sync_def_id, cause);
1921         if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
1922             infcx.report_fulfillment_errors(&err, None, false);
1923         }
1924     });
1925 }
1926
1927 fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<FxHashSet<usize>> {
1928     let attrs = tcx.get_attrs(def_id);
1929     let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?;
1930     let mut ret = FxHashSet::default();
1931     for meta in attr.meta_item_list()? {
1932         match meta.literal()?.kind {
1933             LitKind::Int(a, _) => { ret.insert(a as usize); }
1934             _ => return None,
1935         }
1936     }
1937     Some(ret)
1938 }
1939
1940 fn validator_mismatch(
1941     tcx: TyCtxt<'tcx>,
1942     body: &Body<'tcx>,
1943     mut old_errors: Vec<(Span, String)>,
1944     mut new_errors: Vec<(Span, String)>,
1945 ) {
1946     error!("old validator: {:?}", old_errors);
1947     error!("new validator: {:?}", new_errors);
1948
1949     // ICE on nightly if the validators do not emit exactly the same errors.
1950     // Users can supress this panic with an unstable compiler flag (hopefully after
1951     // filing an issue).
1952     let opts = &tcx.sess.opts;
1953     let strict_validation_enabled = opts.unstable_features.is_nightly_build()
1954         && !opts.debugging_opts.suppress_const_validation_back_compat_ice;
1955
1956     if !strict_validation_enabled {
1957         return;
1958     }
1959
1960     // If this difference would cause a regression from the old to the new or vice versa, trigger
1961     // the ICE.
1962     if old_errors.is_empty() || new_errors.is_empty() {
1963         span_bug!(body.span, "{}", VALIDATOR_MISMATCH_ERR);
1964     }
1965
1966     // HACK: Borrows that would allow mutation are forbidden in const contexts, but they cause the
1967     // new validator to be more conservative about when a dropped local has been moved out of.
1968     //
1969     // Supress the mismatch ICE in cases where the validators disagree only on the number of
1970     // `LiveDrop` errors and both observe the same sequence of `MutBorrow`s.
1971
1972     let is_live_drop = |(_, s): &mut (_, String)| s.starts_with("LiveDrop");
1973     let is_mut_borrow = |(_, s): &&(_, String)| s.starts_with("MutBorrow");
1974
1975     let old_live_drops: Vec<_> = old_errors.drain_filter(is_live_drop).collect();
1976     let new_live_drops: Vec<_> = new_errors.drain_filter(is_live_drop).collect();
1977
1978     let only_live_drops_differ = old_live_drops != new_live_drops && old_errors == new_errors;
1979
1980     let old_mut_borrows = old_errors.iter().filter(is_mut_borrow);
1981     let new_mut_borrows = new_errors.iter().filter(is_mut_borrow);
1982
1983     let at_least_one_mut_borrow = old_mut_borrows.clone().next().is_some();
1984
1985     if only_live_drops_differ && at_least_one_mut_borrow && old_mut_borrows.eq(new_mut_borrows) {
1986         return;
1987     }
1988
1989     span_bug!(body.span, "{}", VALIDATOR_MISMATCH_ERR);
1990 }
1991
1992 const VALIDATOR_MISMATCH_ERR: &str =
1993     r"Disagreement between legacy and dataflow-based const validators.
1994     After filing an issue, use `-Zsuppress-const-validation-back-compat-ice` to compile your code.";