Skip to content

Commit 2d3178b

Browse files
committed
Disable MatchBranchSimplification
1 parent 0ed85d0 commit 2d3178b

11 files changed

+154
-71
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub struct MatchBranchSimplification;
1111

1212
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
1313
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
14-
sess.mir_opt_level() >= 1
14+
// unsound: https://github.com/rust-lang/rust/issues/124150
15+
sess.mir_opt_level() >= 1 && sess.opts.unstable_opts.unsound_mir_opts
1516
}
1617

1718
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff

+25-20
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
let _6: *mut [bool; 0];
2020
scope 6 {
2121
scope 10 (inlined NonNull::<[bool; 0]>::new_unchecked) {
22-
let mut _8: bool;
23-
let _9: ();
24-
let mut _10: *mut ();
25-
let mut _11: *const [bool; 0];
22+
let _8: ();
23+
let mut _9: *mut ();
24+
let mut _10: *const [bool; 0];
2625
scope 11 (inlined core::ub_checks::check_language_ub) {
26+
let mut _11: bool;
2727
scope 12 (inlined core::ub_checks::check_language_ub::runtime) {
2828
}
2929
}
@@ -44,18 +44,18 @@
4444
StorageLive(_1);
4545
StorageLive(_2);
4646
StorageLive(_3);
47-
StorageLive(_9);
47+
StorageLive(_8);
4848
StorageLive(_4);
4949
StorageLive(_5);
5050
StorageLive(_6);
5151
StorageLive(_7);
5252
_7 = const 1_usize;
5353
_6 = const {0x1 as *mut [bool; 0]};
5454
StorageDead(_7);
55+
StorageLive(_10);
5556
StorageLive(_11);
56-
StorageLive(_8);
57-
_8 = UbChecks();
58-
switchInt(move _8) -> [0: bb5, otherwise: bb3];
57+
_11 = UbChecks();
58+
switchInt(move _11) -> [0: bb6, otherwise: bb5];
5959
}
6060

6161
bb1: {
@@ -68,34 +68,39 @@
6868
}
6969

7070
bb3: {
71-
StorageLive(_10);
72-
_10 = const {0x1 as *mut ()};
73-
_9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb4, unwind unreachable];
71+
StorageDead(_9);
72+
goto -> bb4;
7473
}
7574

7675
bb4: {
77-
StorageDead(_10);
78-
goto -> bb5;
79-
}
80-
81-
bb5: {
82-
StorageDead(_8);
83-
_11 = const {0x1 as *const [bool; 0]};
76+
_10 = const {0x1 as *const [bool; 0]};
8477
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
85-
StorageDead(_11);
78+
StorageDead(_10);
8679
StorageDead(_6);
8780
_4 = const Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
8881
StorageDead(_5);
8982
_3 = const Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};
9083
StorageDead(_4);
9184
_2 = const Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global);
92-
StorageDead(_9);
85+
StorageDead(_8);
9386
StorageDead(_3);
9487
_1 = const A {{ foo: Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC2, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global) }};
9588
StorageDead(_2);
9689
_0 = const ();
9790
drop(_1) -> [return: bb1, unwind: bb2];
9891
}
92+
93+
bb5: {
94+
StorageDead(_11);
95+
StorageLive(_9);
96+
_9 = const {0x1 as *mut ()};
97+
_8 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable];
98+
}
99+
100+
bb6: {
101+
StorageDead(_11);
102+
goto -> bb4;
103+
}
99104
}
100105

