Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3fd2d4a

Browse files
committed
Blocked MutatingUseContext::Projection for all locals of kind LocalKind::Temp. Added a cache of LocalKinds to CanConstProp
1 parent 6316601 commit 3fd2d4a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ struct CanConstProp {
766766
can_const_prop: IndexVec<Local, ConstPropMode>,
767767
// false at the beginning, once set, there are not allowed to be any more assignments
768768
found_assignment: BitSet<Local>,
769+
// Cache of locals' information
770+
local_kinds: IndexVec<Local, LocalKind>,
769771
}
770772

771773
impl CanConstProp {
@@ -774,16 +776,19 @@ impl CanConstProp {
774776
let mut cpv = CanConstProp {
775777
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
776778
found_assignment: BitSet::new_empty(body.local_decls.len()),
779+
local_kinds: IndexVec::from_fn_n(
780+
|local| body.local_kind(local),
781+
body.local_decls.len(),
782+
),
777783
};
778784
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
779785
// cannot use args at all
780786
// cannot use locals because if x < y { y - x } else { x - y } would
781787
// lint for x != y
782788
// FIXME(oli-obk): lint variables until they are used in a condition
783789
// FIXME(oli-obk): lint if return value is constant
784-
let local_kind = body.local_kind(local);
785-
786-
if local_kind == LocalKind::Arg || local_kind == LocalKind::Var {
790+
if cpv.local_kinds[local] == LocalKind::Arg || cpv.local_kinds[local] == LocalKind::Var
791+
{
787792
*val = ConstPropMode::OnlyPropagateInto;
788793
trace!("local {:?} can't be const propagated because it's not a temporary", local);
789794
}
@@ -811,8 +816,12 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
811816
| NonMutatingUse(NonMutatingUseContext::Move)
812817
| NonMutatingUse(NonMutatingUseContext::Inspect)
813818
| NonMutatingUse(NonMutatingUseContext::Projection)
814-
| MutatingUse(MutatingUseContext::Projection)
815819
| NonUse(_) => {}
820+
MutatingUse(MutatingUseContext::Projection) => {
821+
if self.local_kinds[local] != LocalKind::Temp {
822+
self.can_const_prop[local] = ConstPropMode::NoPropagation;
823+
}
824+
}
816825
_ => {
817826
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
818827
self.can_const_prop[local] = ConstPropMode::NoPropagation;

0 commit comments

Comments
 (0)