Skip to content

Commit 6b6a867

Browse files
committed
Auto merge of #133474 - RalfJung:gvn-miscompile, r=compiler-errors
Do not unify dereferences of shared borrows in GVN Repost of #132461, the last commit applies my suggestions. Fixes #130853
2 parents c322cd5 + 906f66f commit 6b6a867

33 files changed

+614
-482
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
638638
let proj = match proj {
639639
ProjectionElem::Deref => {
640640
let ty = place.ty(self.local_decls, self.tcx).ty;
641-
if let Some(Mutability::Not) = ty.ref_mutability()
641+
// unsound: https://github.com/rust-lang/rust/issues/130853
642+
if self.tcx.sess.opts.unstable_opts.unsound_mir_opts
643+
&& let Some(Mutability::Not) = ty.ref_mutability()
642644
&& let Some(pointee_ty) = ty.builtin_deref(true)
643645
&& pointee_ty.is_freeze(self.tcx, self.typing_env())
644646
{

tests/coverage/closure.cov-map

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,19 @@ Number of file 0 mappings: 6
140140
- Code(Counter(0)) at (prev + 2, 9) to (start + 0, 10)
141141
Highest counter ID seen: c1
142142

143-
Function name: closure::main::{closure#18} (unused)
144-
Raw bytes (24): 0x[01, 01, 00, 04, 00, 19, 0d, 02, 1c, 00, 02, 1d, 02, 12, 00, 02, 11, 00, 12, 00, 01, 11, 01, 0e]
143+
Function name: closure::main::{closure#18}
144+
Raw bytes (26): 0x[01, 01, 01, 01, 05, 04, 01, 19, 0d, 02, 1c, 05, 02, 1d, 02, 12, 02, 02, 11, 00, 12, 01, 01, 11, 01, 0e]
145145
Number of files: 1
146146
- file 0 => global file 1
147-
Number of expressions: 0
147+
Number of expressions: 1
148+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
148149
Number of file 0 mappings: 4
149-
- Code(Zero) at (prev + 25, 13) to (start + 2, 28)
150-
- Code(Zero) at (prev + 2, 29) to (start + 2, 18)
151-
- Code(Zero) at (prev + 2, 17) to (start + 0, 18)
152-
- Code(Zero) at (prev + 1, 17) to (start + 1, 14)
153-
Highest counter ID seen: (none)
150+
- Code(Counter(0)) at (prev + 25, 13) to (start + 2, 28)
151+
- Code(Counter(1)) at (prev + 2, 29) to (start + 2, 18)
152+
- Code(Expression(0, Sub)) at (prev + 2, 17) to (start + 0, 18)
153+
= (c0 - c1)
154+
- Code(Counter(0)) at (prev + 1, 17) to (start + 1, 14)
155+
Highest counter ID seen: c1
154156

155157
Function name: closure::main::{closure#19}
156158
Raw bytes (26): 0x[01, 01, 01, 01, 05, 04, 01, 43, 0d, 02, 1c, 05, 02, 1d, 02, 12, 02, 02, 11, 00, 12, 01, 01, 11, 01, 0e]

tests/coverage/issue-84561.cov-map

Lines changed: 110 additions & 95 deletions
Large diffs are not rendered by default.

tests/mir-opt/const_prop/read_immutable_static.main.GVN.diff

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,19 @@
1414

1515
bb0: {
1616
StorageLive(_1);
17-
- StorageLive(_2);
17+
StorageLive(_2);
1818
- StorageLive(_3);
19-
+ nop;
2019
+ nop;
2120
_3 = const {ALLOC0: &u8};
22-
- _2 = copy (*_3);
23-
+ _2 = const 2_u8;
21+
_2 = copy (*_3);
2422
StorageLive(_4);
2523
StorageLive(_5);
2624
_5 = const {ALLOC0: &u8};
2725
- _4 = copy (*_5);
28-
- _1 = Add(move _2, move _4);
29-
+ _4 = const 2_u8;
30-
+ _1 = const 4_u8;
26+
+ _4 = copy (*_3);
27+
_1 = Add(move _2, move _4);
3128
StorageDead(_4);
32-
- StorageDead(_2);
33-
+ nop;
29+
StorageDead(_2);
3430
StorageDead(_5);
3531
- StorageDead(_3);
3632
+ nop;

tests/mir-opt/const_prop/read_immutable_static.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ static FOO: u8 = 2;
66
fn main() {
77
// CHECK-LABEL: fn main(
88
// CHECK: debug x => [[x:_.*]];
9-
// CHECK: [[x]] = const 4_u8;
9+
// Disabled due to <https://github.com/rust-lang/rust/issues/130853>
10+
// COM: CHECK: [[x]] = const 4_u8;
1011
let x = FOO + FOO;
1112
}

tests/mir-opt/const_prop/ref_deref.main.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
StorageLive(_2);
1717
_4 = const main::promoted[0];
1818
_2 = &(*_4);
19-
- _1 = copy (*_2);
20-
+ _1 = const 4_i32;
19+
_1 = copy (*_2);
2120
StorageDead(_2);
2221
_0 = const ();
2322
StorageDead(_1);

tests/mir-opt/const_prop/ref_deref_project.main.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
StorageLive(_2);
1717
_4 = const main::promoted[0];
1818
_2 = &((*_4).1: i32);
19-
- _1 = copy (*_2);
20-
+ _1 = const 5_i32;
19+
_1 = copy (*_2);
2120
StorageDead(_2);
2221
_0 = const ();
2322
StorageDead(_1);

tests/mir-opt/const_prop/ref_deref_project.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
fn main() {
66
// CHECK-LABEL: fn main(
77
// CHECK: debug a => [[a:_.*]];
8-
// CHECK: [[a]] = const 5_i32;
8+
// Disabled due to <https://github.com/rust-lang/rust/issues/130853>
9+
// COM: CHECK: [[a]] = const 5_i32;
910
let a = *(&(4, 5).1);
1011
}

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-abort.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind unreachable];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind unreachable];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-unwind.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind continue];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind continue];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

0 commit comments

Comments
 (0)