101106
ALLOC2 (size: 16, align: 8) {

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-unwind.diff

+29-24
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
let _6: *mut [bool; 0];
2020
scope 6 {
2121
scope 10 (inlined NonNull::<[bool; 0]>::new_unchecked) {
22-
let mut _8: bool;
23-
let _9: ();
24-
let mut _10: *mut ();
25-
let mut _11: *const [bool; 0];
22+
let _8: ();
23+
let mut _9: *mut ();
24+
let mut _10: *const [bool; 0];
2625
scope 11 (inlined core::ub_checks::check_language_ub) {
26+
let mut _11: bool;
2727
scope 12 (inlined core::ub_checks::check_language_ub::runtime) {
2828
}
2929
}
@@ -44,7 +44,7 @@
4444
StorageLive(_1);
4545
StorageLive(_2);
4646
StorageLive(_3);
47-
StorageLive(_9);
47+
StorageLive(_8);
4848
StorageLive(_4);
4949
StorageLive(_5);
5050
StorageLive(_6);
@@ -54,10 +54,10 @@
5454
+ _7 = const 1_usize;
5555
+ _6 = const {0x1 as *mut [bool; 0]};
5656
StorageDead(_7);
57+
StorageLive(_10);
5758
StorageLive(_11);
58-
StorageLive(_8);
59-
_8 = UbChecks();
60-
switchInt(move _8) -> [0: bb5, otherwise: bb3];
59+
_11 = UbChecks();
60+
switchInt(move _11) -> [0: bb6, otherwise: bb5];
6161
}
6262

6363
bb1: {
@@ -70,25 +70,16 @@
7070
}
7171

7272
bb3: {
73-
StorageLive(_10);
74-
- _10 = _6 as *mut () (PtrToPtr);
75-
- _9 = NonNull::<T>::new_unchecked::precondition_check(move _10) -> [return: bb4, unwind unreachable];
76-
+ _10 = const {0x1 as *mut ()};
77-
+ _9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb4, unwind unreachable];
73+
StorageDead(_9);
74+
goto -> bb4;
7875
}
7976

8077
bb4: {
81-
StorageDead(_10);
82-
goto -> bb5;
83-
}
84-
85-
bb5: {
86-
StorageDead(_8);
87-
- _11 = _6 as *const [bool; 0] (PointerCoercion(MutToConstPointer));
88-
- _5 = NonNull::<[bool; 0]> { pointer: _11 };
89-
+ _11 = const {0x1 as *const [bool; 0]};
78+
- _10 = _6 as *const [bool; 0] (PointerCoercion(MutToConstPointer));
79+
- _5 = NonNull::<[bool; 0]> { pointer: _10 };
80+
+ _10 = const {0x1 as *const [bool; 0]};
9081
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
91-
StorageDead(_11);
82+
StorageDead(_10);
9283
StorageDead(_6);
9384
- _4 = Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
9485
+ _4 = const Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
@@ -98,14 +89,28 @@
9889
StorageDead(_4);
9990
- _2 = Box::<[bool]>(_3, const std::alloc::Global);
10091
+ _2 = const Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global);
101-
StorageDead(_9);
92+
StorageDead(_8);
10293
StorageDead(_3);
10394
- _1 = A { foo: move _2 };
10495
+ _1 = const A {{ foo: Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC2, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global) }};
10596
StorageDead(_2);
10697
_0 = const ();
10798
drop(_1) -> [return: bb1, unwind: bb2];
10899
}
100+
101+
bb5: {
102+
StorageDead(_11);
103+
StorageLive(_9);
104+
- _9 = _6 as *mut () (PtrToPtr);
105+
- _8 = NonNull::<T>::new_unchecked::precondition_check(move _9) -> [return: bb3, unwind unreachable];
106+
+ _9 = const {0x1 as *mut ()};
107+
+ _8 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable];
108+
}
109+
110+
bb6: {
111+
StorageDead(_11);
112+
goto -> bb4;
113+
}
109114
+ }
110115
+
111116
+ ALLOC2 (size: 16, align: 8) {

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff

+29-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
+ scope 3 (inlined unreachable_unchecked) {
1313
+ let mut _4: bool;
1414
+ let _5: ();
15+
+ let mut _7: bool;
1516
+ scope 4 (inlined core::ub_checks::check_language_ub) {
17+
+ let mut _6: bool;
1618
+ scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1719
+ }
1820
+ }
@@ -30,26 +32,42 @@
3032
}
3133

3234
bb1: {
33-
- StorageDead(_2);
34-
- return;
3535
+ unreachable;
36-
}
37-
38-
- bb2 (cleanup): {
39-
- resume;
36+
+ }
37+
+
4038
+ bb2: {
39+
+ StorageLive(_7);
4140
+ StorageLive(_4);
42-
+ _4 = UbChecks();
43-
+ assume(_4);
44-
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
41+
+ StorageLive(_6);
42+
+ _6 = UbChecks();
43+
+ switchInt(move _6) -> [0: bb5, otherwise: bb4];
4544
+ }
4645
+
4746
+ bb3: {
4847
+ _0 = move ((_2 as Some).0: T);
4948
+ StorageDead(_5);
5049
+ StorageDead(_3);
51-
+ StorageDead(_2);
52-
+ return;
50+
StorageDead(_2);
51+
return;
52+
}
53+
54+
- bb2 (cleanup): {
55+
- resume;
56+
+ bb4: {
57+
+ _4 = const true;
58+
+ goto -> bb6;
59+
+ }
60+
+
61+
+ bb5: {
62+
+ _4 = const false;
63+
+ goto -> bb6;
64+
+ }
65+
+
66+
+ bb6: {
67+
+ StorageDead(_6);
68+
+ _7 = _4;
69+
+ assume(move _7);
70+
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
5371
}
5472
}
5573

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
88
scope 2 {
99
}
1010
scope 3 (inlined unreachable_unchecked) {
11+
let mut _3: bool;
12+
let _4: ();
1113
scope 4 (inlined core::ub_checks::check_language_ub) {
1214
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1315
}
@@ -18,16 +20,22 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
1820
bb0: {
1921
StorageLive(_2);
2022
_2 = discriminant(_1);
21-
switchInt(move _2) -> [0: bb2, 1: bb1, otherwise: bb2];
23+
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb3];
2224
}
2325

