Skip to content

Commit aeac555

Browse files
committed
Do not see through copies of mutable pointers.
1 parent d0d4e02 commit aeac555

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

compiler/rustc_mir_transform/src/ref_prop.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ fn compute_replacement<'tcx>(
208208
// have been visited before.
209209
Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
210210
| Rvalue::CopyForDeref(place) => {
211-
if let Some(rhs) = place.as_local() {
211+
if let Some(rhs) = place.as_local() && ssa.is_ssa(rhs) {
212212
let target = targets[rhs];
213-
if matches!(target, Value::Pointer(..)) {
213+
// Only see through immutable reference and pointers, as we do not know yet if
214+
// mutable references are fully replaced.
215+
if !needs_unique && matches!(target, Value::Pointer(..)) {
214216
targets[local] = target;
215-
} else if ssa.is_ssa(rhs) {
216-
let refmut = body.local_decls[rhs].ty.is_mutable_ptr();
217-
targets[local] = Value::Pointer(tcx.mk_place_deref(rhs.into()), refmut);
217+
} else {
218+
targets[local] = Value::Pointer(tcx.mk_place_deref(rhs.into()), needs_unique);
218219
}
219220
}
220221
}

tests/mir-opt/reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
- StorageLive(_2); // scope 1 at $DIR/reference_prop.rs:+2:9: +2:13
3838
_2 = &mut _1; // scope 1 at $DIR/reference_prop.rs:+2:16: +2:22
3939
StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:9: +3:13
40-
StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
40+
- StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
4141
- StorageLive(_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
4242
- _5 = &mut (*_2); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
4343
- _4 = &raw mut (*_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
4444
+ _4 = &raw mut _1; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
4545
_3 = _4; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
4646
- StorageDead(_5); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
47-
StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
47+
- StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
4848
StorageLive(_6); // scope 3 at $DIR/reference_prop.rs:+4:9: +4:13
4949
- _6 = &(*_2); // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22
5050
+ _6 = &_1; // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22

tests/mir-opt/reference_prop.unique_with_copies.ReferencePropagation.diff

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+1:9: +1:10
2929
StorageLive(_2); // scope 0 at $DIR/reference_prop.rs:+2:13: +2:18
3030
_2 = const 0_i32; // scope 0 at $DIR/reference_prop.rs:+2:21: +2:22
31-
StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:13: +3:14
31+
- StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:13: +3:14
3232
_3 = &raw mut _2; // scope 2 at $DIR/reference_prop.rs:+3:17: +3:27
3333
StorageLive(_4); // scope 3 at $DIR/reference_prop.rs:+5:9: +5:30
3434
StorageLive(_5); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
35-
- _5 = (*_3); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
36-
+ _5 = _2; // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
35+
_5 = (*_3); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
3736
_4 = opaque::<i32>(move _5) -> bb1; // scope 4 at $DIR/reference_prop.rs:+5:18: +5:28
3837
// mir::Constant
3938
// + span: $DIR/reference_prop.rs:452:18: 452:24
@@ -44,11 +43,12 @@
4443
StorageDead(_5); // scope 4 at $DIR/reference_prop.rs:+5:27: +5:28
4544
StorageDead(_4); // scope 3 at $DIR/reference_prop.rs:+5:30: +5:31
4645
_1 = _3; // scope 3 at $DIR/reference_prop.rs:+6:9: +6:10
47-
StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+7:5: +7:6
46+
- StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+7:5: +7:6
4847
StorageDead(_2); // scope 0 at $DIR/reference_prop.rs:+7:5: +7:6
4948
StorageLive(_6); // scope 1 at $DIR/reference_prop.rs:+9:5: +9:26
5049
StorageLive(_7); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
51-
_7 = (*_1); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
50+
- _7 = (*_1); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
51+
+ _7 = (*_3); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
5252
_6 = opaque::<i32>(move _7) -> bb2; // scope 5 at $DIR/reference_prop.rs:+9:14: +9:24
5353
// mir::Constant
5454
// + span: $DIR/reference_prop.rs:456:14: 456:20

0 commit comments

Comments
 (0)