]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/const_prop.rs
Merge commit '61667dedf55e3e5aa584f7ae2bd0471336b92ce9' into sync_cg_clif-2021-09-19
[rust.git] / compiler / rustc_mir_transform / src / const_prop.rs
1 //! Propagates constants for early reporting of statically known
2 //! assertion failures
3
4 use std::cell::Cell;
5
6 use rustc_ast::Mutability;
7 use rustc_data_structures::fx::FxHashSet;
8 use rustc_hir::def::DefKind;
9 use rustc_hir::HirId;
10 use rustc_index::bit_set::BitSet;
11 use rustc_index::vec::IndexVec;
12 use rustc_middle::mir::visit::{
13     MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
14 };
15 use rustc_middle::mir::{
16     AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind,
17     Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
18     StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
19 };
20 use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
21 use rustc_middle::ty::subst::{InternalSubsts, Subst};
22 use rustc_middle::ty::{
23     self, ConstInt, ConstKind, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, TypeFoldable,
24 };
25 use rustc_session::lint;
26 use rustc_span::{def_id::DefId, Span};
27 use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
28 use rustc_target::spec::abi::Abi;
29 use rustc_trait_selection::traits;
30
31 use crate::MirPass;
32 use rustc_const_eval::const_eval::ConstEvalErr;
33 use rustc_const_eval::interpret::{
34     self, compile_time_machine, AllocId, Allocation, ConstValue, CtfeValidationMode, Frame, ImmTy,
35     Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, MemoryKind, OpTy,
36     Operand as InterpOperand, PlaceTy, Scalar, ScalarMaybeUninit, StackPopCleanup, StackPopUnwind,
37 };
38
39 /// The maximum number of bytes that we'll allocate space for a local or the return value.
40 /// Needed for #66397, because otherwise we eval into large places and that can cause OOM or just
41 /// Severely regress performance.
42 const MAX_ALLOC_LIMIT: u64 = 1024;
43
44 /// Macro for machine-specific `InterpError` without allocation.
45 /// (These will never be shown to the user, but they help diagnose ICEs.)
46 macro_rules! throw_machine_stop_str {
47     ($($tt:tt)*) => {{
48         // We make a new local type for it. The type itself does not carry any information,
49         // but its vtable (for the `MachineStopType` trait) does.
50         struct Zst;
51         // Printing this type shows the desired string.
52         impl std::fmt::Display for Zst {
53             fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54                 write!(f, $($tt)*)
55             }
56         }
57         impl rustc_middle::mir::interpret::MachineStopType for Zst {}
58         throw_machine_stop!(Zst)
59     }};
60 }
61
62 pub struct ConstProp;
63
64 impl<'tcx> MirPass<'tcx> for ConstProp {
65     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
66         // will be evaluated by miri and produce its errors there
67         if body.source.promoted.is_some() {
68             return;
69         }
70
71         use rustc_middle::hir::map::blocks::FnLikeNode;
72         let def_id = body.source.def_id().expect_local();
73         let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
74
75         let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some();
76         let is_assoc_const = tcx.def_kind(def_id.to_def_id()) == DefKind::AssocConst;
77
78         // Only run const prop on functions, methods, closures and associated constants
79         if !is_fn_like && !is_assoc_const {
80             // skip anon_const/statics/consts because they'll be evaluated by miri anyway
81             trace!("ConstProp skipped for {:?}", def_id);
82             return;
83         }
84
85         let is_generator = tcx.type_of(def_id.to_def_id()).is_generator();
86         // FIXME(welseywiser) const prop doesn't work on generators because of query cycles
87         // computing their layout.
88         if is_generator {
89             trace!("ConstProp skipped for generator {:?}", def_id);
90             return;
91         }
92
93         // Check if it's even possible to satisfy the 'where' clauses
94         // for this item.
95         // This branch will never be taken for any normal function.
96         // However, it's possible to `#!feature(trivial_bounds)]` to write
97         // a function with impossible to satisfy clauses, e.g.:
98         // `fn foo() where String: Copy {}`
99         //
100         // We don't usually need to worry about this kind of case,
101         // since we would get a compilation error if the user tried
102         // to call it. However, since we can do const propagation
103         // even without any calls to the function, we need to make
104         // sure that it even makes sense to try to evaluate the body.
105         // If there are unsatisfiable where clauses, then all bets are
106         // off, and we just give up.
107         //
108         // We manually filter the predicates, skipping anything that's not
109         // "global". We are in a potentially generic context
110         // (e.g. we are evaluating a function without substituting generic
111         // parameters, so this filtering serves two purposes:
112         //
113         // 1. We skip evaluating any predicates that we would
114         // never be able prove are unsatisfiable (e.g. `<T as Foo>`
115         // 2. We avoid trying to normalize predicates involving generic
116         // parameters (e.g. `<T as Foo>::MyItem`). This can confuse
117         // the normalization code (leading to cycle errors), since
118         // it's usually never invoked in this way.
119         let predicates = tcx
120             .predicates_of(def_id.to_def_id())
121             .predicates
122             .iter()
123             .filter_map(|(p, _)| if p.is_global(tcx) { Some(*p) } else { None });
124         if traits::impossible_predicates(
125             tcx,
126             traits::elaborate_predicates(tcx, predicates).map(|o| o.predicate).collect(),
127         ) {
128             trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", def_id);
129             return;
130         }
131
132         trace!("ConstProp starting for {:?}", def_id);
133
134         let dummy_body = &Body::new(
135             tcx,
136             body.source,
137             body.basic_blocks().clone(),
138             body.source_scopes.clone(),
139             body.local_decls.clone(),
140             Default::default(),
141             body.arg_count,
142             Default::default(),
143             body.span,
144             body.generator_kind(),
145         );
146
147         // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
148         // constants, instead of just checking for const-folding succeeding.
149         // That would require a uniform one-def no-mutation analysis
150         // and RPO (or recursing when needing the value of a local).
151         let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx);
152         optimization_finder.visit_body(body);
153
154         trace!("ConstProp done for {:?}", def_id);
155     }
156 }
157
158 struct ConstPropMachine<'mir, 'tcx> {
159     /// The virtual call stack.
160     stack: Vec<Frame<'mir, 'tcx>>,
161     /// `OnlyInsideOwnBlock` locals that were written in the current block get erased at the end.
162     written_only_inside_own_block_locals: FxHashSet<Local>,
163     /// Locals that need to be cleared after every block terminates.
164     only_propagate_inside_block_locals: BitSet<Local>,
165     can_const_prop: IndexVec<Local, ConstPropMode>,
166 }
167
168 impl<'mir, 'tcx> ConstPropMachine<'mir, 'tcx> {
169     fn new(
170         only_propagate_inside_block_locals: BitSet<Local>,
171         can_const_prop: IndexVec<Local, ConstPropMode>,
172     ) -> Self {
173         Self {
174             stack: Vec::new(),
175             written_only_inside_own_block_locals: Default::default(),
176             only_propagate_inside_block_locals,
177             can_const_prop,
178         }
179     }
180 }
181
182 impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> {
183     compile_time_machine!(<'mir, 'tcx>);
184     const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small (see `MAX_ALLOC_LIMIT`)
185
186     type MemoryKind = !;
187
188     type MemoryExtra = ();
189
190     fn load_mir(
191         _ecx: &InterpCx<'mir, 'tcx, Self>,
192         _instance: ty::InstanceDef<'tcx>,
193     ) -> InterpResult<'tcx, &'tcx Body<'tcx>> {
194         throw_machine_stop_str!("calling functions isn't supported in ConstProp")
195     }
196
197     fn find_mir_or_eval_fn(
198         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
199         _instance: ty::Instance<'tcx>,
200         _abi: Abi,
201         _args: &[OpTy<'tcx>],
202         _ret: Option<(&PlaceTy<'tcx>, BasicBlock)>,
203         _unwind: StackPopUnwind,
204     ) -> InterpResult<'tcx, Option<&'mir Body<'tcx>>> {
205         Ok(None)
206     }
207
208     fn call_intrinsic(
209         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
210         _instance: ty::Instance<'tcx>,
211         _args: &[OpTy<'tcx>],
212         _ret: Option<(&PlaceTy<'tcx>, BasicBlock)>,
213         _unwind: StackPopUnwind,
214     ) -> InterpResult<'tcx> {
215         throw_machine_stop_str!("calling intrinsics isn't supported in ConstProp")
216     }
217
218     fn assert_panic(
219         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
220         _msg: &rustc_middle::mir::AssertMessage<'tcx>,
221         _unwind: Option<rustc_middle::mir::BasicBlock>,
222     ) -> InterpResult<'tcx> {
223         bug!("panics terminators are not evaluated in ConstProp")
224     }
225
226     fn binary_ptr_op(
227         _ecx: &InterpCx<'mir, 'tcx, Self>,
228         _bin_op: BinOp,
229         _left: &ImmTy<'tcx>,
230         _right: &ImmTy<'tcx>,
231     ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
232         // We can't do this because aliasing of memory can differ between const eval and llvm
233         throw_machine_stop_str!("pointer arithmetic or comparisons aren't supported in ConstProp")
234     }
235
236     fn box_alloc(
237         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
238         _dest: &PlaceTy<'tcx>,
239     ) -> InterpResult<'tcx> {
240         throw_machine_stop_str!("can't const prop heap allocations")
241     }
242
243     fn access_local(
244         _ecx: &InterpCx<'mir, 'tcx, Self>,
245         frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
246         local: Local,
247     ) -> InterpResult<'tcx, InterpOperand<Self::PointerTag>> {
248         let l = &frame.locals[local];
249
250         if l.value == LocalValue::Uninitialized {
251             throw_machine_stop_str!("tried to access an uninitialized local")
252         }
253
254         l.access()
255     }
256
257     fn access_local_mut<'a>(
258         ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
259         frame: usize,
260         local: Local,
261     ) -> InterpResult<'tcx, Result<&'a mut LocalValue<Self::PointerTag>, MemPlace<Self::PointerTag>>>
262     {
263         if ecx.machine.can_const_prop[local] == ConstPropMode::NoPropagation {
264             throw_machine_stop_str!("tried to write to a local that is marked as not propagatable")
265         }
266         if frame == 0 && ecx.machine.only_propagate_inside_block_locals.contains(local) {
267             trace!(
268                 "mutating local {:?} which is restricted to its block. \
269                 Will remove it from const-prop after block is finished.",
270                 local
271             );
272             ecx.machine.written_only_inside_own_block_locals.insert(local);
273         }
274         ecx.machine.stack[frame].locals[local].access_mut()
275     }
276
277     fn before_access_global(
278         _memory_extra: &(),
279         _alloc_id: AllocId,
280         allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
281         _static_def_id: Option<DefId>,
282         is_write: bool,
283     ) -> InterpResult<'tcx> {
284         if is_write {
285             throw_machine_stop_str!("can't write to global");
286         }
287         // If the static allocation is mutable, then we can't const prop it as its content
288         // might be different at runtime.
289         if allocation.mutability == Mutability::Mut {
290             throw_machine_stop_str!("can't access mutable globals in ConstProp");
291         }
292
293         Ok(())
294     }
295
296     #[inline(always)]
297     fn init_frame_extra(
298         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
299         frame: Frame<'mir, 'tcx>,
300     ) -> InterpResult<'tcx, Frame<'mir, 'tcx>> {
301         Ok(frame)
302     }
303
304     #[inline(always)]
305     fn stack(
306         ecx: &'a InterpCx<'mir, 'tcx, Self>,
307     ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
308         &ecx.machine.stack
309     }
310
311     #[inline(always)]
312     fn stack_mut(
313         ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
314     ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
315         &mut ecx.machine.stack
316     }
317 }
318
319 /// Finds optimization opportunities on the MIR.
320 struct ConstPropagator<'mir, 'tcx> {
321     ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
322     tcx: TyCtxt<'tcx>,
323     param_env: ParamEnv<'tcx>,
324     // FIXME(eddyb) avoid cloning these two fields more than once,
325     // by accessing them through `ecx` instead.
326     source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
327     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
328     // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
329     // the last known `SourceInfo` here and just keep revisiting it.
330     source_info: Option<SourceInfo>,
331 }
332
333 impl<'mir, 'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'mir, 'tcx> {
334     type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
335
336     #[inline]
337     fn handle_layout_err(&self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>) -> LayoutError<'tcx> {
338         err
339     }
340 }
341
342 impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
343     #[inline]
344     fn data_layout(&self) -> &TargetDataLayout {
345         &self.tcx.data_layout
346     }
347 }
348
349 impl<'mir, 'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'mir, 'tcx> {
350     #[inline]
351     fn tcx(&self) -> TyCtxt<'tcx> {
352         self.tcx
353     }
354 }
355
356 impl<'mir, 'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'mir, 'tcx> {
357     #[inline]
358     fn param_env(&self) -> ty::ParamEnv<'tcx> {
359         self.param_env
360     }
361 }
362
363 impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
364     fn new(
365         body: &Body<'tcx>,
366         dummy_body: &'mir Body<'tcx>,
367         tcx: TyCtxt<'tcx>,
368     ) -> ConstPropagator<'mir, 'tcx> {
369         let def_id = body.source.def_id();
370         let substs = &InternalSubsts::identity_for_item(tcx, def_id);
371         let param_env = tcx.param_env_reveal_all_normalized(def_id);
372
373         let span = tcx.def_span(def_id);
374         // FIXME: `CanConstProp::check` computes the layout of all locals, return those layouts
375         // so we can write them to `ecx.frame_mut().locals.layout, reducing the duplication in
376         // `layout_of` query invocations.
377         let can_const_prop = CanConstProp::check(tcx, param_env, body);
378         let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
379         for (l, mode) in can_const_prop.iter_enumerated() {
380             if *mode == ConstPropMode::OnlyInsideOwnBlock {
381                 only_propagate_inside_block_locals.insert(l);
382             }
383         }
384         let mut ecx = InterpCx::new(
385             tcx,
386             span,
387             param_env,
388             ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop),
389             (),
390         );
391
392         let ret = ecx
393             .layout_of(body.return_ty().subst(tcx, substs))
394             .ok()
395             // Don't bother allocating memory for ZST types which have no values
396             // or for large values.
397             .filter(|ret_layout| {
398                 !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
399             })
400             .map(|ret_layout| {
401                 ecx.allocate(ret_layout, MemoryKind::Stack)
402                     .expect("couldn't perform small allocation")
403                     .into()
404             });
405
406         ecx.push_stack_frame(
407             Instance::new(def_id, substs),
408             dummy_body,
409             ret.as_ref(),
410             StackPopCleanup::None { cleanup: false },
411         )
412         .expect("failed to push initial stack frame");
413
414         ConstPropagator {
415             ecx,
416             tcx,
417             param_env,
418             // FIXME(eddyb) avoid cloning these two fields more than once,
419             // by accessing them through `ecx` instead.
420             source_scopes: body.source_scopes.clone(),
421             //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
422             local_decls: body.local_decls.clone(),
423             source_info: None,
424         }
425     }
426
427     fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
428         let op = match self.ecx.eval_place_to_op(place, None) {
429             Ok(op) => op,
430             Err(e) => {
431                 trace!("get_const failed: {}", e);
432                 return None;
433             }
434         };
435
436         // Try to read the local as an immediate so that if it is representable as a scalar, we can
437         // handle it as such, but otherwise, just return the value as is.
438         Some(match self.ecx.try_read_immediate(&op) {
439             Ok(Ok(imm)) => imm.into(),
440             _ => op,
441         })
442     }
443
444     /// Remove `local` from the pool of `Locals`. Allows writing to them,
445     /// but not reading from them anymore.
446     fn remove_const(ecx: &mut InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>, local: Local) {
447         ecx.frame_mut().locals[local] =
448             LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
449     }
450
451     fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
452         source_info.scope.lint_root(&self.source_scopes)
453     }
454
455     fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
456     where
457         F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
458     {
459         match f(self) {
460             Ok(val) => Some(val),
461             Err(error) => {
462                 trace!("InterpCx operation failed: {:?}", error);
463                 // Some errors shouldn't come up because creating them causes
464                 // an allocation, which we should avoid. When that happens,
465                 // dedicated error variants should be introduced instead.
466                 assert!(
467                     !error.kind().formatted_string(),
468                     "const-prop encountered formatting error: {}",
469                     error
470                 );
471                 None
472             }
473         }
474     }
475
476     /// Returns the value, if any, of evaluating `c`.
477     fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
478         // FIXME we need to revisit this for #67176
479         if c.definitely_needs_subst(self.tcx) {
480             return None;
481         }
482
483         match self.ecx.mir_const_to_op(&c.literal, None) {
484             Ok(op) => Some(op),
485             Err(error) => {
486                 let tcx = self.ecx.tcx.at(c.span);
487                 let err = ConstEvalErr::new(&self.ecx, error, Some(c.span));
488                 if let Some(lint_root) = self.lint_root(source_info) {
489                     let lint_only = match c.literal {
490                         ConstantKind::Ty(ct) => match ct.val {
491                             // Promoteds must lint and not error as the user didn't ask for them
492                             ConstKind::Unevaluated(ty::Unevaluated {
493                                 def: _,
494                                 substs_: _,
495                                 promoted: Some(_),
496                             }) => true,
497                             // Out of backwards compatibility we cannot report hard errors in unused
498                             // generic functions using associated constants of the generic parameters.
499                             _ => c.literal.definitely_needs_subst(*tcx),
500                         },
501                         ConstantKind::Val(_, ty) => ty.definitely_needs_subst(*tcx),
502                     };
503                     if lint_only {
504                         // Out of backwards compatibility we cannot report hard errors in unused
505                         // generic functions using associated constants of the generic parameters.
506                         err.report_as_lint(tcx, "erroneous constant used", lint_root, Some(c.span));
507                     } else {
508                         err.report_as_error(tcx, "erroneous constant used");
509                     }
510                 } else {
511                     err.report_as_error(tcx, "erroneous constant used");
512                 }
513                 None
514             }
515         }
516     }
517
518     /// Returns the value, if any, of evaluating `place`.
519     fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
520         trace!("eval_place(place={:?})", place);
521         self.use_ecx(|this| this.ecx.eval_place_to_op(place, None))
522     }
523
524     /// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
525     /// or `eval_place`, depending on the variant of `Operand` used.
526     fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
527         match *op {
528             Operand::Constant(ref c) => self.eval_constant(c, source_info),
529             Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
530         }
531     }
532
533     fn report_assert_as_lint(
534         &self,
535         lint: &'static lint::Lint,
536         source_info: SourceInfo,
537         message: &'static str,
538         panic: AssertKind<impl std::fmt::Debug>,
539     ) {
540         if let Some(lint_root) = self.lint_root(source_info) {
541             self.tcx.struct_span_lint_hir(lint, lint_root, source_info.span, |lint| {
542                 let mut err = lint.build(message);
543                 err.span_label(source_info.span, format!("{:?}", panic));
544                 err.emit()
545             });
546         }
547     }
548
549     fn check_unary_op(
550         &mut self,
551         op: UnOp,
552         arg: &Operand<'tcx>,
553         source_info: SourceInfo,
554     ) -> Option<()> {
555         if let (val, true) = self.use_ecx(|this| {
556             let val = this.ecx.read_immediate(&this.ecx.eval_operand(arg, None)?)?;
557             let (_res, overflow, _ty) = this.ecx.overflowing_unary_op(op, &val)?;
558             Ok((val, overflow))
559         })? {
560             // `AssertKind` only has an `OverflowNeg` variant, so make sure that is
561             // appropriate to use.
562             assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
563             self.report_assert_as_lint(
564                 lint::builtin::ARITHMETIC_OVERFLOW,
565                 source_info,
566                 "this arithmetic operation will overflow",
567                 AssertKind::OverflowNeg(val.to_const_int()),
568             );
569             return None;
570         }
571
572         Some(())
573     }
574
575     fn check_binary_op(
576         &mut self,
577         op: BinOp,
578         left: &Operand<'tcx>,
579         right: &Operand<'tcx>,
580         source_info: SourceInfo,
581     ) -> Option<()> {
582         let r = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?));
583         let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?));
584         // Check for exceeding shifts *even if* we cannot evaluate the LHS.
585         if op == BinOp::Shr || op == BinOp::Shl {
586             let r = r?;
587             // We need the type of the LHS. We cannot use `place_layout` as that is the type
588             // of the result, which for checked binops is not the same!
589             let left_ty = left.ty(&self.local_decls, self.tcx);
590             let left_size = self.ecx.layout_of(left_ty).ok()?.size;
591             let right_size = r.layout.size;
592             let r_bits = r.to_scalar().ok();
593             let r_bits = r_bits.and_then(|r| r.to_bits(right_size).ok());
594             if r_bits.map_or(false, |b| b >= left_size.bits() as u128) {
595                 debug!("check_binary_op: reporting assert for {:?}", source_info);
596                 self.report_assert_as_lint(
597                     lint::builtin::ARITHMETIC_OVERFLOW,
598                     source_info,
599                     "this arithmetic operation will overflow",
600                     AssertKind::Overflow(
601                         op,
602                         match l {
603                             Some(l) => l.to_const_int(),
604                             // Invent a dummy value, the diagnostic ignores it anyway
605                             None => ConstInt::new(
606                                 ScalarInt::try_from_uint(1_u8, left_size).unwrap(),
607                                 left_ty.is_signed(),
608                                 left_ty.is_ptr_sized_integral(),
609                             ),
610                         },
611                         r.to_const_int(),
612                     ),
613                 );
614                 return None;
615             }
616         }
617
618         if let (Some(l), Some(r)) = (&l, &r) {
619             // The remaining operators are handled through `overflowing_binary_op`.
620             if self.use_ecx(|this| {
621                 let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
622                 Ok(overflow)
623             })? {
624                 self.report_assert_as_lint(
625                     lint::builtin::ARITHMETIC_OVERFLOW,
626                     source_info,
627                     "this arithmetic operation will overflow",
628                     AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
629                 );
630                 return None;
631             }
632         }
633         Some(())
634     }
635
636     fn propagate_operand(&mut self, operand: &mut Operand<'tcx>) {
637         match *operand {
638             Operand::Copy(l) | Operand::Move(l) => {
639                 if let Some(value) = self.get_const(l) {
640                     if self.should_const_prop(&value) {
641                         // FIXME(felix91gr): this code only handles `Scalar` cases.
642                         // For now, we're not handling `ScalarPair` cases because
643                         // doing so here would require a lot of code duplication.
644                         // We should hopefully generalize `Operand` handling into a fn,
645                         // and use it to do const-prop here and everywhere else
646                         // where it makes sense.
647                         if let interpret::Operand::Immediate(interpret::Immediate::Scalar(
648                             ScalarMaybeUninit::Scalar(scalar),
649                         )) = *value
650                         {
651                             *operand = self.operand_from_scalar(
652                                 scalar,
653                                 value.layout.ty,
654                                 self.source_info.unwrap().span,
655                             );
656                         }
657                     }
658                 }
659             }
660             Operand::Constant(_) => (),
661         }
662     }
663
664     fn const_prop(
665         &mut self,
666         rvalue: &Rvalue<'tcx>,
667         source_info: SourceInfo,
668         place: Place<'tcx>,
669     ) -> Option<()> {
670         // Perform any special handling for specific Rvalue types.
671         // Generally, checks here fall into one of two categories:
672         //   1. Additional checking to provide useful lints to the user
673         //        - In this case, we will do some validation and then fall through to the
674         //          end of the function which evals the assignment.
675         //   2. Working around bugs in other parts of the compiler
676         //        - In this case, we'll return `None` from this function to stop evaluation.
677         match rvalue {
678             // Additional checking: give lints to the user if an overflow would occur.
679             // We do this here and not in the `Assert` terminator as that terminator is
680             // only sometimes emitted (overflow checks can be disabled), but we want to always
681             // lint.
682             Rvalue::UnaryOp(op, arg) => {
683                 trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
684                 self.check_unary_op(*op, arg, source_info)?;
685             }
686             Rvalue::BinaryOp(op, box (left, right)) => {
687                 trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
688                 self.check_binary_op(*op, left, right, source_info)?;
689             }
690             Rvalue::CheckedBinaryOp(op, box (left, right)) => {
691                 trace!(
692                     "checking CheckedBinaryOp(op = {:?}, left = {:?}, right = {:?})",
693                     op,
694                     left,
695                     right
696                 );
697                 self.check_binary_op(*op, left, right, source_info)?;
698             }
699
700             // Do not try creating references (#67862)
701             Rvalue::AddressOf(_, place) | Rvalue::Ref(_, _, place) => {
702                 trace!("skipping AddressOf | Ref for {:?}", place);
703
704                 // This may be creating mutable references or immutable references to cells.
705                 // If that happens, the pointed to value could be mutated via that reference.
706                 // Since we aren't tracking references, the const propagator loses track of what
707                 // value the local has right now.
708                 // Thus, all locals that have their reference taken
709                 // must not take part in propagation.
710                 Self::remove_const(&mut self.ecx, place.local);
711
712                 return None;
713             }
714             Rvalue::ThreadLocalRef(def_id) => {
715                 trace!("skipping ThreadLocalRef({:?})", def_id);
716
717                 return None;
718             }
719
720             // There's no other checking to do at this time.
721             Rvalue::Aggregate(..)
722             | Rvalue::Use(..)
723             | Rvalue::Repeat(..)
724             | Rvalue::Len(..)
725             | Rvalue::Cast(..)
726             | Rvalue::Discriminant(..)
727             | Rvalue::NullaryOp(..) => {}
728         }
729
730         // FIXME we need to revisit this for #67176
731         if rvalue.definitely_needs_subst(self.tcx) {
732             return None;
733         }
734
735         if self.tcx.sess.mir_opt_level() >= 4 {
736             self.eval_rvalue_with_identities(rvalue, place)
737         } else {
738             self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
739         }
740     }
741
742     // Attempt to use albegraic identities to eliminate constant expressions
743     fn eval_rvalue_with_identities(
744         &mut self,
745         rvalue: &Rvalue<'tcx>,
746         place: Place<'tcx>,
747     ) -> Option<()> {
748         self.use_ecx(|this| {
749             match rvalue {
750                 Rvalue::BinaryOp(op, box (left, right))
751                 | Rvalue::CheckedBinaryOp(op, box (left, right)) => {
752                     let l = this.ecx.eval_operand(left, None);
753                     let r = this.ecx.eval_operand(right, None);
754
755                     let const_arg = match (l, r) {
756                         (Ok(ref x), Err(_)) | (Err(_), Ok(ref x)) => this.ecx.read_immediate(x)?,
757                         (Err(e), Err(_)) => return Err(e),
758                         (Ok(_), Ok(_)) => {
759                             this.ecx.eval_rvalue_into_place(rvalue, place)?;
760                             return Ok(());
761                         }
762                     };
763
764                     let arg_value = const_arg.to_scalar()?.to_bits(const_arg.layout.size)?;
765                     let dest = this.ecx.eval_place(place)?;
766
767                     match op {
768                         BinOp::BitAnd => {
769                             if arg_value == 0 {
770                                 this.ecx.write_immediate(*const_arg, &dest)?;
771                             }
772                         }
773                         BinOp::BitOr => {
774                             if arg_value == const_arg.layout.size.truncate(u128::MAX)
775                                 || (const_arg.layout.ty.is_bool() && arg_value == 1)
776                             {
777                                 this.ecx.write_immediate(*const_arg, &dest)?;
778                             }
779                         }
780                         BinOp::Mul => {
781                             if const_arg.layout.ty.is_integral() && arg_value == 0 {
782                                 if let Rvalue::CheckedBinaryOp(_, _) = rvalue {
783                                     let val = Immediate::ScalarPair(
784                                         const_arg.to_scalar()?.into(),
785                                         Scalar::from_bool(false).into(),
786                                     );
787                                     this.ecx.write_immediate(val, &dest)?;
788                                 } else {
789                                     this.ecx.write_immediate(*const_arg, &dest)?;
790                                 }
791                             }
792                         }
793                         _ => {
794                             this.ecx.eval_rvalue_into_place(rvalue, place)?;
795                         }
796                     }
797                 }
798                 _ => {
799                     this.ecx.eval_rvalue_into_place(rvalue, place)?;
800                 }
801             }
802
803             Ok(())
804         })
805     }
806
807     /// Creates a new `Operand::Constant` from a `Scalar` value
808     fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
809         Operand::Constant(Box::new(Constant {
810             span,
811             user_ty: None,
812             literal: ty::Const::from_scalar(self.tcx, scalar, ty).into(),
813         }))
814     }
815
816     fn replace_with_const(
817         &mut self,
818         rval: &mut Rvalue<'tcx>,
819         value: &OpTy<'tcx>,
820         source_info: SourceInfo,
821     ) {
822         if let Rvalue::Use(Operand::Constant(c)) = rval {
823             match c.literal {
824                 ConstantKind::Ty(c) if matches!(c.val, ConstKind::Unevaluated(..)) => {}
825                 _ => {
826                     trace!("skipping replace of Rvalue::Use({:?} because it is already a const", c);
827                     return;
828                 }
829             }
830         }
831
832         trace!("attempting to replace {:?} with {:?}", rval, value);
833         if let Err(e) = self.ecx.const_validate_operand(
834             value,
835             vec![],
836             // FIXME: is ref tracking too expensive?
837             // FIXME: what is the point of ref tracking if we do not even check the tracked refs?
838             &mut interpret::RefTracking::empty(),
839             CtfeValidationMode::Regular,
840         ) {
841             trace!("validation error, attempt failed: {:?}", e);
842             return;
843         }
844
845         // FIXME> figure out what to do when try_read_immediate fails
846         let imm = self.use_ecx(|this| this.ecx.try_read_immediate(value));
847
848         if let Some(Ok(imm)) = imm {
849             match *imm {
850                 interpret::Immediate::Scalar(ScalarMaybeUninit::Scalar(scalar)) => {
851                     *rval = Rvalue::Use(self.operand_from_scalar(
852                         scalar,
853                         value.layout.ty,
854                         source_info.span,
855                     ));
856                 }
857                 Immediate::ScalarPair(
858                     ScalarMaybeUninit::Scalar(_),
859                     ScalarMaybeUninit::Scalar(_),
860                 ) => {
861                     // Found a value represented as a pair. For now only do const-prop if the type
862                     // of `rvalue` is also a tuple with two scalars.
863                     // FIXME: enable the general case stated above ^.
864                     let ty = &value.layout.ty;
865                     // Only do it for tuples
866                     if let ty::Tuple(substs) = ty.kind() {
867                         // Only do it if tuple is also a pair with two scalars
868                         if substs.len() == 2 {
869                             let alloc = self.use_ecx(|this| {
870                                 let ty1 = substs[0].expect_ty();
871                                 let ty2 = substs[1].expect_ty();
872                                 let ty_is_scalar = |ty| {
873                                     this.ecx.layout_of(ty).ok().map(|layout| layout.abi.is_scalar())
874                                         == Some(true)
875                                 };
876                                 if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
877                                     let alloc = this
878                                         .ecx
879                                         .intern_with_temp_alloc(value.layout, |ecx, dest| {
880                                             ecx.write_immediate(*imm, dest)
881                                         })
882                                         .unwrap();
883                                     Ok(Some(alloc))
884                                 } else {
885                                     Ok(None)
886                                 }
887                             });
888
889                             if let Some(Some(alloc)) = alloc {
890                                 // Assign entire constant in a single statement.
891                                 // We can't use aggregates, as we run after the aggregate-lowering `MirPhase`.
892                                 *rval = Rvalue::Use(Operand::Constant(Box::new(Constant {
893                                     span: source_info.span,
894                                     user_ty: None,
895                                     literal: self
896                                         .ecx
897                                         .tcx
898                                         .mk_const(ty::Const {
899                                             ty,
900                                             val: ty::ConstKind::Value(ConstValue::ByRef {
901                                                 alloc,
902                                                 offset: Size::ZERO,
903                                             }),
904                                         })
905                                         .into(),
906                                 })));
907                             }
908                         }
909                     }
910                 }
911                 // Scalars or scalar pairs that contain undef values are assumed to not have
912                 // successfully evaluated and are thus not propagated.
913                 _ => {}
914             }
915         }
916     }
917
918     /// Returns `true` if and only if this `op` should be const-propagated into.
919     fn should_const_prop(&mut self, op: &OpTy<'tcx>) -> bool {
920         let mir_opt_level = self.tcx.sess.mir_opt_level();
921
922         if mir_opt_level == 0 {
923             return false;
924         }
925
926         if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
927             return false;
928         }
929
930         match **op {
931             interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
932                 s.try_to_int().is_ok()
933             }
934             interpret::Operand::Immediate(Immediate::ScalarPair(
935                 ScalarMaybeUninit::Scalar(l),
936                 ScalarMaybeUninit::Scalar(r),
937             )) => l.try_to_int().is_ok() && r.try_to_int().is_ok(),
938             _ => false,
939         }
940     }
941 }
942
943 /// The mode that `ConstProp` is allowed to run in for a given `Local`.
944 #[derive(Clone, Copy, Debug, PartialEq)]
945 enum ConstPropMode {
946     /// The `Local` can be propagated into and reads of this `Local` can also be propagated.
947     FullConstProp,
948     /// The `Local` can only be propagated into and from its own block.
949     OnlyInsideOwnBlock,
950     /// The `Local` can be propagated into but reads cannot be propagated.
951     OnlyPropagateInto,
952     /// The `Local` cannot be part of propagation at all. Any statement
953     /// referencing it either for reading or writing will not get propagated.
954     NoPropagation,
955 }
956
957 struct CanConstProp {
958     can_const_prop: IndexVec<Local, ConstPropMode>,
959     // False at the beginning. Once set, no more assignments are allowed to that local.
960     found_assignment: BitSet<Local>,
961     // Cache of locals' information
962     local_kinds: IndexVec<Local, LocalKind>,
963 }
964
965 impl CanConstProp {
966     /// Returns true if `local` can be propagated
967     fn check(
968         tcx: TyCtxt<'tcx>,
969         param_env: ParamEnv<'tcx>,
970         body: &Body<'tcx>,
971     ) -> IndexVec<Local, ConstPropMode> {
972         let mut cpv = CanConstProp {
973             can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
974             found_assignment: BitSet::new_empty(body.local_decls.len()),
975             local_kinds: IndexVec::from_fn_n(
976                 |local| body.local_kind(local),
977                 body.local_decls.len(),
978             ),
979         };
980         for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
981             let ty = body.local_decls[local].ty;
982             match tcx.layout_of(param_env.and(ty)) {
983                 Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
984                 // Either the layout fails to compute, then we can't use this local anyway
985                 // or the local is too large, then we don't want to.
986                 _ => {
987                     *val = ConstPropMode::NoPropagation;
988                     continue;
989                 }
990             }
991             // Cannot use args at all
992             // Cannot use locals because if x < y { y - x } else { x - y } would
993             //        lint for x != y
994             // FIXME(oli-obk): lint variables until they are used in a condition
995             // FIXME(oli-obk): lint if return value is constant
996             if cpv.local_kinds[local] == LocalKind::Arg {
997                 *val = ConstPropMode::OnlyPropagateInto;
998                 trace!(
999                     "local {:?} can't be const propagated because it's a function argument",
1000                     local
1001                 );
1002             } else if cpv.local_kinds[local] == LocalKind::Var {
1003                 *val = ConstPropMode::OnlyInsideOwnBlock;
1004                 trace!(
1005                     "local {:?} will only be propagated inside its block, because it's a user variable",
1006                     local
1007                 );
1008             }
1009         }
1010         cpv.visit_body(&body);
1011         cpv.can_const_prop
1012     }
1013 }
1014
1015 impl<'tcx> Visitor<'tcx> for CanConstProp {
1016     fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
1017         use rustc_middle::mir::visit::PlaceContext::*;
1018         match context {
1019             // Projections are fine, because `&mut foo.x` will be caught by
1020             // `MutatingUseContext::Borrow` elsewhere.
1021             MutatingUse(MutatingUseContext::Projection)
1022             // These are just stores, where the storing is not propagatable, but there may be later
1023             // mutations of the same local via `Store`
1024             | MutatingUse(MutatingUseContext::Call)
1025             // Actual store that can possibly even propagate a value
1026             | MutatingUse(MutatingUseContext::Store) => {
1027                 if !self.found_assignment.insert(local) {
1028                     match &mut self.can_const_prop[local] {
1029                         // If the local can only get propagated in its own block, then we don't have
1030                         // to worry about multiple assignments, as we'll nuke the const state at the
1031                         // end of the block anyway, and inside the block we overwrite previous
1032                         // states as applicable.
1033                         ConstPropMode::OnlyInsideOwnBlock => {}
1034                         ConstPropMode::NoPropagation => {}
1035                         ConstPropMode::OnlyPropagateInto => {}
1036                         other @ ConstPropMode::FullConstProp => {
1037                             trace!(
1038                                 "local {:?} can't be propagated because of multiple assignments. Previous state: {:?}",
1039                                 local, other,
1040                             );
1041                             *other = ConstPropMode::OnlyInsideOwnBlock;
1042                         }
1043                     }
1044                 }
1045             }
1046             // Reading constants is allowed an arbitrary number of times
1047             NonMutatingUse(NonMutatingUseContext::Copy)
1048             | NonMutatingUse(NonMutatingUseContext::Move)
1049             | NonMutatingUse(NonMutatingUseContext::Inspect)
1050             | NonMutatingUse(NonMutatingUseContext::Projection)
1051             | NonUse(_) => {}
1052
1053             // These could be propagated with a smarter analysis or just some careful thinking about
1054             // whether they'd be fine right now.
1055             MutatingUse(MutatingUseContext::AsmOutput)
1056             | MutatingUse(MutatingUseContext::Yield)
1057             | MutatingUse(MutatingUseContext::Drop)
1058             | MutatingUse(MutatingUseContext::Retag)
1059             // These can't ever be propagated under any scheme, as we can't reason about indirect
1060             // mutation.
1061             | NonMutatingUse(NonMutatingUseContext::SharedBorrow)
1062             | NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
1063             | NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
1064             | NonMutatingUse(NonMutatingUseContext::AddressOf)
1065             | MutatingUse(MutatingUseContext::Borrow)
1066             | MutatingUse(MutatingUseContext::AddressOf) => {
1067                 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
1068                 self.can_const_prop[local] = ConstPropMode::NoPropagation;
1069             }
1070         }
1071     }
1072 }
1073
1074 impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
1075     fn tcx(&self) -> TyCtxt<'tcx> {
1076         self.tcx
1077     }
1078
1079     fn visit_body(&mut self, body: &mut Body<'tcx>) {
1080         for (bb, data) in body.basic_blocks_mut().iter_enumerated_mut() {
1081             self.visit_basic_block_data(bb, data);
1082         }
1083     }
1084
1085     fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
1086         self.super_operand(operand, location);
1087
1088         // Only const prop copies and moves on `mir_opt_level=3` as doing so
1089         // currently slightly increases compile time in some cases.
1090         if self.tcx.sess.mir_opt_level() >= 3 {
1091             self.propagate_operand(operand)
1092         }
1093     }
1094
1095     fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
1096         trace!("visit_constant: {:?}", constant);
1097         self.super_constant(constant, location);
1098         self.eval_constant(constant, self.source_info.unwrap());
1099     }
1100
1101     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1102         trace!("visit_statement: {:?}", statement);
1103         let source_info = statement.source_info;
1104         self.source_info = Some(source_info);
1105         if let StatementKind::Assign(box (place, ref mut rval)) = statement.kind {
1106             let can_const_prop = self.ecx.machine.can_const_prop[place.local];
1107             if let Some(()) = self.const_prop(rval, source_info, place) {
1108                 // This will return None if the above `const_prop` invocation only "wrote" a
1109                 // type whose creation requires no write. E.g. a generator whose initial state
1110                 // consists solely of uninitialized memory (so it doesn't capture any locals).
1111                 if let Some(ref value) = self.get_const(place) {
1112                     if self.should_const_prop(value) {
1113                         trace!("replacing {:?} with {:?}", rval, value);
1114                         self.replace_with_const(rval, value, source_info);
1115                         if can_const_prop == ConstPropMode::FullConstProp
1116                             || can_const_prop == ConstPropMode::OnlyInsideOwnBlock
1117                         {
1118                             trace!("propagated into {:?}", place);
1119                         }
1120                     }
1121                 }
1122                 match can_const_prop {
1123                     ConstPropMode::OnlyInsideOwnBlock => {
1124                         trace!(
1125                             "found local restricted to its block. \
1126                                 Will remove it from const-prop after block is finished. Local: {:?}",
1127                             place.local
1128                         );
1129                     }
1130                     ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
1131                         trace!("can't propagate into {:?}", place);
1132                         if place.local != RETURN_PLACE {
1133                             Self::remove_const(&mut self.ecx, place.local);
1134                         }
1135                     }
1136                     ConstPropMode::FullConstProp => {}
1137                 }
1138             } else {
1139                 // Const prop failed, so erase the destination, ensuring that whatever happens
1140                 // from here on, does not know about the previous value.
1141                 // This is important in case we have
1142                 // ```rust
1143                 // let mut x = 42;
1144                 // x = SOME_MUTABLE_STATIC;
1145                 // // x must now be uninit
1146                 // ```
1147                 // FIXME: we overzealously erase the entire local, because that's easier to
1148                 // implement.
1149                 trace!(
1150                     "propagation into {:?} failed.
1151                         Nuking the entire site from orbit, it's the only way to be sure",
1152                     place,
1153                 );
1154                 Self::remove_const(&mut self.ecx, place.local);
1155             }
1156         } else {
1157             match statement.kind {
1158                 StatementKind::SetDiscriminant { ref place, .. } => {
1159                     match self.ecx.machine.can_const_prop[place.local] {
1160                         ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
1161                             if self.use_ecx(|this| this.ecx.statement(statement)).is_some() {
1162                                 trace!("propped discriminant into {:?}", place);
1163                             } else {
1164                                 Self::remove_const(&mut self.ecx, place.local);
1165                             }
1166                         }
1167                         ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
1168                             Self::remove_const(&mut self.ecx, place.local);
1169                         }
1170                     }
1171                 }
1172                 StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
1173                     let frame = self.ecx.frame_mut();
1174                     frame.locals[local].value =
1175                         if let StatementKind::StorageLive(_) = statement.kind {
1176                             LocalValue::Uninitialized
1177                         } else {
1178                             LocalValue::Dead
1179                         };
1180                 }
1181                 _ => {}
1182             }
1183         }
1184
1185         self.super_statement(statement, location);
1186     }
1187
1188     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
1189         let source_info = terminator.source_info;
1190         self.source_info = Some(source_info);
1191         self.super_terminator(terminator, location);
1192         match &mut terminator.kind {
1193             TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
1194                 if let Some(ref value) = self.eval_operand(&cond, source_info) {
1195                     trace!("assertion on {:?} should be {:?}", value, expected);
1196                     let expected = ScalarMaybeUninit::from(Scalar::from_bool(*expected));
1197                     let value_const = self.ecx.read_scalar(&value).unwrap();
1198                     if expected != value_const {
1199                         enum DbgVal<T> {
1200                             Val(T),
1201                             Underscore,
1202                         }
1203                         impl<T: std::fmt::Debug> std::fmt::Debug for DbgVal<T> {
1204                             fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1205                                 match self {
1206                                     Self::Val(val) => val.fmt(fmt),
1207                                     Self::Underscore => fmt.write_str("_"),
1208                                 }
1209                             }
1210                         }
1211                         let mut eval_to_int = |op| {
1212                             // This can be `None` if the lhs wasn't const propagated and we just
1213                             // triggered the assert on the value of the rhs.
1214                             self.eval_operand(op, source_info).map_or(DbgVal::Underscore, |op| {
1215                                 DbgVal::Val(self.ecx.read_immediate(&op).unwrap().to_const_int())
1216                             })
1217                         };
1218                         let msg = match msg {
1219                             AssertKind::DivisionByZero(op) => {
1220                                 Some(AssertKind::DivisionByZero(eval_to_int(op)))
1221                             }
1222                             AssertKind::RemainderByZero(op) => {
1223                                 Some(AssertKind::RemainderByZero(eval_to_int(op)))
1224                             }
1225                             AssertKind::BoundsCheck { ref len, ref index } => {
1226                                 let len = eval_to_int(len);
1227                                 let index = eval_to_int(index);
1228                                 Some(AssertKind::BoundsCheck { len, index })
1229                             }
1230                             // Overflow is are already covered by checks on the binary operators.
1231                             AssertKind::Overflow(..) | AssertKind::OverflowNeg(_) => None,
1232                             // Need proper const propagator for these.
1233                             _ => None,
1234                         };
1235                         // Poison all places this operand references so that further code
1236                         // doesn't use the invalid value
1237                         match cond {
1238                             Operand::Move(ref place) | Operand::Copy(ref place) => {
1239                                 Self::remove_const(&mut self.ecx, place.local);
1240                             }
1241                             Operand::Constant(_) => {}
1242                         }
1243                         if let Some(msg) = msg {
1244                             self.report_assert_as_lint(
1245                                 lint::builtin::UNCONDITIONAL_PANIC,
1246                                 source_info,
1247                                 "this operation will panic at runtime",
1248                                 msg,
1249                             );
1250                         }
1251                     } else {
1252                         if self.should_const_prop(value) {
1253                             if let ScalarMaybeUninit::Scalar(scalar) = value_const {
1254                                 *cond = self.operand_from_scalar(
1255                                     scalar,
1256                                     self.tcx.types.bool,
1257                                     source_info.span,
1258                                 );
1259                             }
1260                         }
1261                     }
1262                 }
1263             }
1264             TerminatorKind::SwitchInt { ref mut discr, .. } => {
1265                 // FIXME: This is currently redundant with `visit_operand`, but sadly
1266                 // always visiting operands currently causes a perf regression in LLVM codegen, so
1267                 // `visit_operand` currently only runs for propagates places for `mir_opt_level=4`.
1268                 self.propagate_operand(discr)
1269             }
1270             // None of these have Operands to const-propagate.
1271             TerminatorKind::Goto { .. }
1272             | TerminatorKind::Resume
1273             | TerminatorKind::Abort
1274             | TerminatorKind::Return
1275             | TerminatorKind::Unreachable
1276             | TerminatorKind::Drop { .. }
1277             | TerminatorKind::DropAndReplace { .. }
1278             | TerminatorKind::Yield { .. }
1279             | TerminatorKind::GeneratorDrop
1280             | TerminatorKind::FalseEdge { .. }
1281             | TerminatorKind::FalseUnwind { .. }
1282             | TerminatorKind::InlineAsm { .. } => {}
1283             // Every argument in our function calls have already been propagated in `visit_operand`.
1284             //
1285             // NOTE: because LLVM codegen gives slight performance regressions with it, so this is
1286             // gated on `mir_opt_level=3`.
1287             TerminatorKind::Call { .. } => {}
1288         }
1289
1290         // We remove all Locals which are restricted in propagation to their containing blocks and
1291         // which were modified in the current block.
1292         // Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`.
1293         let mut locals = std::mem::take(&mut self.ecx.machine.written_only_inside_own_block_locals);
1294         for &local in locals.iter() {
1295             Self::remove_const(&mut self.ecx, local);
1296         }
1297         locals.clear();
1298         // Put it back so we reuse the heap of the storage
1299         self.ecx.machine.written_only_inside_own_block_locals = locals;
1300         if cfg!(debug_assertions) {
1301             // Ensure we are correctly erasing locals with the non-debug-assert logic.
1302             for local in self.ecx.machine.only_propagate_inside_block_locals.iter() {
1303                 assert!(
1304                     self.get_const(local.into()).is_none()
1305                         || self
1306                             .layout_of(self.local_decls[local].ty)
1307                             .map_or(true, |layout| layout.is_zst())
1308                 )
1309             }
1310         }
1311     }
1312 }