Skip to content

Commit 16c1019

Browse files
committed
Auto merge of rust-lang#133474 - RalfJung:gvn-miscompile, r=<try>
Do not unify dereferences of shared borrows in GVN Repost of rust-lang#132461, the last commit applies my suggestions. Fixes rust-lang#130853
2 parents 7db7489 + 906f66f commit 16c1019

33 files changed

+614
-482
lines changed

Diff for: compiler/rustc_mir_transform/src/gvn.rs

+3-1
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
{

Diff for: tests/coverage/closure.cov-map

+10-8
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]

Diff for: tests/coverage/issue-84561.cov-map

+110-95
Large diffs are not rendered by default.

Diff for: tests/mir-opt/const_prop/read_immutable_static.main.GVN.diff

+5-9
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;

Diff for: tests/mir-opt/const_prop/read_immutable_static.rs

+2-1
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
}

Diff for: tests/mir-opt/const_prop/ref_deref.main.GVN.diff

+1-2
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);

Diff for: tests/mir-opt/const_prop/ref_deref_project.main.GVN.diff

+1-2
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);

Diff for: tests/mir-opt/const_prop/ref_deref_project.rs

+2-1
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
}

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

+4-5
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);

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

+4-5
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);

Diff for: tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-abort.diff

+4-5
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);

Diff for: tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-unwind.diff

+4-5
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);

Diff for: tests/mir-opt/const_prop/slice_len.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ fn main() {
88
// CHECK-LABEL: fn main(
99
// CHECK: debug a => [[a:_.*]];
1010
// CHECK: [[slice:_.*]] = copy {{.*}} as &[u32] (PointerCoercion(Unsize, AsCast));
11-
// CHECK: assert(const true,
12-
// CHECK: [[a]] = const 2_u32;
11+
// Disabled due to <https://github.com/rust-lang/rust/issues/130853>
12+
// COM: CHECK: assert(const true,
13+
// COM: CHECK: [[a]] = const 2_u32;
1314
let a = (&[1u32, 2, 3] as &[u32])[1];
1415
}

Diff for: tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
}
1919

2020
bb2: {
21-
- _0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind unreachable];
22-
+ _0 = opaque::<T>(copy _1) -> [return: bb3, unwind unreachable];
21+
_0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind unreachable];
2322
}
2423

2524
bb3: {

Diff for: tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
}
1919

2020
bb2: {
21-
- _0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind continue];
22-
+ _0 = opaque::<T>(copy _1) -> [return: bb3, unwind continue];
21+
_0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind continue];
2322
}
2423

2524
bb3: {

Diff for: tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff

+10-20
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,18 @@
107107
StorageLive(_18);
108108
_18 = &(*_1);
109109
StorageLive(_19);
110-
- StorageLive(_20);
111-
+ nop;
110+
StorageLive(_20);
112111
_20 = copy (*_18);
113-
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind unreachable];
114-
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind unreachable];
112+
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind unreachable];
115113
}
116114

117115
bb7: {
118-
- StorageDead(_20);
119-
+ nop;
116+
StorageDead(_20);
120117
StorageDead(_19);
121118
StorageLive(_21);
122119
StorageLive(_22);
123-
- _22 = copy (*_18);
124-
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind unreachable];
125-
+ _22 = copy _20;
126-
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind unreachable];
120+
_22 = copy (*_18);
121+
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind unreachable];
127122
}
128123

129124
bb8: {
@@ -157,23 +152,18 @@
157152
StorageDead(_28);
158153
StorageDead(_27);
159154
StorageLive(_29);
160-
- StorageLive(_30);
161-
+ nop;
155+
StorageLive(_30);
162156
_30 = copy ((*_3).0: u32);
163-
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind unreachable];
164-
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind unreachable];
157+
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind unreachable];
165158
}
166159

