1 //! Propagates constants for early reporting of statically known
7 use rustc::mir::interpret::{InterpResult, PanicInfo, Scalar};
8 use rustc::mir::visit::{
9 MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
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,
17 use rustc::traits::TraitQueryMode;
18 use rustc::ty::layout::{
19 HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
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;
27 use rustc_index::vec::IndexVec;
28 use rustc_span::{Span, DUMMY_SP};
29 use syntax::ast::Mutability;
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,
37 use crate::transform::{MirPass, MirSource};
39 /// The maximum number of bytes that we'll allocate space for a return value.
40 const MAX_ALLOC_LIMIT: u64 = 1024;
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() {
51 use rustc::hir::map::blocks::FnLikeNode;
54 .as_local_hir_id(source.def_id())
55 .expect("Non-local call to local provider is_const_fn");
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,
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());
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.
74 trace!("ConstProp skipped for generator {:?}", source.def_id());
78 // Check if it's even possible to satisfy the 'where' clauses
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 {}`
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.
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.
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
106 if !tcx.substitute_normalize_and_test_predicates((
108 InternalSubsts::identity_for_item(tcx, source.def_id()),
109 TraitQueryMode::Canonical,
112 "ConstProp skipped for item with unsatisfiable predicates: {:?}",
118 trace!("ConstProp starting for {:?}", source.def_id());
120 let dummy_body = &Body::new(
121 body.basic_blocks().clone(),
122 body.source_scopes.clone(),
123 body.local_decls.clone(),
127 tcx.def_span(source.def_id()),
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);
140 trace!("ConstProp done for {:?}", source.def_id());
144 struct ConstPropMachine;
146 impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
147 type MemoryKinds = !;
148 type PointerTag = ();
151 type FrameExtra = ();
152 type MemoryExtra = ();
153 type AllocExtra = ();
155 type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
157 const STATIC_KIND: Option<!> = None;
159 const CHECK_ALIGN: bool = false;
162 fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
166 fn find_mir_or_eval_fn(
167 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
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>>> {
178 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
180 _args: &[OpTy<'tcx>],
181 _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
182 _unwind: Option<BasicBlock>,
183 ) -> InterpResult<'tcx> {
188 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
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"));
199 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
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");
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"));
212 _ecx: &InterpCx<'mir, 'tcx, Self>,
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 \
224 fn find_foreign_static(
227 ) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
228 throw_unsup!(ReadForeignStatic)
232 fn init_allocation_extra<'b>(
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
243 fn tag_static_base_pointer(_memory_extra: &(), _id: AllocId) -> Self::PointerTag {
248 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
249 _dest: PlaceTy<'tcx>,
250 ) -> InterpResult<'tcx> {
251 throw_unsup!(ConstPropUnsupported("can't const prop `box` keyword"));
255 _ecx: &InterpCx<'mir, 'tcx, Self>,
256 frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
258 ) -> InterpResult<'tcx, InterpOperand<Self::PointerTag>> {
259 let l = &frame.locals[local];
261 if l.value == LocalValue::Uninitialized {
262 throw_unsup!(ConstPropUnsupported("tried to access an uninitialized local"));
268 fn before_access_static(
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"));
281 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
286 fn stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
291 /// Finds optimization opportunities on the MIR.
292 struct ConstPropagator<'mir, 'tcx> {
293 ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
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>,
308 impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
310 type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
312 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
313 self.tcx.layout_of(self.param_env.and(ty))
317 impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
319 fn data_layout(&self) -> &TargetDataLayout {
320 &self.tcx.data_layout
324 impl<'mir, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'mir, 'tcx> {
326 fn tcx(&self) -> TyCtxt<'tcx> {
331 impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
333 body: ReadOnlyBodyAndCache<'_, 'tcx>,
334 dummy_body: &'mir Body<'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);
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()`
345 if !substs.needs_subst() {
346 param_env = param_env.with_reveal_all();
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);
354 .layout_of(body.return_ty().subst(tcx, substs))
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)
361 .map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
363 ecx.push_stack_frame(
364 Instance::new(def_id, substs),
368 StackPopCleanup::None { cleanup: false },
370 .expect("failed to push initial stack frame");
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),
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()),
398 self.ecx.access_local(self.ecx.frame(), local, None).ok()
401 fn remove_const(&mut self, local: Local) {
402 self.ecx.frame_mut().locals[local] =
403 LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
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,
413 fn use_ecx<F, T>(&mut self, source_info: SourceInfo, f: F) -> Option<T>
415 F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
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),
425 use rustc::mir::interpret::{
426 InterpError::*, UndefinedBehaviorInfo, UnsupportedOpInfo,
429 MachineStop(_) => bug!("ConstProp does not stop"),
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) =>
441 bug!("const-prop encountered allocating error: {:?}", error.kind);
445 | UndefinedBehavior(_)
447 | ResourceExhaustion(_) => {
448 // Ignore these errors.
451 let diagnostic = error_to_const_error(&self.ecx, error);
452 diagnostic.report_as_lint(
454 "this expression will panic at runtime",
463 self.ecx.tcx.span = DUMMY_SP;
467 fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
468 self.ecx.tcx.span = c.span;
470 // FIXME we need to revisit this for #67176
475 match self.ecx.eval_const_to_op(c.literal, None) {
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(),
488 // Out of backwards compatibility we cannot report hard errors in unused
489 // generic functions using associated constants of the generic parameters.
492 "erroneous constant used",
497 err.report_as_error(self.ecx.tcx, "erroneous constant used");
500 err.report_as_error(self.ecx.tcx, "erroneous constant used");
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))
512 fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
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)
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);
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)
544 left: &Operand<'tcx>,
545 right: &Operand<'tcx>,
546 source_info: SourceInfo,
547 place_layout: TyLayout<'tcx>,
548 overflow_check: bool,
550 let r = self.use_ecx(source_info, |this| {
551 this.ecx.read_immediate(this.ecx.eval_operand(right, None)?)
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" };
561 ::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
564 &format!("attempt to shift {} with overflow", dir),
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.
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)?;
580 let err = err_panic!(Overflow(op)).into();
593 rvalue: &Rvalue<'tcx>,
594 place_layout: TyLayout<'tcx>,
595 source_info: SourceInfo,
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) {
603 // FIXME we need to revisit this for #67176
604 if rvalue.needs_subst() {
608 let overflow_check = self.tcx.sess.overflow_checks();
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.
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)?;
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)?;
633 // Do not try creating references (#67862)
634 Rvalue::Ref(_, _, place_ref) => {
635 trace!("skipping Ref({:?})", place_ref);
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)?;
650 fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
651 Operand::Constant(Box::new(Constant {
654 literal: self.tcx.mk_const(*ty::Const::from_scalar(self.tcx, scalar, ty)),
658 fn replace_with_const(
660 rval: &mut Rvalue<'tcx>,
662 source_info: SourceInfo,
664 trace!("attepting to replace {:?} with {:?}", rval, value);
665 if let Err(e) = self.ecx.validate_operand(
668 // FIXME: is ref tracking too expensive?
669 Some(&mut interpret::RefTracking::empty()),
671 trace!("validation error, attempt failed: {:?}", e);
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));
678 if let Some(Ok(imm)) = imm {
680 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
681 *rval = Rvalue::Use(self.operand_from_scalar(
687 Immediate::ScalarPair(
688 ScalarMaybeUndef::Scalar(one),
689 ScalarMaybeUndef::Scalar(two),
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())
706 if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
713 if let Some(Some((ty1, ty2))) = opt_ty1_ty2 {
714 *rval = Rvalue::Aggregate(
715 Box::new(AggregateKind::Tuple),
717 self.operand_from_scalar(one, ty1, source_info.span),
718 self.operand_from_scalar(two, ty2, source_info.span),
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;
733 if mir_opt_level == 0 {
738 interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) => {
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");
756 /// The mode that `ConstProp` is allowed to run in for a given `Local`.
757 #[derive(Clone, Copy, Debug, PartialEq)]
759 /// The `Local` can be propagated into and reads of this `Local` can also be propagated.
761 /// The `Local` can be propagated into but reads cannot be propagated.
763 /// No propagation is allowed at all.
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>,
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),
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
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);
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);
793 cpv.visit_body(body);
798 impl<'tcx> Visitor<'tcx> for CanConstProp {
799 fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
800 use rustc::mir::visit::PlaceContext::*;
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;
810 self.found_assignment[local] = true
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)
821 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
822 self.can_const_prop[local] = ConstPropMode::NoPropagation;
828 impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
829 fn tcx(&self) -> TyCtxt<'tcx> {
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());
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
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);
857 if can_const_prop == ConstPropMode::FullConstProp {
858 trace!("propagated into {:?}", local);
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);
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
887 self.super_statement(statement, location);
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
904 Operand::Move(ref place) | Operand::Copy(ref place) => {
905 self.remove_const(place.local);
907 Operand::Constant(_) => {}
909 let span = terminator.source_info.span;
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 } => {
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),
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),
935 "index out of bounds: \
936 the len is {} but the index is {}",
940 // Need proper const propagator for these
943 self.tcx.lint_hir(::rustc::lint::builtin::CONST_ERR, hir_id, span, &msg);
945 if self.should_const_prop(value) {
946 if let ScalarMaybeUndef::Scalar(scalar) = value_const {
947 *cond = self.operand_from_scalar(
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()
963 *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
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 { .. } => {}