]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/const_prop.rs
8e7093b833d441e3f678005b2e079a0ddd769593
[rust.git] / src / librustc_mir / transform / const_prop.rs
1 //! Propagates constants for early reporting of statically known
2 //! assertion failures
3
4 use rustc::hir::def::DefKind;
5 use rustc::mir::{
6     AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue,
7     Local, NullOp, UnOp, StatementKind, Statement, LocalKind, Static, StaticKind,
8     TerminatorKind, Terminator,  ClearCrossCrate, SourceInfo, BinOp, ProjectionElem,
9     SourceScope, SourceScopeLocalData, LocalDecl, Promoted,
10 };
11 use rustc::mir::visit::{
12     Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
13 };
14 use rustc::mir::interpret::{InterpError, Scalar, GlobalId, EvalResult};
15 use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
16 use syntax_pos::{Span, DUMMY_SP};
17 use rustc::ty::subst::InternalSubsts;
18 use rustc_data_structures::indexed_vec::IndexVec;
19 use rustc::ty::layout::{
20     LayoutOf, TyLayout, LayoutError,
21     HasTyCtxt, TargetDataLayout, HasDataLayout,
22 };
23
24 use crate::interpret::{
25     self, InterpretCx, ScalarMaybeUndef, Immediate, OpTy, ImmTy, MemoryKind,
26 };
27 use crate::const_eval::{
28     CompileTimeInterpreter, error_to_const_error, eval_promoted, mk_eval_cx,
29 };
30 use crate::transform::{MirPass, MirSource};
31
32 pub struct ConstProp;
33
34 impl MirPass for ConstProp {
35     fn run_pass<'a, 'tcx>(&self,
36                           tcx: TyCtxt<'a, 'tcx, 'tcx>,
37                           source: MirSource<'tcx>,
38                           mir: &mut Body<'tcx>) {
39         // will be evaluated by miri and produce its errors there
40         if source.promoted.is_some() {
41             return;
42         }
43
44         use rustc::hir::map::blocks::FnLikeNode;
45         let hir_id = tcx.hir().as_local_hir_id(source.def_id())
46                               .expect("Non-local call to local provider is_const_fn");
47
48         let is_fn_like = FnLikeNode::from_node(tcx.hir().get_by_hir_id(hir_id)).is_some();
49         let is_assoc_const = match tcx.def_kind(source.def_id()) {
50             Some(DefKind::AssocConst) => true,
51             _ => false,
52         };
53
54         // Only run const prop on functions, methods, closures and associated constants
55         if !is_fn_like && !is_assoc_const  {
56             // skip anon_const/statics/consts because they'll be evaluated by miri anyway
57             trace!("ConstProp skipped for {:?}", source.def_id());
58             return
59         }
60
61         trace!("ConstProp starting for {:?}", source.def_id());
62
63         // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
64         // constants, instead of just checking for const-folding succeeding.
65         // That would require an uniform one-def no-mutation analysis
66         // and RPO (or recursing when needing the value of a local).
67         let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
68         optimization_finder.visit_body(mir);
69
70         // put back the data we stole from `mir`
71         std::mem::replace(
72             &mut mir.source_scope_local_data,
73             optimization_finder.source_scope_local_data
74         );
75         std::mem::replace(
76             &mut mir.promoted,
77             optimization_finder.promoted
78         );
79
80         trace!("ConstProp done for {:?}", source.def_id());
81     }
82 }
83
84 type Const<'tcx> = OpTy<'tcx>;
85
86 /// Finds optimization opportunities on the MIR.
87 struct ConstPropagator<'a, 'mir, 'tcx:'a+'mir> {
88     ecx: InterpretCx<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
89     tcx: TyCtxt<'a, 'tcx, 'tcx>,
90     source: MirSource<'tcx>,
91     places: IndexVec<Local, Option<Const<'tcx>>>,
92     can_const_prop: IndexVec<Local, bool>,
93     param_env: ParamEnv<'tcx>,
94     source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
95     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
96     promoted: IndexVec<Promoted, Body<'tcx>>,
97 }
98
99 impl<'a, 'b, 'tcx> LayoutOf for ConstPropagator<'a, 'b, 'tcx> {
100     type Ty = Ty<'tcx>;
101     type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
102
103     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
104         self.tcx.layout_of(self.param_env.and(ty))
105     }
106 }
107
108 impl<'a, 'b, 'tcx> HasDataLayout for ConstPropagator<'a, 'b, 'tcx> {
109     #[inline]
110     fn data_layout(&self) -> &TargetDataLayout {
111         &self.tcx.data_layout
112     }
113 }
114
115 impl<'a, 'b, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'a, 'b, 'tcx> {
116     #[inline]
117     fn tcx<'c>(&'c self) -> TyCtxt<'c, 'tcx, 'tcx> {
118         self.tcx
119     }
120 }
121
122 impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
123     fn new(
124         mir: &mut Body<'tcx>,
125         tcx: TyCtxt<'a, 'tcx, 'tcx>,
126         source: MirSource<'tcx>,
127     ) -> ConstPropagator<'a, 'mir, 'tcx> {
128         let param_env = tcx.param_env(source.def_id());
129         let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id()), param_env);
130         let can_const_prop = CanConstProp::check(mir);
131         let source_scope_local_data = std::mem::replace(
132             &mut mir.source_scope_local_data,
133             ClearCrossCrate::Clear
134         );
135         let promoted = std::mem::replace(
136             &mut mir.promoted,
137             IndexVec::new()
138         );
139
140         ConstPropagator {
141             ecx,
142             tcx,
143             source,
144             param_env,
145             can_const_prop,
146             places: IndexVec::from_elem(None, &mir.local_decls),
147             source_scope_local_data,
148             //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
149             local_decls: mir.local_decls.clone(),
150             promoted,
151         }
152     }
153
154     fn use_ecx<F, T>(
155         &mut self,
156         source_info: SourceInfo,
157         f: F
158     ) -> Option<T>
159     where
160         F: FnOnce(&mut Self) -> EvalResult<'tcx, T>,
161     {
162         self.ecx.tcx.span = source_info.span;
163         let lint_root = match self.source_scope_local_data {
164             ClearCrossCrate::Set(ref ivs) => {
165                 //FIXME(#51314): remove this check
166                 if source_info.scope.index() >= ivs.len() {
167                     return None;
168                 }
169                 ivs[source_info.scope].lint_root
170             },
171             ClearCrossCrate::Clear => return None,
172         };
173         let r = match f(self) {
174             Ok(val) => Some(val),
175             Err(error) => {
176                 let diagnostic = error_to_const_error(&self.ecx, error);
177                 use rustc::mir::interpret::InterpError::*;
178                 match diagnostic.error {
179                     // don't report these, they make no sense in a const prop context
180                     | MachineError(_)
181                     | Exit(_)
182                     // at runtime these transformations might make sense
183                     // FIXME: figure out the rules and start linting
184                     | FunctionAbiMismatch(..)
185                     | FunctionArgMismatch(..)
186                     | FunctionRetMismatch(..)
187                     | FunctionArgCountMismatch
188                     // fine at runtime, might be a register address or sth
189                     | ReadBytesAsPointer
190                     // fine at runtime
191                     | ReadForeignStatic
192                     | Unimplemented(_)
193                     // don't report const evaluator limits
194                     | StackFrameLimitReached
195                     | NoMirFor(..)
196                     | InlineAsm
197                     => {},
198
199                     | InvalidMemoryAccess
200                     | DanglingPointerDeref
201                     | DoubleFree
202                     | InvalidFunctionPointer
203                     | InvalidBool
204                     | InvalidDiscriminant(..)
205                     | PointerOutOfBounds { .. }
206                     | InvalidNullPointerUsage
207                     | ValidationFailure(..)
208                     | InvalidPointerMath
209                     | ReadUndefBytes(_)
210                     | DeadLocal
211                     | InvalidBoolOp(_)
212                     | DerefFunctionPointer
213                     | ExecuteMemory
214                     | Intrinsic(..)
215                     | InvalidChar(..)
216                     | AbiViolation(_)
217                     | AlignmentCheckFailed{..}
218                     | CalledClosureAsFunction
219                     | VtableForArgumentlessMethod
220                     | ModifiedConstantMemory
221                     | ModifiedStatic
222                     | AssumptionNotHeld
223                     // FIXME: should probably be removed and turned into a bug! call
224                     | TypeNotPrimitive(_)
225                     | ReallocatedWrongMemoryKind(_, _)
226                     | DeallocatedWrongMemoryKind(_, _)
227                     | ReallocateNonBasePtr
228                     | DeallocateNonBasePtr
229                     | IncorrectAllocationInformation(..)
230                     | UnterminatedCString(_)
231                     | HeapAllocZeroBytes
232                     | HeapAllocNonPowerOfTwoAlignment(_)
233                     | Unreachable
234                     | ReadFromReturnPointer
235                     | GeneratorResumedAfterReturn
236                     | GeneratorResumedAfterPanic
237                     | ReferencedConstant
238                     | InfiniteLoop
239                     => {
240                         // FIXME: report UB here
241                     },
242
243                     | OutOfTls
244                     | TlsOutOfBounds
245                     | PathNotFound(_)
246                     => bug!("these should not be in rustc, but in miri's machine errors"),
247
248                     | Layout(_)
249                     | UnimplementedTraitSelection
250                     | TypeckError
251                     | TooGeneric
252                     // these are just noise
253                     => {},
254
255                     // non deterministic
256                     | ReadPointerAsBytes
257                     // FIXME: implement
258                     => {},
259
260                     | Panic { .. }
261                     | BoundsCheck{..}
262                     | Overflow(_)
263                     | OverflowNeg
264                     | DivisionByZero
265                     | RemainderByZero
266                     => {
267                         diagnostic.report_as_lint(
268                             self.ecx.tcx,
269                             "this expression will panic at runtime",
270                             lint_root,
271                             None,
272                         );
273                     }
274                 }
275                 None
276             },
277         };
278         self.ecx.tcx.span = DUMMY_SP;
279         r
280     }
281
282     fn eval_constant(
283         &mut self,
284         c: &Constant<'tcx>,
285     ) -> Option<Const<'tcx>> {
286         self.ecx.tcx.span = c.span;
287         match self.ecx.eval_const_to_op(c.literal, None) {
288             Ok(op) => {
289                 Some(op)
290             },
291             Err(error) => {
292                 let err = error_to_const_error(&self.ecx, error);
293                 err.report_as_error(self.ecx.tcx, "erroneous constant used");
294                 None
295             },
296         }
297     }
298
299     fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
300         trace!("eval_place(place={:?})", place);
301         place.iterate(|place_base, place_projection| {
302             let mut eval = match place_base {
303                 PlaceBase::Local(loc) => self.places[*loc].clone()?,
304                 PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted), ..}) => {
305                     let generics = self.tcx.generics_of(self.source.def_id());
306                     if generics.requires_monomorphization(self.tcx) {
307                         // FIXME: can't handle code with generics
308                         return None;
309                     }
310                     let substs = InternalSubsts::identity_for_item(self.tcx, self.source.def_id());
311                     let instance = Instance::new(self.source.def_id(), substs);
312                     let cid = GlobalId {
313                         instance,
314                         promoted: Some(*promoted),
315                     };
316                     // cannot use `const_eval` here, because that would require having the MIR
317                     // for the current function available, but we're producing said MIR right now
318                     let res = self.use_ecx(source_info, |this| {
319                         let mir = &this.promoted[*promoted];
320                         eval_promoted(this.tcx, cid, mir, this.param_env)
321                     })?;
322                     trace!("evaluated promoted {:?} to {:?}", promoted, res);
323                     res.into()
324                 }
325                 _ => return None,
326             };
327
328             for proj in place_projection {
329                 match proj.elem {
330                     ProjectionElem::Field(field, _) => {
331                         trace!("field proj on {:?}", proj.base);
332                         eval = self.use_ecx(source_info, |this| {
333                             this.ecx.operand_field(eval, field.index() as u64)
334                         })?;
335                     },
336                     // We could get more projections by using e.g., `operand_projection`,
337                     // but we do not even have the stack frame set up properly so
338                     // an `Index` projection would throw us off-track.
339                     _ => return None,
340                 }
341             }
342
343             Some(eval)
344         })
345     }
346
347     fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
348         match *op {
349             Operand::Constant(ref c) => self.eval_constant(c),
350             | Operand::Move(ref place)
351             | Operand::Copy(ref place) => self.eval_place(place, source_info),
352         }
353     }
354
355     fn const_prop(
356         &mut self,
357         rvalue: &Rvalue<'tcx>,
358         place_layout: TyLayout<'tcx>,
359         source_info: SourceInfo,
360     ) -> Option<Const<'tcx>> {
361         let span = source_info.span;
362         match *rvalue {
363             Rvalue::Use(ref op) => {
364                 self.eval_operand(op, source_info)
365             },
366             Rvalue::Ref(_, _, ref place) => {
367                 let src = self.eval_place(place, source_info)?;
368                 let mplace = src.try_as_mplace().ok()?;
369                 Some(ImmTy::from_scalar(mplace.ptr.into(), place_layout).into())
370             },
371             Rvalue::Repeat(..) |
372             Rvalue::Aggregate(..) |
373             Rvalue::NullaryOp(NullOp::Box, _) |
374             Rvalue::Discriminant(..) => None,
375
376             Rvalue::Cast(kind, ref operand, _) => {
377                 let op = self.eval_operand(operand, source_info)?;
378                 self.use_ecx(source_info, |this| {
379                     let dest = this.ecx.allocate(place_layout, MemoryKind::Stack);
380                     this.ecx.cast(op, kind, dest.into())?;
381                     Ok(dest.into())
382                 })
383             }
384
385             // FIXME(oli-obk): evaluate static/constant slice lengths
386             Rvalue::Len(_) => None,
387             Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
388                 type_size_of(self.tcx, self.param_env, ty).and_then(|n| Some(
389                     ImmTy {
390                         imm: Immediate::Scalar(
391                             Scalar::from_uint(n, self.tcx.data_layout.pointer_size).into()
392                         ),
393                         layout: self.tcx.layout_of(self.param_env.and(self.tcx.types.usize)).ok()?,
394                     }.into()
395                 ))
396             }
397             Rvalue::UnaryOp(op, ref arg) => {
398                 let def_id = if self.tcx.is_closure(self.source.def_id()) {
399                     self.tcx.closure_base_def_id(self.source.def_id())
400                 } else {
401                     self.source.def_id()
402                 };
403                 let generics = self.tcx.generics_of(def_id);
404                 if generics.requires_monomorphization(self.tcx) {
405                     // FIXME: can't handle code with generics
406                     return None;
407                 }
408
409                 let arg = self.eval_operand(arg, source_info)?;
410                 let val = self.use_ecx(source_info, |this| {
411                     let prim = this.ecx.read_immediate(arg)?;
412                     match op {
413                         UnOp::Neg => {
414                             // Need to do overflow check here: For actual CTFE, MIR
415                             // generation emits code that does this before calling the op.
416                             if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
417                                 return err!(OverflowNeg);
418                             }
419                         }
420                         UnOp::Not => {
421                             // Cannot overflow
422                         }
423                     }
424                     // Now run the actual operation.
425                     this.ecx.unary_op(op, prim)
426                 })?;
427                 let res = ImmTy {
428                     imm: Immediate::Scalar(val.into()),
429                     layout: place_layout,
430                 };
431                 Some(res.into())
432             }
433             Rvalue::CheckedBinaryOp(op, ref left, ref right) |
434             Rvalue::BinaryOp(op, ref left, ref right) => {
435                 trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
436                 let right = self.eval_operand(right, source_info)?;
437                 let def_id = if self.tcx.is_closure(self.source.def_id()) {
438                     self.tcx.closure_base_def_id(self.source.def_id())
439                 } else {
440                     self.source.def_id()
441                 };
442                 let generics = self.tcx.generics_of(def_id);
443                 if generics.requires_monomorphization(self.tcx) {
444                     // FIXME: can't handle code with generics
445                     return None;
446                 }
447
448                 let r = self.use_ecx(source_info, |this| {
449                     this.ecx.read_immediate(right)
450                 })?;
451                 if op == BinOp::Shr || op == BinOp::Shl {
452                     let left_ty = left.ty(&self.local_decls, self.tcx);
453                     let left_bits = self
454                         .tcx
455                         .layout_of(self.param_env.and(left_ty))
456                         .unwrap()
457                         .size
458                         .bits();
459                     let right_size = right.layout.size;
460                     let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
461                     if r_bits.ok().map_or(false, |b| b >= left_bits as u128) {
462                         let source_scope_local_data = match self.source_scope_local_data {
463                             ClearCrossCrate::Set(ref data) => data,
464                             ClearCrossCrate::Clear => return None,
465                         };
466                         let dir = if op == BinOp::Shr {
467                             "right"
468                         } else {
469                             "left"
470                         };
471                         let hir_id = source_scope_local_data[source_info.scope].lint_root;
472                         self.tcx.lint_hir(
473                             ::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
474                             hir_id,
475                             span,
476                             &format!("attempt to shift {} with overflow", dir));
477                         return None;
478                     }
479                 }
480                 let left = self.eval_operand(left, source_info)?;
481                 let l = self.use_ecx(source_info, |this| {
482                     this.ecx.read_immediate(left)
483                 })?;
484                 trace!("const evaluating {:?} for {:?} and {:?}", op, left, right);
485                 let (val, overflow) = self.use_ecx(source_info, |this| {
486                     this.ecx.binary_op(op, l, r)
487                 })?;
488                 let val = if let Rvalue::CheckedBinaryOp(..) = *rvalue {
489                     Immediate::ScalarPair(
490                         val.into(),
491                         Scalar::from_bool(overflow).into(),
492                     )
493                 } else {
494                     if overflow {
495                         let err = InterpError::Overflow(op).into();
496                         let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
497                         return None;
498                     }
499                     Immediate::Scalar(val.into())
500                 };
501                 let res = ImmTy {
502                     imm: val,
503                     layout: place_layout,
504                 };
505                 Some(res.into())
506             },
507         }
508     }
509
510     fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
511         Operand::Constant(Box::new(
512             Constant {
513                 span,
514                 ty,
515                 user_ty: None,
516                 literal: self.tcx.mk_const(*ty::Const::from_scalar(
517                     self.tcx,
518                     scalar,
519                     ty,
520                 ))
521             }
522         ))
523     }
524
525     fn replace_with_const(
526         &mut self,
527         rval: &mut Rvalue<'tcx>,
528         value: Const<'tcx>,
529         source_info: SourceInfo,
530     ) {
531         trace!("attepting to replace {:?} with {:?}", rval, value);
532         if let Err(e) = self.ecx.validate_operand(value, vec![], None, true) {
533             trace!("validation error, attempt failed: {:?}", e);
534             return;
535         }
536
537         // FIXME> figure out what tho do when try_read_immediate fails
538         let imm = self.use_ecx(source_info, |this| {
539             this.ecx.try_read_immediate(value)
540         });
541
542         if let Some(Ok(imm)) = imm {
543             match imm {
544                 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
545                     *rval = Rvalue::Use(
546                         self.operand_from_scalar(scalar, value.layout.ty, source_info.span));
547                 },
548                 Immediate::ScalarPair(
549                     ScalarMaybeUndef::Scalar(one),
550                     ScalarMaybeUndef::Scalar(two)
551                 ) => {
552                     let ty = &value.layout.ty.sty;
553                     if let ty::Tuple(substs) = ty {
554                         *rval = Rvalue::Aggregate(
555                             Box::new(AggregateKind::Tuple),
556                             vec![
557                                 self.operand_from_scalar(
558                                     one, substs[0].expect_ty(), source_info.span
559                                 ),
560                                 self.operand_from_scalar(
561                                     two, substs[1].expect_ty(), source_info.span
562                                 ),
563                             ],
564                         );
565                     }
566                 },
567                 _ => { }
568             }
569         }
570     }
571
572     fn should_const_prop(&self) -> bool {
573         self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
574     }
575 }
576
577 fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
578                           param_env: ty::ParamEnv<'tcx>,
579                           ty: Ty<'tcx>) -> Option<u64> {
580     tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
581 }
582
583 struct CanConstProp {
584     can_const_prop: IndexVec<Local, bool>,
585     // false at the beginning, once set, there are not allowed to be any more assignments
586     found_assignment: IndexVec<Local, bool>,
587 }
588
589 impl CanConstProp {
590     /// returns true if `local` can be propagated
591     fn check(mir: &Body<'_>) -> IndexVec<Local, bool> {
592         let mut cpv = CanConstProp {
593             can_const_prop: IndexVec::from_elem(true, &mir.local_decls),
594             found_assignment: IndexVec::from_elem(false, &mir.local_decls),
595         };
596         for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
597             // cannot use args at all
598             // cannot use locals because if x < y { y - x } else { x - y } would
599             //        lint for x != y
600             // FIXME(oli-obk): lint variables until they are used in a condition
601             // FIXME(oli-obk): lint if return value is constant
602             *val = mir.local_kind(local) == LocalKind::Temp;
603
604             if !*val {
605                 trace!("local {:?} can't be propagated because it's not a temporary", local);
606             }
607         }
608         cpv.visit_body(mir);
609         cpv.can_const_prop
610     }
611 }
612
613 impl<'tcx> Visitor<'tcx> for CanConstProp {
614     fn visit_local(
615         &mut self,
616         &local: &Local,
617         context: PlaceContext,
618         _: Location,
619     ) {
620         use rustc::mir::visit::PlaceContext::*;
621         match context {
622             // Constants must have at most one write
623             // FIXME(oli-obk): we could be more powerful here, if the multiple writes
624             // only occur in independent execution paths
625             MutatingUse(MutatingUseContext::Store) => if self.found_assignment[local] {
626                 trace!("local {:?} can't be propagated because of multiple assignments", local);
627                 self.can_const_prop[local] = false;
628             } else {
629                 self.found_assignment[local] = true
630             },
631             // Reading constants is allowed an arbitrary number of times
632             NonMutatingUse(NonMutatingUseContext::Copy) |
633             NonMutatingUse(NonMutatingUseContext::Move) |
634             NonMutatingUse(NonMutatingUseContext::Inspect) |
635             NonMutatingUse(NonMutatingUseContext::Projection) |
636             MutatingUse(MutatingUseContext::Projection) |
637             NonUse(_) => {},
638             _ => {
639                 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
640                 self.can_const_prop[local] = false;
641             },
642         }
643     }
644 }
645
646 impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
647     fn visit_constant(
648         &mut self,
649         constant: &mut Constant<'tcx>,
650         location: Location,
651     ) {
652         trace!("visit_constant: {:?}", constant);
653         self.super_constant(constant, location);
654         self.eval_constant(constant);
655     }
656
657     fn visit_statement(
658         &mut self,
659         statement: &mut Statement<'tcx>,
660         location: Location,
661     ) {
662         trace!("visit_statement: {:?}", statement);
663         if let StatementKind::Assign(ref place, ref mut rval) = statement.kind {
664             let place_ty: Ty<'tcx> = place
665                 .ty(&self.local_decls, self.tcx)
666                 .ty;
667             if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
668                 if let Some(value) = self.const_prop(rval, place_layout, statement.source_info) {
669                     if let Place::Base(PlaceBase::Local(local)) = *place {
670                         trace!("checking whether {:?} can be stored to {:?}", value, local);
671                         if self.can_const_prop[local] {
672                             trace!("storing {:?} to {:?}", value, local);
673                             assert!(self.places[local].is_none());
674                             self.places[local] = Some(value);
675
676                             if self.should_const_prop() {
677                                 self.replace_with_const(
678                                     rval,
679                                     value,
680                                     statement.source_info,
681                                 );
682                             }
683                         }
684                     }
685                 }
686             }
687         }
688         self.super_statement(statement, location);
689     }
690
691     fn visit_terminator(
692         &mut self,
693         terminator: &mut Terminator<'tcx>,
694         location: Location,
695     ) {
696         self.super_terminator(terminator, location);
697         let source_info = terminator.source_info;
698         match &mut terminator.kind {
699             TerminatorKind::Assert { expected, msg, ref mut cond, .. } => {
700                 if let Some(value) = self.eval_operand(&cond, source_info) {
701                     trace!("assertion on {:?} should be {:?}", value, expected);
702                     let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
703                     let value_const = self.ecx.read_scalar(value).unwrap();
704                     if expected != value_const {
705                         // poison all places this operand references so that further code
706                         // doesn't use the invalid value
707                         match cond {
708                             Operand::Move(ref place) | Operand::Copy(ref place) => {
709                                 let mut place = place;
710                                 while let Place::Projection(ref proj) = *place {
711                                     place = &proj.base;
712                                 }
713                                 if let Place::Base(PlaceBase::Local(local)) = *place {
714                                     self.places[local] = None;
715                                 }
716                             },
717                             Operand::Constant(_) => {}
718                         }
719                         let span = terminator.source_info.span;
720                         let hir_id = self
721                             .tcx
722                             .hir()
723                             .as_local_hir_id(self.source.def_id())
724                             .expect("some part of a failing const eval must be local");
725                         use rustc::mir::interpret::InterpError::*;
726                         let msg = match msg {
727                             Overflow(_) |
728                             OverflowNeg |
729                             DivisionByZero |
730                             RemainderByZero => msg.description().to_owned(),
731                             BoundsCheck { ref len, ref index } => {
732                                 let len = self
733                                     .eval_operand(len, source_info)
734                                     .expect("len must be const");
735                                 let len = match self.ecx.read_scalar(len) {
736                                     Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
737                                         data, ..
738                                     })) => data,
739                                     other => bug!("const len not primitive: {:?}", other),
740                                 };
741                                 let index = self
742                                     .eval_operand(index, source_info)
743                                     .expect("index must be const");
744                                 let index = match self.ecx.read_scalar(index) {
745                                     Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
746                                         data, ..
747                                     })) => data,
748                                     other => bug!("const index not primitive: {:?}", other),
749                                 };
750                                 format!(
751                                     "index out of bounds: \
752                                     the len is {} but the index is {}",
753                                     len,
754                                     index,
755                                 )
756                             },
757                             // Need proper const propagator for these
758                             _ => return,
759                         };
760                         self.tcx.lint_hir(
761                             ::rustc::lint::builtin::CONST_ERR,
762                             hir_id,
763                             span,
764                             &msg,
765                         );
766                     } else {
767                         if self.should_const_prop() {
768                             if let ScalarMaybeUndef::Scalar(scalar) = value_const {
769                                 *cond = self.operand_from_scalar(
770                                     scalar,
771                                     self.tcx.types.bool,
772                                     source_info.span,
773                                 );
774                             }
775                         }
776                     }
777                 }
778             },
779             TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
780                 if self.should_const_prop() {
781                     if let Some(value) = self.eval_operand(&discr, source_info) {
782                         if let ScalarMaybeUndef::Scalar(scalar) =
783                                 self.ecx.read_scalar(value).unwrap() {
784                             *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
785                         }
786                     }
787                 }
788             },
789             //none of these have Operands to const-propagate
790             TerminatorKind::Goto { .. } |
791             TerminatorKind::Resume |
792             TerminatorKind::Abort |
793             TerminatorKind::Return |
794             TerminatorKind::Unreachable |
795             TerminatorKind::Drop { .. } |
796             TerminatorKind::DropAndReplace { .. } |
797             TerminatorKind::Yield { .. } |
798             TerminatorKind::GeneratorDrop |
799             TerminatorKind::FalseEdges { .. } |
800             TerminatorKind::FalseUnwind { .. } => { }
801             //FIXME(wesleywiser) Call does have Operands that could be const-propagated
802             TerminatorKind::Call { .. } => { }
803         }
804     }
805 }