2426
bb1: {
27+
_3 = const false;
28+
assume(_3);
29+
_4 = unreachable_unchecked::precondition_check() -> [return: bb3, unwind unreachable];
30+
}
31+
32+
bb2: {
2533
_0 = ((_1 as Some).0: T);
2634
StorageDead(_2);
2735
return;
2836
}
2937

30-
bb2: {
38+
bb3: {
3139
unreachable;
3240
}
3341
}

tests/mir-opt/instsimplify/ub_check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ unit-test: InstSimplify
2+
//@ should-fail Broken due to https://github.com/rust-lang/rust/issues/124150.
23
//@ compile-flags: -Cdebug-assertions=no -Zinline-mir
34

45
// EMIT_MIR ub_check.unwrap_unchecked.InstSimplify.diff

tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify.diff

+24-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
scope 3 (inlined unreachable_unchecked) {
1313
let mut _4: bool;
1414
let _5: ();
15+
let mut _7: bool;
1516
scope 4 (inlined core::ub_checks::check_language_ub) {
17+
let mut _6: bool;
1618
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1719
}
1820
}
@@ -33,11 +35,12 @@
3335
}
3436

3537
bb2: {
38+
StorageLive(_7);
3639
StorageLive(_4);
37-
- _4 = UbChecks();
38-
+ _4 = const false;
39-
assume(_4);
40-
_5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
40+
StorageLive(_6);
41+
- _6 = UbChecks();
42+
+ _6 = const false;
43+
switchInt(move _6) -> [0: bb5, otherwise: bb4];
4144
}
4245

4346
bb3: {
@@ -47,5 +50,22 @@
4750
StorageDead(_2);
4851
return;
4952
}
53+
54+
bb4: {
55+
_4 = const true;
56+
goto -> bb6;
57+
}
58+
59+
bb5: {
60+
_4 = const false;
61+
goto -> bb6;
62+
}
63+
64+
bb6: {
65+
StorageDead(_6);
66+
_7 = _4;
67+
assume(move _7);
68+
_5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
69+
}
5070
}
5171

tests/mir-opt/issues/issue_75439.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
//@ unit-test: MatchBranchSimplification
12
// skip-filecheck
2-
// EMIT_MIR issue_75439.foo.MatchBranchSimplification.diff
33
//@ ignore-endian-big
44

55
use std::mem::transmute;
66

7+
// EMIT_MIR issue_75439.foo.MatchBranchSimplification.diff
78
pub fn foo(bytes: [u8; 16]) -> Option<[u8; 4]> {
89
// big endian `u32`s
910
let dwords: [u32; 4] = unsafe { transmute(bytes) };

tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ fn ub_if_b(_1: Thing) -> Thing {
55
let mut _0: Thing;
66
let mut _2: isize;
77
scope 1 (inlined unreachable_unchecked) {
8+
let mut _3: bool;
9+
let _4: ();
810
scope 2 (inlined core::ub_checks::check_language_ub) {
911
scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
1012
}
@@ -13,7 +15,7 @@ fn ub_if_b(_1: Thing) -> Thing {
1315

1416
bb0: {
1517
_2 = discriminant(_1);
16-
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb2];
18+
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb3];
1719
}
1820

1921
bb1: {
@@ -22,6 +24,12 @@ fn ub_if_b(_1: Thing) -> Thing {
2224
}
2325

2426
bb2: {
27+
_3 = const false;
28+
assume(_3);
29+
_4 = unreachable_unchecked::precondition_check() -> [return: bb3, unwind unreachable];
30+
}
31+
32+
bb3: {
2533
unreachable;
2634
}
2735
}

0 commit comments

Comments
 (0)