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