]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/const_prop.rs
Merge branch 'master' into bare-metal-cortex-a
[rust.git] / src / librustc_mir / transform / const_prop.rs
1 //! Propagates constants for early reporting of statically known
2 //! assertion failures
3
4 use std::borrow::Cow;
5 use std::cell::Cell;
6
7 use rustc::mir::interpret::{InterpResult, PanicInfo, Scalar};
8 use rustc::mir::visit::{
9     MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
10 };
11 use rustc::mir::{
12     read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
13     Local, LocalDecl, LocalKind, Location, Operand, Place, ReadOnlyBodyAndCache, Rvalue,
14     SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
15     UnOp, RETURN_PLACE,
16 };
17 use rustc::traits::TraitQueryMode;
18 use rustc::ty::layout::{
19     HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
20 };
21 use rustc::ty::subst::{InternalSubsts, Subst};
22 use rustc::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
23 use rustc_data_structures::fx::FxHashMap;
24 use rustc_hir::def::DefKind;
25 use rustc_hir::def_id::DefId;
26 use rustc_hir::HirId;
27 use rustc_index::vec::IndexVec;
28 use rustc_span::{Span, DUMMY_SP};
29 use syntax::ast::Mutability;
30
31 use crate::const_eval::error_to_const_error;
32 use crate::interpret::{
33     self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InternKind,
34     InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
35     Pointer, ScalarMaybeUndef, StackPopCleanup,
36 };
37 use crate::transform::{MirPass, MirSource};
38
39 /// The maximum number of bytes that we'll allocate space for a return value.
40 const MAX_ALLOC_LIMIT: u64 = 1024;
41
42 pub struct ConstProp;
43
44 impl<'tcx> MirPass<'tcx> for ConstProp {
45     fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
46         // will be evaluated by miri and produce its errors there
47         if source.promoted.is_some() {
48             return;
49         }
50
51         use rustc::hir::map::blocks::FnLikeNode;
52         let hir_id = tcx
53             .hir()
54             .as_local_hir_id(source.def_id())
55             .expect("Non-local call to local provider is_const_fn");
56
57         let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some();
58         let is_assoc_const = match tcx.def_kind(source.def_id()) {
59             Some(DefKind::AssocConst) => true,
60             _ => false,
61         };
62
63         // Only run const prop on functions, methods, closures and associated constants
64         if !is_fn_like && !is_assoc_const {
65             // skip anon_const/statics/consts because they'll be evaluated by miri anyway
66             trace!("ConstProp skipped for {:?}", source.def_id());
67             return;
68         }
69
70         let is_generator = tcx.type_of(source.def_id()).is_generator();
71         // FIXME(welseywiser) const prop doesn't work on generators because of query cycles
72         // computing their layout.
73         if is_generator {
74             trace!("ConstProp skipped for generator {:?}", source.def_id());
75             return;
76         }
77
78         // Check if it's even possible to satisfy the 'where' clauses
79         // for this item.
80         // This branch will never be taken for any normal function.
81         // However, it's possible to `#!feature(trivial_bounds)]` to write
82         // a function with impossible to satisfy clauses, e.g.:
83         // `fn foo() where String: Copy {}`
84         //
85         // We don't usually need to worry about this kind of case,
86         // since we would get a compilation error if the user tried
87         // to call it. However, since we can do const propagation
88         // even without any calls to the function, we need to make
89         // sure that it even makes sense to try to evaluate the body.
90         // If there are unsatisfiable where clauses, then all bets are
91         // off, and we just give up.
92         //
93         // Note that we use TraitQueryMode::Canonical here, which causes
94         // us to treat overflow like any other error. This is because we
95         // are "speculatively" evaluating this item with the default substs.
96         // While this usually succeeds, it may fail with tricky impls
97         // (e.g. the typenum crate). Const-propagation is fundamentally
98         // "best-effort", and does not affect correctness in any way.
99         // Therefore, it's perfectly fine to just "give up" if we're
100         // unable to check the bounds with the default substs.
101         //
102         // False negatives (failing to run const-prop on something when we actually
103         // could) are fine. However, false positives (running const-prop on
104         // an item with unsatisfiable bounds) can lead to us generating invalid
105         // MIR.
106         if !tcx.substitute_normalize_and_test_predicates((
107             source.def_id(),
108             InternalSubsts::identity_for_item(tcx, source.def_id()),
109             TraitQueryMode::Canonical,
110         )) {
111             trace!(
112                 "ConstProp skipped for item with unsatisfiable predicates: {:?}",
113                 source.def_id()
114             );
115             return;
116         }
117
118         trace!("ConstProp starting for {:?}", source.def_id());
119
120         let dummy_body = &Body::new(
121             body.basic_blocks().clone(),
122             body.source_scopes.clone(),
123             body.local_decls.clone(),
124             Default::default(),
125             body.arg_count,
126             Default::default(),
127             tcx.def_span(source.def_id()),
128             Default::default(),
129             body.generator_kind,
130         );
131
132         // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
133         // constants, instead of just checking for const-folding succeeding.
134         // That would require an uniform one-def no-mutation analysis
135         // and RPO (or recursing when needing the value of a local).
136         let mut optimization_finder =
137             ConstPropagator::new(read_only!(body), dummy_body, tcx, source);
138         optimization_finder.visit_body(body);
139
140         trace!("ConstProp done for {:?}", source.def_id());
141     }
142 }
143
144 struct ConstPropMachine;
145
146 impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
147     type MemoryKinds = !;
148     type PointerTag = ();
149     type ExtraFnVal = !;
150
151     type FrameExtra = ();
152     type MemoryExtra = ();
153     type AllocExtra = ();
154
155     type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
156
157     const STATIC_KIND: Option<!> = None;
158
159     const CHECK_ALIGN: bool = false;
160
161     #[inline(always)]
162     fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
163         false
164     }
165
166     fn find_mir_or_eval_fn(
167         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
168         _span: Span,
169         _instance: ty::Instance<'tcx>,
170         _args: &[OpTy<'tcx>],
171         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
172         _unwind: Option<BasicBlock>,
173     ) -> InterpResult<'tcx, Option<&'mir Body<'tcx>>> {
174         Ok(None)
175     }
176
177     fn call_extra_fn(
178         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
179         fn_val: !,
180         _args: &[OpTy<'tcx>],
181         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
182         _unwind: Option<BasicBlock>,
183     ) -> InterpResult<'tcx> {
184         match fn_val {}
185     }
186
187     fn call_intrinsic(
188         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
189         _span: Span,
190         _instance: ty::Instance<'tcx>,
191         _args: &[OpTy<'tcx>],
192         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
193         _unwind: Option<BasicBlock>,
194     ) -> InterpResult<'tcx> {
195         throw_unsup!(ConstPropUnsupported("calling intrinsics isn't supported in ConstProp"));
196     }
197
198     fn assert_panic(
199         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
200         _span: Span,
201         _msg: &rustc::mir::interpret::AssertMessage<'tcx>,
202         _unwind: Option<rustc::mir::BasicBlock>,
203     ) -> InterpResult<'tcx> {
204         bug!("panics terminators are not evaluated in ConstProp");
205     }
206
207     fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
208         throw_unsup!(ConstPropUnsupported("ptr-to-int casts aren't supported in ConstProp"));
209     }
210
211     fn binary_ptr_op(
212         _ecx: &InterpCx<'mir, 'tcx, Self>,
213         _bin_op: BinOp,
214         _left: ImmTy<'tcx>,
215         _right: ImmTy<'tcx>,
216     ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
217         // We can't do this because aliasing of memory can differ between const eval and llvm
218         throw_unsup!(ConstPropUnsupported(
219             "pointer arithmetic or comparisons aren't supported \
220             in ConstProp"
221         ));
222     }
223
224     fn find_foreign_static(
225         _tcx: TyCtxt<'tcx>,
226         _def_id: DefId,
227     ) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
228         throw_unsup!(ReadForeignStatic)
229     }
230
231     #[inline(always)]
232     fn init_allocation_extra<'b>(
233         _memory_extra: &(),
234         _id: AllocId,
235         alloc: Cow<'b, Allocation>,
236         _kind: Option<MemoryKind<!>>,
237     ) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
238         // We do not use a tag so we can just cheaply forward the allocation
239         (alloc, ())
240     }
241
242     #[inline(always)]
243     fn tag_static_base_pointer(_memory_extra: &(), _id: AllocId) -> Self::PointerTag {
244         ()
245     }
246
247     fn box_alloc(
248         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
249         _dest: PlaceTy<'tcx>,
250     ) -> InterpResult<'tcx> {
251         throw_unsup!(ConstPropUnsupported("can't const prop `box` keyword"));
252     }
253
254     fn access_local(
255         _ecx: &InterpCx<'mir, 'tcx, Self>,
256         frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
257         local: Local,
258     ) -> InterpResult<'tcx, InterpOperand<Self::PointerTag>> {
259         let l = &frame.locals[local];
260
261         if l.value == LocalValue::Uninitialized {
262             throw_unsup!(ConstPropUnsupported("tried to access an uninitialized local"));
263         }
264
265         l.access()
266     }
267
268     fn before_access_static(
269         _memory_extra: &(),
270         allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
271     ) -> InterpResult<'tcx> {
272         // if the static allocation is mutable or if it has relocations (it may be legal to mutate
273         // the memory behind that in the future), then we can't const prop it
274         if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
275             throw_unsup!(ConstPropUnsupported("can't eval mutable statics in ConstProp"));
276         }
277
278         Ok(())
279     }
280
281     fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
282         Ok(())
283     }
284
285     #[inline(always)]
286     fn stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
287         Ok(())
288     }
289 }
290
291 /// Finds optimization opportunities on the MIR.
292 struct ConstPropagator<'mir, 'tcx> {
293     ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
294     tcx: TyCtxt<'tcx>,
295     source: MirSource<'tcx>,
296     can_const_prop: IndexVec<Local, ConstPropMode>,
297     param_env: ParamEnv<'tcx>,
298     // FIXME(eddyb) avoid cloning these two fields more than once,
299     // by accessing them through `ecx` instead.
300     source_scopes: IndexVec<SourceScope, SourceScopeData>,
301     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
302     ret: Option<OpTy<'tcx, ()>>,
303     // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
304     // the last known `SourceInfo` here and just keep revisiting it.
305     source_info: Option<SourceInfo>,
306 }
307
308 impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
309     type Ty = Ty<'tcx>;
310     type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
311
312     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
313         self.tcx.layout_of(self.param_env.and(ty))
314     }
315 }
316
317 impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
318     #[inline]
319     fn data_layout(&self) -> &TargetDataLayout {
320         &self.tcx.data_layout
321     }
322 }
323
324 impl<'mir, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'mir, 'tcx> {
325     #[inline]
326     fn tcx(&self) -> TyCtxt<'tcx> {
327         self.tcx
328     }
329 }
330
331 impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
332     fn new(
333         body: ReadOnlyBodyAndCache<'_, 'tcx>,
334         dummy_body: &'mir Body<'tcx>,
335         tcx: TyCtxt<'tcx>,
336         source: MirSource<'tcx>,
337     ) -> ConstPropagator<'mir, 'tcx> {
338         let def_id = source.def_id();
339         let substs = &InternalSubsts::identity_for_item(tcx, def_id);
340         let mut param_env = tcx.param_env(def_id);
341
342         // If we're evaluating inside a monomorphic function, then use `Reveal::All` because
343         // we want to see the same instances that codegen will see. This allows us to `resolve()`
344         // specializations.
345         if !substs.needs_subst() {
346             param_env = param_env.with_reveal_all();
347         }
348
349         let span = tcx.def_span(def_id);
350         let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
351         let can_const_prop = CanConstProp::check(body);
352
353         let ret = ecx
354             .layout_of(body.return_ty().subst(tcx, substs))
355             .ok()
356             // Don't bother allocating memory for ZST types which have no values
357             // or for large values.
358             .filter(|ret_layout| {
359                 !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
360             })
361             .map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
362
363         ecx.push_stack_frame(
364             Instance::new(def_id, substs),
365             span,
366             dummy_body,
367             ret.map(Into::into),
368             StackPopCleanup::None { cleanup: false },
369         )
370         .expect("failed to push initial stack frame");
371
372         ConstPropagator {
373             ecx,
374             tcx,
375             source,
376             param_env,
377             can_const_prop,
378             // FIXME(eddyb) avoid cloning these two fields more than once,
379             // by accessing them through `ecx` instead.
380             source_scopes: body.source_scopes.clone(),
381             //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
382             local_decls: body.local_decls.clone(),
383             ret: ret.map(Into::into),
384             source_info: None,
385         }
386     }
387
388     fn get_const(&self, local: Local) -> Option<OpTy<'tcx>> {
389         if local == RETURN_PLACE {
390             // Try to read the return place as an immediate so that if it is representable as a
391             // scalar, we can handle it as such, but otherwise, just return the value as is.
392             return match self.ret.map(|ret| self.ecx.try_read_immediate(ret)) {
393                 Some(Ok(Ok(imm))) => Some(imm.into()),
394                 _ => self.ret,
395             };
396         }
397
398         self.ecx.access_local(self.ecx.frame(), local, None).ok()
399     }
400
401     fn remove_const(&mut self, local: Local) {
402         self.ecx.frame_mut().locals[local] =
403             LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
404     }
405
406     fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
407         match &self.source_scopes[source_info.scope].local_data {
408             ClearCrossCrate::Set(data) => Some(data.lint_root),
409             ClearCrossCrate::Clear => None,
410         }
411     }
412
413     fn use_ecx<F, T>(&mut self, source_info: SourceInfo, f: F) -> Option<T>
414     where
415         F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
416     {
417         self.ecx.tcx.span = source_info.span;
418         // FIXME(eddyb) move this to the `Panic(_)` error case, so that
419         // `f(self)` is always called, and that the only difference when the
420         // scope's `local_data` is missing, is that the lint isn't emitted.
421         let lint_root = self.lint_root(source_info)?;
422         let r = match f(self) {
423             Ok(val) => Some(val),
424             Err(error) => {
425                 use rustc::mir::interpret::{
426                     InterpError::*, UndefinedBehaviorInfo, UnsupportedOpInfo,
427                 };
428                 match error.kind {
429                     MachineStop(_) => bug!("ConstProp does not stop"),
430
431                     // Some error shouldn't come up because creating them causes
432                     // an allocation, which we should avoid. When that happens,
433                     // dedicated error variants should be introduced instead.
434                     // Only test this in debug builds though to avoid disruptions.
435                     Unsupported(UnsupportedOpInfo::Unsupported(_))
436                     | Unsupported(UnsupportedOpInfo::ValidationFailure(_))
437                     | UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
438                     | UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_))
439                         if cfg!(debug_assertions) =>
440                     {
441                         bug!("const-prop encountered allocating error: {:?}", error.kind);
442                     }
443
444                     Unsupported(_)
445                     | UndefinedBehavior(_)
446                     | InvalidProgram(_)
447                     | ResourceExhaustion(_) => {
448                         // Ignore these errors.
449                     }
450                     Panic(_) => {
451                         let diagnostic = error_to_const_error(&self.ecx, error);
452                         diagnostic.report_as_lint(
453                             self.ecx.tcx,
454                             "this expression will panic at runtime",
455                             lint_root,
456                             None,
457                         );
458                     }
459                 }
460                 None
461             }
462         };
463         self.ecx.tcx.span = DUMMY_SP;
464         r
465     }
466
467     fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
468         self.ecx.tcx.span = c.span;
469
470         // FIXME we need to revisit this for #67176
471         if c.needs_subst() {
472             return None;
473         }
474
475         match self.ecx.eval_const_to_op(c.literal, None) {
476             Ok(op) => Some(op),
477             Err(error) => {
478                 let err = error_to_const_error(&self.ecx, error);
479                 if let Some(lint_root) = self.lint_root(source_info) {
480                     let lint_only = match c.literal.val {
481                         // Promoteds must lint and not error as the user didn't ask for them
482                         ConstKind::Unevaluated(_, _, Some(_)) => true,
483                         // Out of backwards compatibility we cannot report hard errors in unused
484                         // generic functions using associated constants of the generic parameters.
485                         _ => c.literal.needs_subst(),
486                     };
487                     if lint_only {
488                         // Out of backwards compatibility we cannot report hard errors in unused
489                         // generic functions using associated constants of the generic parameters.
490                         err.report_as_lint(
491                             self.ecx.tcx,
492                             "erroneous constant used",
493                             lint_root,
494                             Some(c.span),
495                         );
496                     } else {
497                         err.report_as_error(self.ecx.tcx, "erroneous constant used");
498                     }
499                 } else {
500                     err.report_as_error(self.ecx.tcx, "erroneous constant used");
501                 }
502                 None
503             }
504         }
505     }
506
507     fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
508         trace!("eval_place(place={:?})", place);
509         self.use_ecx(source_info, |this| this.ecx.eval_place_to_op(place, None))
510     }
511
512     fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
513         match *op {
514             Operand::Constant(ref c) => self.eval_constant(c, source_info),
515             Operand::Move(ref place) | Operand::Copy(ref place) => {
516                 self.eval_place(place, source_info)
517             }
518         }
519     }
520
521     fn check_unary_op(&mut self, arg: &Operand<'tcx>, source_info: SourceInfo) -> Option<()> {
522         self.use_ecx(source_info, |this| {
523             let ty = arg.ty(&this.local_decls, this.tcx);
524
525             if ty.is_integral() {
526                 let arg = this.ecx.eval_operand(arg, None)?;
527                 let prim = this.ecx.read_immediate(arg)?;
528                 // Need to do overflow check here: For actual CTFE, MIR
529                 // generation emits code that does this before calling the op.
530                 if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
531                     throw_panic!(OverflowNeg)
532                 }
533             }
534
535             Ok(())
536         })?;
537
538         Some(())
539     }
540
541     fn check_binary_op(
542         &mut self,
543         op: BinOp,
544         left: &Operand<'tcx>,
545         right: &Operand<'tcx>,
546         source_info: SourceInfo,
547         place_layout: TyLayout<'tcx>,
548         overflow_check: bool,
549     ) -> Option<()> {
550         let r = self.use_ecx(source_info, |this| {
551             this.ecx.read_immediate(this.ecx.eval_operand(right, None)?)
552         })?;
553         if op == BinOp::Shr || op == BinOp::Shl {
554             let left_bits = place_layout.size.bits();
555             let right_size = r.layout.size;
556             let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
557             if r_bits.map_or(false, |b| b >= left_bits as u128) {
558                 let lint_root = self.lint_root(source_info)?;
559                 let dir = if op == BinOp::Shr { "right" } else { "left" };
560                 self.tcx.lint_hir(
561                     ::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
562                     lint_root,
563                     source_info.span,
564                     &format!("attempt to shift {} with overflow", dir),
565                 );
566                 return None;
567             }
568         }
569
570         // If overflow checking is enabled (like in debug mode by default),
571         // then we'll already catch overflow when we evaluate the `Assert` statement
572         // in MIR. However, if overflow checking is disabled, then there won't be any
573         // `Assert` statement and so we have to do additional checking here.
574         if !overflow_check {
575             self.use_ecx(source_info, |this| {
576                 let l = this.ecx.read_immediate(this.ecx.eval_operand(left, None)?)?;
577                 let (_, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
578
579                 if overflow {
580                     let err = err_panic!(Overflow(op)).into();
581                     return Err(err);
582                 }
583
584                 Ok(())
585             })?;
586         }
587
588         Some(())
589     }
590
591     fn const_prop(
592         &mut self,
593         rvalue: &Rvalue<'tcx>,
594         place_layout: TyLayout<'tcx>,
595         source_info: SourceInfo,
596         place: &Place<'tcx>,
597     ) -> Option<()> {
598         // #66397: Don't try to eval into large places as that can cause an OOM
599         if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
600             return None;
601         }
602
603         // FIXME we need to revisit this for #67176
604         if rvalue.needs_subst() {
605             return None;
606         }
607
608         let overflow_check = self.tcx.sess.overflow_checks();
609
610         // Perform any special handling for specific Rvalue types.
611         // Generally, checks here fall into one of two categories:
612         //   1. Additional checking to provide useful lints to the user
613         //        - In this case, we will do some validation and then fall through to the
614         //          end of the function which evals the assignment.
615         //   2. Working around bugs in other parts of the compiler
616         //        - In this case, we'll return `None` from this function to stop evaluation.
617         match rvalue {
618             // Additional checking: if overflow checks are disabled (which is usually the case in
619             // release mode), then we need to do additional checking here to give lints to the user
620             // if an overflow would occur.
621             Rvalue::UnaryOp(UnOp::Neg, arg) if !overflow_check => {
622                 trace!("checking UnaryOp(op = Neg, arg = {:?})", arg);
623                 self.check_unary_op(arg, source_info)?;
624             }
625
626             // Additional checking: check for overflows on integer binary operations and report
627             // them to the user as lints.
628             Rvalue::BinaryOp(op, left, right) => {
629                 trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
630                 self.check_binary_op(*op, left, right, source_info, place_layout, overflow_check)?;
631             }
632
633             // Do not try creating references (#67862)
634             Rvalue::Ref(_, _, place_ref) => {
635                 trace!("skipping Ref({:?})", place_ref);
636
637                 return None;
638             }
639
640             _ => {}
641         }
642
643         self.use_ecx(source_info, |this| {
644             trace!("calling eval_rvalue_into_place(rvalue = {:?}, place = {:?})", rvalue, place);
645             this.ecx.eval_rvalue_into_place(rvalue, place)?;
646             Ok(())
647         })
648     }
649
650     fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
651         Operand::Constant(Box::new(Constant {
652             span,
653             user_ty: None,
654             literal: self.tcx.mk_const(*ty::Const::from_scalar(self.tcx, scalar, ty)),
655         }))
656     }
657
658     fn replace_with_const(
659         &mut self,
660         rval: &mut Rvalue<'tcx>,
661         value: OpTy<'tcx>,
662         source_info: SourceInfo,
663     ) {
664         trace!("attepting to replace {:?} with {:?}", rval, value);
665         if let Err(e) = self.ecx.validate_operand(
666             value,
667             vec![],
668             // FIXME: is ref tracking too expensive?
669             Some(&mut interpret::RefTracking::empty()),
670         ) {
671             trace!("validation error, attempt failed: {:?}", e);
672             return;
673         }
674
675         // FIXME> figure out what tho do when try_read_immediate fails
676         let imm = self.use_ecx(source_info, |this| this.ecx.try_read_immediate(value));
677
678         if let Some(Ok(imm)) = imm {
679             match *imm {
680                 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
681                     *rval = Rvalue::Use(self.operand_from_scalar(
682                         scalar,
683                         value.layout.ty,
684                         source_info.span,
685                     ));
686                 }
687                 Immediate::ScalarPair(
688                     ScalarMaybeUndef::Scalar(one),
689                     ScalarMaybeUndef::Scalar(two),
690                 ) => {
691                     // Found a value represented as a pair. For now only do cont-prop if type of
692                     // Rvalue is also a pair with two scalars. The more general case is more
693                     // complicated to implement so we'll do it later.
694                     let ty = &value.layout.ty.kind;
695                     // Only do it for tuples
696                     if let ty::Tuple(substs) = ty {
697                         // Only do it if tuple is also a pair with two scalars
698                         if substs.len() == 2 {
699                             let opt_ty1_ty2 = self.use_ecx(source_info, |this| {
700                                 let ty1 = substs[0].expect_ty();
701                                 let ty2 = substs[1].expect_ty();
702                                 let ty_is_scalar = |ty| {
703                                     this.ecx.layout_of(ty).ok().map(|ty| ty.details.abi.is_scalar())
704                                         == Some(true)
705                                 };
706                                 if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
707                                     Ok(Some((ty1, ty2)))
708                                 } else {
709                                     Ok(None)
710                                 }
711                             });
712
713                             if let Some(Some((ty1, ty2))) = opt_ty1_ty2 {
714                                 *rval = Rvalue::Aggregate(
715                                     Box::new(AggregateKind::Tuple),
716                                     vec![
717                                         self.operand_from_scalar(one, ty1, source_info.span),
718                                         self.operand_from_scalar(two, ty2, source_info.span),
719                                     ],
720                                 );
721                             }
722                         }
723                     }
724                 }
725                 _ => {}
726             }
727         }
728     }
729
730     fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
731         let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
732
733         if mir_opt_level == 0 {
734             return false;
735         }
736
737         match *op {
738             interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) => {
739                 s.is_bits()
740             }
741             interpret::Operand::Immediate(Immediate::ScalarPair(
742                 ScalarMaybeUndef::Scalar(l),
743                 ScalarMaybeUndef::Scalar(r),
744             )) => l.is_bits() && r.is_bits(),
745             interpret::Operand::Indirect(_) if mir_opt_level >= 2 => {
746                 let mplace = op.assert_mem_place(&self.ecx);
747                 intern_const_alloc_recursive(&mut self.ecx, InternKind::ConstProp, mplace, false)
748                     .expect("failed to intern alloc");
749                 true
750             }
751             _ => false,
752         }
753     }
754 }
755
756 /// The mode that `ConstProp` is allowed to run in for a given `Local`.
757 #[derive(Clone, Copy, Debug, PartialEq)]
758 enum ConstPropMode {
759     /// The `Local` can be propagated into and reads of this `Local` can also be propagated.
760     FullConstProp,
761     /// The `Local` can be propagated into but reads cannot be propagated.
762     OnlyPropagateInto,
763     /// No propagation is allowed at all.
764     NoPropagation,
765 }
766
767 struct CanConstProp {
768     can_const_prop: IndexVec<Local, ConstPropMode>,
769     // false at the beginning, once set, there are not allowed to be any more assignments
770     found_assignment: IndexVec<Local, bool>,
771 }
772
773 impl CanConstProp {
774     /// returns true if `local` can be propagated
775     fn check(body: ReadOnlyBodyAndCache<'_, '_>) -> IndexVec<Local, ConstPropMode> {
776         let mut cpv = CanConstProp {
777             can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
778             found_assignment: IndexVec::from_elem(false, &body.local_decls),
779         };
780         for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
781             // cannot use args at all
782             // cannot use locals because if x < y { y - x } else { x - y } would
783             //        lint for x != y
784             // FIXME(oli-obk): lint variables until they are used in a condition
785             // FIXME(oli-obk): lint if return value is constant
786             let local_kind = body.local_kind(local);
787
788             if local_kind == LocalKind::Arg || local_kind == LocalKind::Var {
789                 *val = ConstPropMode::OnlyPropagateInto;
790                 trace!("local {:?} can't be const propagated because it's not a temporary", local);
791             }
792         }
793         cpv.visit_body(body);
794         cpv.can_const_prop
795     }
796 }
797
798 impl<'tcx> Visitor<'tcx> for CanConstProp {
799     fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
800         use rustc::mir::visit::PlaceContext::*;
801         match context {
802             // Constants must have at most one write
803             // FIXME(oli-obk): we could be more powerful here, if the multiple writes
804             // only occur in independent execution paths
805             MutatingUse(MutatingUseContext::Store) => {
806                 if self.found_assignment[local] {
807                     trace!("local {:?} can't be propagated because of multiple assignments", local);
808                     self.can_const_prop[local] = ConstPropMode::NoPropagation;
809                 } else {
810                     self.found_assignment[local] = true
811                 }
812             }
813             // Reading constants is allowed an arbitrary number of times
814             NonMutatingUse(NonMutatingUseContext::Copy)
815             | NonMutatingUse(NonMutatingUseContext::Move)
816             | NonMutatingUse(NonMutatingUseContext::Inspect)
817             | NonMutatingUse(NonMutatingUseContext::Projection)
818             | MutatingUse(MutatingUseContext::Projection)
819             | NonUse(_) => {}
820             _ => {
821                 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
822                 self.can_const_prop[local] = ConstPropMode::NoPropagation;
823             }
824         }
825     }
826 }
827
828 impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
829     fn tcx(&self) -> TyCtxt<'tcx> {
830         self.tcx
831     }
832
833     fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
834         trace!("visit_constant: {:?}", constant);
835         self.super_constant(constant, location);
836         self.eval_constant(constant, self.source_info.unwrap());
837     }
838
839     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
840         trace!("visit_statement: {:?}", statement);
841         let source_info = statement.source_info;
842         self.source_info = Some(source_info);
843         if let StatementKind::Assign(box (ref place, ref mut rval)) = statement.kind {
844             let place_ty: Ty<'tcx> = place.ty(&self.local_decls, self.tcx).ty;
845             if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
846                 if let Some(local) = place.as_local() {
847                     let can_const_prop = self.can_const_prop[local];
848                     if let Some(()) = self.const_prop(rval, place_layout, source_info, place) {
849                         if can_const_prop == ConstPropMode::FullConstProp
850                             || can_const_prop == ConstPropMode::OnlyPropagateInto
851                         {
852                             if let Some(value) = self.get_const(local) {
853                                 if self.should_const_prop(value) {
854                                     trace!("replacing {:?} with {:?}", rval, value);
855                                     self.replace_with_const(rval, value, statement.source_info);
856
857                                     if can_const_prop == ConstPropMode::FullConstProp {
858                                         trace!("propagated into {:?}", local);
859                                     }
860                                 }
861                             }
862                         }
863                     }
864                     if self.can_const_prop[local] != ConstPropMode::FullConstProp {
865                         trace!("can't propagate into {:?}", local);
866                         if local != RETURN_PLACE {
867                             self.remove_const(local);
868                         }
869                     }
870                 }
871             }
872         } else {
873             match statement.kind {
874                 StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
875                     let frame = self.ecx.frame_mut();
876                     frame.locals[local].value =
877                         if let StatementKind::StorageLive(_) = statement.kind {
878                             LocalValue::Uninitialized
879                         } else {
880                             LocalValue::Dead
881                         };
882                 }
883                 _ => {}
884             }
885         }
886
887         self.super_statement(statement, location);
888     }
889
890     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
891         let source_info = terminator.source_info;
892         self.source_info = Some(source_info);
893         self.super_terminator(terminator, location);
894         match &mut terminator.kind {
895             TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
896                 if let Some(value) = self.eval_operand(&cond, source_info) {
897                     trace!("assertion on {:?} should be {:?}", value, expected);
898                     let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
899                     let value_const = self.ecx.read_scalar(value).unwrap();
900                     if expected != value_const {
901                         // poison all places this operand references so that further code
902                         // doesn't use the invalid value
903                         match cond {
904                             Operand::Move(ref place) | Operand::Copy(ref place) => {
905                                 self.remove_const(place.local);
906                             }
907                             Operand::Constant(_) => {}
908                         }
909                         let span = terminator.source_info.span;
910                         let hir_id = self
911                             .tcx
912                             .hir()
913                             .as_local_hir_id(self.source.def_id())
914                             .expect("some part of a failing const eval must be local");
915                         let msg = match msg {
916                             PanicInfo::Overflow(_)
917                             | PanicInfo::OverflowNeg
918                             | PanicInfo::DivisionByZero
919                             | PanicInfo::RemainderByZero => msg.description().to_owned(),
920                             PanicInfo::BoundsCheck { ref len, ref index } => {
921                                 let len =
922                                     self.eval_operand(len, source_info).expect("len must be const");
923                                 let len = match self.ecx.read_scalar(len) {
924                                     Ok(ScalarMaybeUndef::Scalar(Scalar::Raw { data, .. })) => data,
925                                     other => bug!("const len not primitive: {:?}", other),
926                                 };
927                                 let index = self
928                                     .eval_operand(index, source_info)
929                                     .expect("index must be const");
930                                 let index = match self.ecx.read_scalar(index) {
931                                     Ok(ScalarMaybeUndef::Scalar(Scalar::Raw { data, .. })) => data,
932                                     other => bug!("const index not primitive: {:?}", other),
933                                 };
934                                 format!(
935                                     "index out of bounds: \
936                                     the len is {} but the index is {}",
937                                     len, index,
938                                 )
939                             }
940                             // Need proper const propagator for these
941                             _ => return,
942                         };
943                         self.tcx.lint_hir(::rustc::lint::builtin::CONST_ERR, hir_id, span, &msg);
944                     } else {
945                         if self.should_const_prop(value) {
946                             if let ScalarMaybeUndef::Scalar(scalar) = value_const {
947                                 *cond = self.operand_from_scalar(
948                                     scalar,
949                                     self.tcx.types.bool,
950                                     source_info.span,
951                                 );
952                             }
953                         }
954                     }
955                 }
956             }
957             TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
958                 if let Some(value) = self.eval_operand(&discr, source_info) {
959                     if self.should_const_prop(value) {
960                         if let ScalarMaybeUndef::Scalar(scalar) =
961                             self.ecx.read_scalar(value).unwrap()
962                         {
963                             *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
964                         }
965                     }
966                 }
967             }
968             //none of these have Operands to const-propagate
969             TerminatorKind::Goto { .. }
970             | TerminatorKind::Resume
971             | TerminatorKind::Abort
972             | TerminatorKind::Return
973             | TerminatorKind::Unreachable
974             | TerminatorKind::Drop { .. }
975             | TerminatorKind::DropAndReplace { .. }
976             | TerminatorKind::Yield { .. }
977             | TerminatorKind::GeneratorDrop
978             | TerminatorKind::FalseEdges { .. }
979             | TerminatorKind::FalseUnwind { .. } => {}
980             //FIXME(wesleywiser) Call does have Operands that could be const-propagated
981             TerminatorKind::Call { .. } => {}
982         }
983     }
984 }