167160
bb12: {
168-
- StorageDead(_30);
169-
+ nop;
161+
StorageDead(_30);
170162
StorageDead(_29);
171163
StorageLive(_31);
172164
StorageLive(_32);
173-
- _32 = copy ((*_3).0: u32);
174-
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind unreachable];
175-
+ _32 = copy _30;
176-
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind unreachable];
165+
_32 = copy ((*_3).0: u32);
166+
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind unreachable];
177167
}
178168

179169
bb13: {

Diff for: tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff

+10-20
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,18 @@
107107
StorageLive(_18);
108108
_18 = &(*_1);
109109
StorageLive(_19);
110-
- StorageLive(_20);
111-
+ nop;
110+
StorageLive(_20);
112111
_20 = copy (*_18);
113-
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
114-
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind continue];
112+
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
115113
}
116114

117115
bb7: {
118-
- StorageDead(_20);
119-
+ nop;
116+
StorageDead(_20);
120117
StorageDead(_19);
121118
StorageLive(_21);
122119
StorageLive(_22);
123-
- _22 = copy (*_18);
124-
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
125-
+ _22 = copy _20;
126-
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind continue];
120+
_22 = copy (*_18);
121+
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
127122
}
128123

129124
bb8: {
@@ -157,23 +152,18 @@
157152
StorageDead(_28);
158153
StorageDead(_27);
159154
StorageLive(_29);
160-
- StorageLive(_30);
161-
+ nop;
155+
StorageLive(_30);
162156
_30 = copy ((*_3).0: u32);
163-
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
164-
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind continue];
157+
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
165158
}
166159

167160
bb12: {
168-
- StorageDead(_30);
169-
+ nop;
161+
StorageDead(_30);
170162
StorageDead(_29);
171163
StorageLive(_31);
172164
StorageLive(_32);
173-
- _32 = copy ((*_3).0: u32);
174-
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
175-
+ _32 = copy _30;
176-
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind continue];
165+
_32 = copy ((*_3).0: u32);
166+
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
177167
}
178168

179169
bb13: {

Diff for: tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
let mut _3: fn(u8) -> u8;
99
let _5: ();
1010
let mut _6: fn(u8) -> u8;
11-
let mut _9: {closure@$DIR/gvn.rs:614:19: 614:21};
11+
let mut _9: {closure@$DIR/gvn.rs:615:19: 615:21};
1212
let _10: ();
1313
let mut _11: fn();
14-
let mut _13: {closure@$DIR/gvn.rs:614:19: 614:21};
14+
let mut _13: {closure@$DIR/gvn.rs:615:19: 615:21};
1515
let _14: ();
1616
let mut _15: fn();
1717
scope 1 {
1818
debug f => _1;
1919
let _4: fn(u8) -> u8;
2020
scope 2 {
2121
debug g => _4;
22-
let _7: {closure@$DIR/gvn.rs:614:19: 614:21};
22+
let _7: {closure@$DIR/gvn.rs:615:19: 615:21};
2323
scope 3 {
2424
debug closure => _7;
2525
let _8: fn();
@@ -62,16 +62,16 @@
6262
StorageDead(_6);
6363
StorageDead(_5);
6464
- StorageLive(_7);
65-
- _7 = {closure@$DIR/gvn.rs:614:19: 614:21};
65+
- _7 = {closure@$DIR/gvn.rs:615:19: 615:21};
6666
- StorageLive(_8);
6767
+ nop;
68-
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
68+
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:615:19: 615:21};
6969
+ nop;
7070
StorageLive(_9);
7171
- _9 = copy _7;
7272
- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
73-
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
74-
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
73+
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:615:19: 615:21};
74+
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:615:19: 615:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
7575
StorageDead(_9);
7676
StorageLive(_10);
7777
StorageLive(_11);
@@ -88,8 +88,8 @@
8888
StorageLive(_13);
8989
- _13 = copy _7;
9090
- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
91-
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
92-
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
91+
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:615:19: 615:21};
92+
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:615:19: 615:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
9393
StorageDead(_13);
9494
StorageLive(_14);
9595
StorageLive(_15);

0 commit comments

Comments
 (0)