Skip to content

Do not unify borrowed locals in CopyProp. #143509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ fn compute_copy_classes(ssa: &mut SsaLocals, body: &Body<'_>) {
// visited before `local`, and we just have to copy the representing local.
let head = copies[rhs];

// Do not unify two borrowed locals.
if borrowed_classes.contains(local) && borrowed_classes.contains(head) {
// Do not unify borrowed locals.
if borrowed_classes.contains(local) || borrowed_classes.contains(head) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
- // MIR for `borrow_in_loop` before CopyProp
+ // MIR for `borrow_in_loop` after CopyProp

fn borrow_in_loop() -> () {
let mut _0: ();
let mut _1: bool;
let _3: bool;
let mut _4: !;
let mut _5: ();
let mut _7: bool;
let mut _9: bool;
let mut _10: bool;
let mut _11: &bool;
let _12: &bool;
let mut _13: bool;
let mut _14: bool;
let mut _15: bool;
let mut _16: !;
scope 1 {
debug c => _1;
let mut _2: &bool;
let mut _17: &bool;
scope 2 {
debug p => _2;
let _6: bool;
scope 3 {
debug a => _6;
let _8: bool;
scope 4 {
debug b => _8;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_17 = const borrow_in_loop::promoted[0];
_2 = &(*_17);
- StorageLive(_4);
goto -> bb1;
}

bb1: {
- StorageLive(_6);
StorageLive(_7);
_7 = copy (*_2);
_6 = Not(move _7);
StorageDead(_7);
- StorageLive(_8);
StorageLive(_9);
_9 = copy (*_2);
_8 = Not(move _9);
StorageDead(_9);
- StorageLive(_10);
- _10 = copy _6;
- _1 = move _10;
- StorageDead(_10);
+ _1 = copy _6;
StorageLive(_11);
StorageLive(_12);
_12 = &_1;
_11 = &(*_12);
_2 = move _11;
StorageDead(_11);
StorageDead(_12);
StorageLive(_13);
- StorageLive(_14);
- _14 = copy _6;
- StorageLive(_15);
- _15 = copy _8;
- _13 = Ne(move _14, move _15);
+ _13 = Ne(copy _6, copy _8);
switchInt(move _13) -> [0: bb3, otherwise: bb2];
}

bb2: {
- StorageDead(_15);
- StorageDead(_14);
_0 = const ();
StorageDead(_13);
- StorageDead(_8);
- StorageDead(_6);
- StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
return;
}

bb3: {
- StorageDead(_15);
- StorageDead(_14);
- _5 = const ();
StorageDead(_13);
- StorageDead(_8);
- StorageDead(_6);
goto -> bb1;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
- // MIR for `borrow_in_loop` before CopyProp
+ // MIR for `borrow_in_loop` after CopyProp

fn borrow_in_loop() -> () {
let mut _0: ();
let mut _1: bool;
let _3: bool;
let mut _4: !;
let mut _5: ();
let mut _7: bool;
let mut _9: bool;
let mut _10: bool;
let mut _11: &bool;
let _12: &bool;
let mut _13: bool;
let mut _14: bool;
let mut _15: bool;
let mut _16: !;
scope 1 {
debug c => _1;
let mut _2: &bool;
let mut _17: &bool;
scope 2 {
debug p => _2;
let _6: bool;
scope 3 {
debug a => _6;
let _8: bool;
scope 4 {
debug b => _8;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_17 = const borrow_in_loop::promoted[0];
_2 = &(*_17);
- StorageLive(_4);
goto -> bb1;
}

bb1: {
- StorageLive(_6);
StorageLive(_7);
_7 = copy (*_2);
_6 = Not(move _7);
StorageDead(_7);
- StorageLive(_8);
StorageLive(_9);
_9 = copy (*_2);
_8 = Not(move _9);
StorageDead(_9);
- StorageLive(_10);
- _10 = copy _6;
- _1 = move _10;
- StorageDead(_10);
+ _1 = copy _6;
StorageLive(_11);
StorageLive(_12);
_12 = &_1;
_11 = &(*_12);
_2 = move _11;
StorageDead(_11);
StorageDead(_12);
StorageLive(_13);
- StorageLive(_14);
- _14 = copy _6;
- StorageLive(_15);
- _15 = copy _8;
- _13 = Ne(move _14, move _15);
+ _13 = Ne(copy _6, copy _8);
switchInt(move _13) -> [0: bb3, otherwise: bb2];
}

bb2: {
- StorageDead(_15);
- StorageDead(_14);
_0 = const ();
StorageDead(_13);
- StorageDead(_8);
- StorageDead(_6);
- StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
return;
}

bb3: {
- StorageDead(_15);
- StorageDead(_14);
- _5 = const ();
StorageDead(_13);
- StorageDead(_8);
- StorageDead(_6);
goto -> bb1;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
let mut _3: &T;

bb0: {
- _2 = copy _1;
_2 = copy _1;
_3 = &_1;
_0 = opaque::<&T>(copy _3) -> [return: bb1, unwind unreachable];
}

bb1: {
- _0 = opaque::<T>(copy _2) -> [return: bb2, unwind unreachable];
+ _0 = opaque::<T>(copy _1) -> [return: bb2, unwind unreachable];
_0 = opaque::<T>(copy _2) -> [return: bb2, unwind unreachable];
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
let mut _3: &T;

bb0: {
- _2 = copy _1;
_2 = copy _1;
_3 = &_1;
_0 = opaque::<&T>(copy _3) -> [return: bb1, unwind continue];
}

bb1: {
- _0 = opaque::<T>(copy _2) -> [return: bb2, unwind continue];
+ _0 = opaque::<T>(copy _1) -> [return: bb2, unwind continue];
_0 = opaque::<T>(copy _2) -> [return: bb2, unwind continue];
}

bb2: {
Expand Down
37 changes: 36 additions & 1 deletion tests/mir-opt/copy-prop/borrowed_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ fn compare_address() -> bool {
fn borrowed<T: Copy + Freeze>(x: T) -> bool {
// CHECK-LABEL: fn borrowed(
// CHECK: bb0: {
// CHECK-NEXT: _2 = copy _1;
// CHECK-NEXT: _3 = &_1;
// CHECK-NEXT: _0 = opaque::<&T>(copy _3)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<T>(copy _1)
// CHECK-NEXT: _0 = opaque::<T>(copy _2)
mir! {
{
let a = x;
Expand Down Expand Up @@ -94,11 +95,45 @@ fn non_freeze<T: Copy>(x: T) -> bool {
}
}

/// We must not unify a borrowed local with another that may be written-to before the borrow is
/// read again. As we have no aliasing model yet, this means forbidding unifying borrowed locals.
fn borrow_in_loop() {
// CHECK-LABEL: fn borrow_in_loop(
// CHECK: debug c => [[c:_.*]];
// CHECK: debug p => [[p:_.*]];
// CHECK: debug a => [[a:_.*]];
// CHECK: debug b => [[b:_.*]];
// CHECK-NOT: &[[a]]
// CHECK-NOT: &[[b]]
// CHECK: [[a]] = Not({{.*}});
// CHECK-NOT: &[[a]]
// CHECK-NOT: &[[b]]
// CHECK: [[b]] = Not({{.*}});
// CHECK-NOT: &[[a]]
// CHECK-NOT: &[[b]]
// CHECK: &[[c]]
// CHECK-NOT: &[[a]]
// CHECK-NOT: &[[b]]
let mut c;
let mut p = &false;
loop {
let a = !*p;
let b = !*p;
c = a;
p = &c;
if a != b {
return;
}
}
}

fn main() {
assert!(!compare_address());
non_freeze(5);
borrow_in_loop();
}

// EMIT_MIR borrowed_local.compare_address.CopyProp.diff
// EMIT_MIR borrowed_local.borrowed.CopyProp.diff
// EMIT_MIR borrowed_local.non_freeze.CopyProp.diff
// EMIT_MIR borrowed_local.borrow_in_loop.CopyProp.diff
5 changes: 2 additions & 3 deletions tests/mir-opt/copy-prop/write_to_borrowed.main.CopyProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
_3 = const 'b';
_5 = copy _3;
_6 = &_3;
- _4 = copy _5;
_4 = copy _5;
(*_1) = copy (*_6);
_6 = &_5;
- _7 = dump_var::<char>(copy _4) -> [return: bb1, unwind unreachable];
+ _7 = dump_var::<char>(copy _5) -> [return: bb1, unwind unreachable];
_7 = dump_var::<char>(copy _4) -> [return: bb1, unwind unreachable];
}

bb1: {
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/copy-prop/write_to_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ fn main() {
_5 = _3;
// CHECK-NEXT: _6 = &_3;
_6 = &_3;
// CHECK-NOT: {{_.*}} = {{_.*}};
// CHECK-NEXT: _4 = copy _5;
_4 = _5;
// CHECK-NEXT: (*_1) = copy (*_6);
*_1 = *_6;
// CHECK-NEXT: _6 = &_5;
_6 = &_5;
// CHECK-NEXT: _7 = dump_var::<char>(copy _5)
// CHECK-NEXT: _7 = dump_var::<char>(copy _4)
Call(_7 = dump_var(_4), ReturnTo(bb1), UnwindUnreachable())
}
bb1 = { Return() }
Expand Down
Loading
Loading