From aeac5555780cb11cf701a5fa69d66adcaa3e2f4a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 10 May 2023 17:29:02 +0000 Subject: [PATCH] Do not see through copies of mutable pointers. --- compiler/rustc_mir_transform/src/ref_prop.rs | 11 ++++++----- ...rop.mut_raw_then_mut_shr.ReferencePropagation.diff | 4 ++-- ..._prop.unique_with_copies.ReferencePropagation.diff | 10 +++++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_mir_transform/src/ref_prop.rs b/compiler/rustc_mir_transform/src/ref_prop.rs index ac55ce3b316..38be7b3c7ea 100644 --- a/compiler/rustc_mir_transform/src/ref_prop.rs +++ b/compiler/rustc_mir_transform/src/ref_prop.rs @@ -208,13 +208,14 @@ fn compute_replacement<'tcx>( // have been visited before. Rvalue::Use(Operand::Copy(place) | Operand::Move(place)) | Rvalue::CopyForDeref(place) => { - if let Some(rhs) = place.as_local() { + if let Some(rhs) = place.as_local() && ssa.is_ssa(rhs) { let target = targets[rhs]; - if matches!(target, Value::Pointer(..)) { + // Only see through immutable reference and pointers, as we do not know yet if + // mutable references are fully replaced. + if !needs_unique && matches!(target, Value::Pointer(..)) { targets[local] = target; - } else if ssa.is_ssa(rhs) { - let refmut = body.local_decls[rhs].ty.is_mutable_ptr(); - targets[local] = Value::Pointer(tcx.mk_place_deref(rhs.into()), refmut); + } else { + targets[local] = Value::Pointer(tcx.mk_place_deref(rhs.into()), needs_unique); } } } diff --git a/tests/mir-opt/reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff b/tests/mir-opt/reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff index ed9f8c2b187..af8ee2411d3 100644 --- a/tests/mir-opt/reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff +++ b/tests/mir-opt/reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff @@ -37,14 +37,14 @@ - StorageLive(_2); // scope 1 at $DIR/reference_prop.rs:+2:9: +2:13 _2 = &mut _1; // scope 1 at $DIR/reference_prop.rs:+2:16: +2:22 StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:9: +3:13 - StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36 +- StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36 - StorageLive(_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26 - _5 = &mut (*_2); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26 - _4 = &raw mut (*_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26 + _4 = &raw mut _1; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26 _3 = _4; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36 - StorageDead(_5); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37 - StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37 +- StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37 StorageLive(_6); // scope 3 at $DIR/reference_prop.rs:+4:9: +4:13 - _6 = &(*_2); // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22 + _6 = &_1; // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22 diff --git a/tests/mir-opt/reference_prop.unique_with_copies.ReferencePropagation.diff b/tests/mir-opt/reference_prop.unique_with_copies.ReferencePropagation.diff index 89aa6c7b792..2cda2409e80 100644 --- a/tests/mir-opt/reference_prop.unique_with_copies.ReferencePropagation.diff +++ b/tests/mir-opt/reference_prop.unique_with_copies.ReferencePropagation.diff @@ -28,12 +28,11 @@ StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+1:9: +1:10 StorageLive(_2); // scope 0 at $DIR/reference_prop.rs:+2:13: +2:18 _2 = const 0_i32; // scope 0 at $DIR/reference_prop.rs:+2:21: +2:22 - StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:13: +3:14 +- StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:13: +3:14 _3 = &raw mut _2; // scope 2 at $DIR/reference_prop.rs:+3:17: +3:27 StorageLive(_4); // scope 3 at $DIR/reference_prop.rs:+5:9: +5:30 StorageLive(_5); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27 -- _5 = (*_3); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27 -+ _5 = _2; // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27 + _5 = (*_3); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27 _4 = opaque::(move _5) -> bb1; // scope 4 at $DIR/reference_prop.rs:+5:18: +5:28 // mir::Constant // + span: $DIR/reference_prop.rs:452:18: 452:24 @@ -44,11 +43,12 @@ StorageDead(_5); // scope 4 at $DIR/reference_prop.rs:+5:27: +5:28 StorageDead(_4); // scope 3 at $DIR/reference_prop.rs:+5:30: +5:31 _1 = _3; // scope 3 at $DIR/reference_prop.rs:+6:9: +6:10 - StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+7:5: +7:6 +- StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+7:5: +7:6 StorageDead(_2); // scope 0 at $DIR/reference_prop.rs:+7:5: +7:6 StorageLive(_6); // scope 1 at $DIR/reference_prop.rs:+9:5: +9:26 StorageLive(_7); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23 - _7 = (*_1); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23 +- _7 = (*_1); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23 ++ _7 = (*_3); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23 _6 = opaque::(move _7) -> bb2; // scope 5 at $DIR/reference_prop.rs:+9:14: +9:24 // mir::Constant // + span: $DIR/reference_prop.rs:456:14: 456:20