Skip to content

Commit de434d8

Browse files
committed
Propagate locals, even if they have unpropagatable assignments somewhere.
1 parent c849a0d commit de434d8

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,10 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
775775
// Projections are fine, because `&mut foo.x` will be caught by
776776
// `MutatingUseContext::Borrow` elsewhere.
777777
MutatingUse(MutatingUseContext::Projection)
778+
// These are just stores, where the storing is not propagatable, but there may be later
779+
// mutations of the same local via `Store`
780+
| MutatingUse(MutatingUseContext::Call)
781+
// Actual store that can possibly even propagate a value
778782
| MutatingUse(MutatingUseContext::Store) => {
779783
if !self.found_assignment.insert(local) {
780784
match &mut self.can_const_prop[local] {
@@ -799,7 +803,21 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
799803
| NonMutatingUse(NonMutatingUseContext::Inspect)
800804
| NonMutatingUse(NonMutatingUseContext::Projection)
801805
| NonUse(_) => {}
802-
_ => {
806+
807+
// These could be propagated with a smarter analysis or just some careful thinking about
808+
// whether they'd be fine right now.
809+
MutatingUse(MutatingUseContext::AsmOutput)
810+
| MutatingUse(MutatingUseContext::Yield)
811+
| MutatingUse(MutatingUseContext::Drop)
812+
| MutatingUse(MutatingUseContext::Retag)
813+
// These can't ever be propagated under any scheme, as we can't reason about indirect
814+
// mutation.
815+
| NonMutatingUse(NonMutatingUseContext::SharedBorrow)
816+
| NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
817+
| NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
818+
| NonMutatingUse(NonMutatingUseContext::AddressOf)
819+
| MutatingUse(MutatingUseContext::Borrow)
820+
| MutatingUse(MutatingUseContext::AddressOf) => {
803821
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
804822
self.can_const_prop[local] = ConstPropMode::NoPropagation;
805823
}

src/test/mir-opt/const_prop/mutable_variable_aggregate_partial_read/rustc.main.ConstProp.diff

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@
2929
// + ty: i32
3030
// + val: Value(Scalar(0x00000063))
3131
// mir::Constant
32-
// + span: $DIR/mutable_variable_aggregate_partial_read.rs:6:11: 6:13
32+
- // + span: $DIR/mutable_variable_aggregate_partial_read.rs:6:11: 6:13
33+
+ // + span: $DIR/mutable_variable_aggregate_partial_read.rs:6:5: 6:13
3334
// + literal: Const { ty: i32, val: Value(Scalar(0x00000063)) }
3435
(_1.0: i32) = const 42i32; // scope 1 at $DIR/mutable_variable_aggregate_partial_read.rs:7:5: 7:13
3536
// ty::Const
3637
// + ty: i32
3738
// + val: Value(Scalar(0x0000002a))
3839
// mir::Constant
39-
// + span: $DIR/mutable_variable_aggregate_partial_read.rs:7:11: 7:13
40+
- // + span: $DIR/mutable_variable_aggregate_partial_read.rs:7:11: 7:13
41+
+ // + span: $DIR/mutable_variable_aggregate_partial_read.rs:7:5: 7:13
4042
// + literal: Const { ty: i32, val: Value(Scalar(0x0000002a)) }
4143
StorageLive(_2); // scope 1 at $DIR/mutable_variable_aggregate_partial_read.rs:8:9: 8:10
42-
_2 = (_1.1: i32); // scope 1 at $DIR/mutable_variable_aggregate_partial_read.rs:8:13: 8:16
44+
- _2 = (_1.1: i32); // scope 1 at $DIR/mutable_variable_aggregate_partial_read.rs:8:13: 8:16
45+
+ _2 = const 99i32; // scope 1 at $DIR/mutable_variable_aggregate_partial_read.rs:8:13: 8:16
46+
+ // ty::Const
47+
+ // + ty: i32
48+
+ // + val: Value(Scalar(0x00000063))
49+
+ // mir::Constant
50+
+ // + span: $DIR/mutable_variable_aggregate_partial_read.rs:8:13: 8:16
51+
+ // + literal: Const { ty: i32, val: Value(Scalar(0x00000063)) }
4352
_0 = const (); // scope 0 at $DIR/mutable_variable_aggregate_partial_read.rs:4:11: 9:2
4453
// ty::Const
4554
// + ty: ()

0 commit comments

Comments
 (0)