Skip to content

Commit 8d0f254

Browse files
committed
Don't modify the otherwise of the switch
1 parent 30b690b commit 8d0f254

7 files changed

+60
-42
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
166166
};
167167
(value, targets.all_targets()[0])
168168
});
169-
let eq_targets = SwitchTargets::new(eq_new_targets, opt_data.destination);
169+
170+
let eq_targets = SwitchTargets::new(eq_new_targets, parent_targets.otherwise());
170171

171172
// Create `bbEq` in example above
172173
let eq_switch = BasicBlockData::new(Some(Terminator {

tests/mir-opt/early_otherwise_branch.opt10.EarlyOtherwiseBranch.diff

+29-27
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
2828
+ StorageLive(_10);
2929
+ _10 = discriminant((_3.1: E16));
30-
+ switchInt(move _10) -> [0: bb6, otherwise: bb1];
30+
+ switchInt(move _10) -> [0: bb7, otherwise: bb1];
3131
}
3232

3333
bb1: {
@@ -40,49 +40,51 @@
4040
bb2: {
4141
- _6 = discriminant((_3.1: E16));
4242
- switchInt(move _6) -> [0: bb5, otherwise: bb1];
43-
- }
44-
-
45-
- bb3: {
43+
+ _0 = const 1_u32;
44+
+ goto -> bb5;
45+
}
46+
47+
bb3: {
4648
- _7 = discriminant((_3.1: E16));
4749
- switchInt(move _7) -> [0: bb6, otherwise: bb1];
48-
- }
49-
-
50-
- bb4: {
50+
+ _0 = const 2_u32;
51+
+ goto -> bb5;
52+
}
53+
54+
bb4: {
5155
- _8 = discriminant((_3.1: E16));
5256
- switchInt(move _8) -> [0: bb7, otherwise: bb1];
53-
- }
54-
-
55-
- bb5: {
56-
_0 = const 1_u32;
57-
- goto -> bb8;
57+
+ _0 = const 3_u32;
5858
+ goto -> bb5;
5959
}
6060

61-
- bb6: {
62-
+ bb3: {
63-
_0 = const 2_u32;
61+
bb5: {
62+
- _0 = const 1_u32;
6463
- goto -> bb8;
65-
+ goto -> bb5;
64+
+ StorageDead(_3);
65+
+ return;
6666
}
6767

68-
- bb7: {
69-
+ bb4: {
70-
_0 = const 3_u32;
68+
bb6: {
69+
- _0 = const 2_u32;
7170
- goto -> bb8;
72-
+ goto -> bb5;
71+
+ unreachable;
7372
}
7473

74+
bb7: {
75+
- _0 = const 3_u32;
76+
- goto -> bb8;
77+
- }
78+
-
7579
- bb8: {
76-
+ bb5: {
77-
StorageDead(_3);
78-
return;
79-
}
80-
80+
- StorageDead(_3);
81+
- return;
82+
- }
83+
-
8184
- bb9: {
8285
- unreachable;
83-
+ bb6: {
8486
+ StorageDead(_10);
85-
+ switchInt(_9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
87+
+ switchInt(_9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb6];
8688
}
8789
}
8890

tests/mir-opt/early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
+ StorageLive(_12);
3737
+ _12 = Ne(_8, move _11);
3838
+ StorageDead(_11);
39-
+ switchInt(move _12) -> [0: bb5, otherwise: bb1];
39+
+ switchInt(move _12) -> [0: bb6, otherwise: bb1];
4040
}
4141

4242
bb1: {
@@ -82,10 +82,13 @@
8282
}
8383

8484
- bb7: {
85-
- unreachable;
8685
+ bb5: {
86+
unreachable;
87+
+ }
88+
+
89+
+ bb6: {
8790
+ StorageDead(_12);
88-
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
91+
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
8992
}
9093
}
9194

tests/mir-opt/early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
+ StorageLive(_12);
3737
+ _12 = Ne(_8, move _11);
3838
+ StorageDead(_11);
39-
+ switchInt(move _12) -> [0: bb5, otherwise: bb1];
39+
+ switchInt(move _12) -> [0: bb6, otherwise: bb1];
4040
}
4141

4242
bb1: {
@@ -82,10 +82,13 @@
8282
}
8383

8484
- bb7: {
85-
- unreachable;
8685
+ bb5: {
86+
unreachable;
87+
+ }
88+
+
89+
+ bb6: {
8790
+ StorageDead(_12);
88-
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
91+
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
8992
}
9093
}
9194

tests/mir-opt/early_otherwise_branch.opt7.EarlyOtherwiseBranch.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- switchInt(move _8) -> [0: bb2, 1: bb3, otherwise: bb7];
3737
+ StorageLive(_12);
3838
+ _12 = discriminant((_3.1: std::option::Option<u32>));
39-
+ switchInt(move _12) -> [1: bb5, otherwise: bb1];
39+
+ switchInt(move _12) -> [1: bb6, otherwise: bb1];
4040
}
4141

4242
bb1: {
@@ -85,10 +85,13 @@
8585
}
8686

8787
- bb7: {
88-
- unreachable;
8988
+ bb5: {
89+
unreachable;
90+
+ }
91+
+
92+
+ bb6: {
9093
+ StorageDead(_12);
91-
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
94+
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
9295
}
9396
}
9497

tests/mir-opt/early_otherwise_branch.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ fn opt10(x: E8, y: E16) -> u32 {
170170
// CHECK: bb0: {
171171
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
172172
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
173-
// CHECK: switchInt(move [[LOCAL2]]) -> [0: [[SWITCH_BB:bb.*]], otherwise: [[OTHERWISE:bb.*]]];
173+
// CHECK: switchInt(move [[LOCAL2]]) -> [0: [[SWITCH_BB:bb.*]], otherwise: {{bb.*}}];
174+
// CHECK-NEXT: }
175+
// CHECK: [[UNREACHABLE:bb6]]: {
176+
// CHECK-NEXT: unreachable;
174177
// CHECK-NEXT: }
175178
// CHECK: [[SWITCH_BB]]:
176-
// CHECK: switchInt([[LOCAL1]]) -> [0: bb{{.*}}, 1: bb{{.*}}, 2: bb{{.*}}, otherwise: [[OTHERWISE]]];
179+
// CHECK: switchInt([[LOCAL1]]) -> [0: bb{{.*}}, 1: bb{{.*}}, 2: bb{{.*}}, otherwise: [[UNREACHABLE]]];
177180
// CHECK-NEXT: }
178181
match (x, y) {
179182
(E8::A, E16::A) => 1,

tests/mir-opt/early_otherwise_branch_3_element_tuple.opt1.EarlyOtherwiseBranch.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
+ StorageLive(_17);
4646
+ _17 = Ne(_12, move _16);
4747
+ StorageDead(_16);
48-
+ switchInt(move _17) -> [0: bb7, otherwise: bb1];
48+
+ switchInt(move _17) -> [0: bb8, otherwise: bb1];
4949
}
5050

5151
bb1: {
@@ -108,10 +108,13 @@
108108
}
109109

110110
- bb9: {
111-
- unreachable;
112111
+ bb7: {
112+
unreachable;
113+
+ }
114+
+
115+
+ bb8: {
113116
+ StorageDead(_17);
114-
+ switchInt(_12) -> [0: bb2, 1: bb3, otherwise: bb1];
117+
+ switchInt(_12) -> [0: bb2, 1: bb3, otherwise: bb7];
115118
}
116119
}
117120

0 commit comments

Comments
 (0)