Skip to content

Commit 1ffe905

Browse files
committed
Reject borrows of projections in ConstProp.
1 parent e2caebc commit 1ffe905

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,22 @@ impl CanConstProp {
714714
}
715715
}
716716

717-
impl Visitor<'_> for CanConstProp {
717+
impl<'tcx> Visitor<'tcx> for CanConstProp {
718+
fn visit_place(&mut self, place: &Place<'tcx>, mut context: PlaceContext, loc: Location) {
719+
use rustc_middle::mir::visit::PlaceContext::*;
720+
721+
// Dereferencing just read the addess of `place.local`.
722+
if place.projection.first() == Some(&PlaceElem::Deref) {
723+
context = NonMutatingUse(NonMutatingUseContext::Copy);
724+
}
725+
726+
self.visit_local(place.local, context, loc);
727+
self.visit_projection(place.as_ref(), context, loc);
728+
}
729+
718730
fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
719731
use rustc_middle::mir::visit::PlaceContext::*;
720732
match context {
721-
// Projections are fine, because `&mut foo.x` will be caught by
722-
// `MutatingUseContext::Borrow` elsewhere.
723-
MutatingUse(MutatingUseContext::Projection)
724733
// These are just stores, where the storing is not propagatable, but there may be later
725734
// mutations of the same local via `Store`
726735
| MutatingUse(MutatingUseContext::Call)
@@ -751,7 +760,6 @@ impl Visitor<'_> for CanConstProp {
751760
NonMutatingUse(NonMutatingUseContext::Copy)
752761
| NonMutatingUse(NonMutatingUseContext::Move)
753762
| NonMutatingUse(NonMutatingUseContext::Inspect)
754-
| NonMutatingUse(NonMutatingUseContext::Projection)
755763
| NonMutatingUse(NonMutatingUseContext::PlaceMention)
756764
| NonUse(_) => {}
757765

@@ -771,6 +779,8 @@ impl Visitor<'_> for CanConstProp {
771779
trace!("local {:?} can't be propagated because it's used: {:?}", local, context);
772780
self.can_const_prop[local] = ConstPropMode::NoPropagation;
773781
}
782+
MutatingUse(MutatingUseContext::Projection)
783+
| NonMutatingUse(NonMutatingUseContext::Projection) => bug!("visit_place should not pass {context:?} for {local:?}"),
774784
}
775785
}
776786
}

tests/mir-opt/const_prop/address_of_pair.fn0.ConstProp.diff

+5-10
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,20 @@
2323

2424
bb0: {
2525
StorageLive(_2); // scope 0 at $DIR/address_of_pair.rs:+1:9: +1:17
26-
- _2 = (const 1_i32, const false); // scope 0 at $DIR/address_of_pair.rs:+1:20: +1:30
27-
+ _2 = const (1_i32, false); // scope 0 at $DIR/address_of_pair.rs:+1:20: +1:30
26+
_2 = (const 1_i32, const false); // scope 0 at $DIR/address_of_pair.rs:+1:20: +1:30
2827
StorageLive(_3); // scope 1 at $DIR/address_of_pair.rs:+2:9: +2:12
2928
_3 = &raw mut (_2.1: bool); // scope 1 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
30-
- _2 = (const 1_i32, const false); // scope 2 at $DIR/address_of_pair.rs:+3:5: +3:22
31-
+ _2 = const (1_i32, false); // scope 2 at $DIR/address_of_pair.rs:+3:5: +3:22
29+
_2 = (const 1_i32, const false); // scope 2 at $DIR/address_of_pair.rs:+3:5: +3:22
3230
StorageLive(_4); // scope 2 at $DIR/address_of_pair.rs:+4:5: +6:6
3331
(*_3) = const true; // scope 3 at $DIR/address_of_pair.rs:+5:9: +5:20
3432
_4 = const (); // scope 3 at $DIR/address_of_pair.rs:+4:5: +6:6
3533
StorageDead(_4); // scope 2 at $DIR/address_of_pair.rs:+6:5: +6:6
3634
StorageLive(_5); // scope 2 at $DIR/address_of_pair.rs:+7:9: +7:12
3735
StorageLive(_6); // scope 2 at $DIR/address_of_pair.rs:+7:16: +7:22
38-
- _6 = (_2.1: bool); // scope 2 at $DIR/address_of_pair.rs:+7:16: +7:22
39-
- _5 = Not(move _6); // scope 2 at $DIR/address_of_pair.rs:+7:15: +7:22
40-
+ _6 = const false; // scope 2 at $DIR/address_of_pair.rs:+7:16: +7:22
41-
+ _5 = const true; // scope 2 at $DIR/address_of_pair.rs:+7:15: +7:22
36+
_6 = (_2.1: bool); // scope 2 at $DIR/address_of_pair.rs:+7:16: +7:22
37+
_5 = Not(move _6); // scope 2 at $DIR/address_of_pair.rs:+7:15: +7:22
4238
StorageDead(_6); // scope 2 at $DIR/address_of_pair.rs:+7:21: +7:22
43-
- _0 = _5; // scope 4 at $DIR/address_of_pair.rs:+8:12: +8:15
44-
+ _0 = const true; // scope 4 at $DIR/address_of_pair.rs:+8:12: +8:15
39+
_0 = _5; // scope 4 at $DIR/address_of_pair.rs:+8:12: +8:15
4540
StorageDead(_5); // scope 2 at $DIR/address_of_pair.rs:+9:1: +9:2
4641
StorageDead(_3); // scope 1 at $DIR/address_of_pair.rs:+9:1: +9:2
4742
StorageDead(_2); // scope 0 at $DIR/address_of_pair.rs:+9:1: +9:2

tests/mir-opt/const_prop_miscompile.bar.ConstProp.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
bb0: {
2121
StorageLive(_1); // scope 0 at $DIR/const_prop_miscompile.rs:+1:9: +1:14
22-
- _1 = (const 1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
23-
+ _1 = const (1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
22+
_1 = (const 1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
2423
StorageLive(_2); // scope 1 at $DIR/const_prop_miscompile.rs:+2:5: +4:6
2524
StorageLive(_3); // scope 2 at $DIR/const_prop_miscompile.rs:+3:10: +3:22
2625
_3 = &raw mut (_1.0: i32); // scope 2 at $DIR/const_prop_miscompile.rs:+3:10: +3:22

tests/mir-opt/const_prop_miscompile.foo.ConstProp.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
bb0: {
1818
StorageLive(_1); // scope 0 at $DIR/const_prop_miscompile.rs:+1:9: +1:14
19-
- _1 = (const 1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
20-
+ _1 = const (1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
19+
_1 = (const 1_i32,); // scope 0 at $DIR/const_prop_miscompile.rs:+1:17: +1:21
2120
StorageLive(_2); // scope 1 at $DIR/const_prop_miscompile.rs:+2:6: +2:14
2221
_2 = &mut (_1.0: i32); // scope 1 at $DIR/const_prop_miscompile.rs:+2:6: +2:14
2322
(*_2) = const 5_i32; // scope 1 at $DIR/const_prop_miscompile.rs:+2:5: +2:18

0 commit comments

Comments
 (0)