Skip to content

Commit 3d7f8b4

Browse files
committed
Get all variants to eliminate the default branching if we cannot get the layout of type
1 parent 08ae838 commit 3d7f8b4

9 files changed

+60
-40
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
9595

9696
let mut allowed_variants = if let Ok(layout) = layout {
9797
variant_discriminants(&layout, discriminant_ty, tcx)
98+
} else if let Some(variant_range) = discriminant_ty.variant_range(tcx) {
99+
variant_range
100+
.map(|variant| {
101+
discriminant_ty.discriminant_for_variant(tcx, variant).unwrap().val
102+
})
103+
.collect()
98104
} else {
99105
continue;
100106
};

Diff for: tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir

+12-10
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ fn num_to_digit(_1: char) -> u32 {
3333
_3 = &_2;
3434
StorageLive(_4);
3535
_4 = discriminant(_2);
36-
StorageDead(_3);
37-
StorageDead(_2);
38-
switchInt(move _4) -> [1: bb2, otherwise: bb7];
36+
switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8];
3937
}
4038

4139
bb2: {
4240
StorageDead(_4);
41+
StorageDead(_3);
42+
StorageDead(_2);
4343
StorageLive(_5);
4444
_5 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind unreachable];
4545
}
4646

4747
bb3: {
4848
StorageLive(_6);
4949
_6 = discriminant(_5);
50-
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6];
50+
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8];
5151
}
5252

5353
bb4: {
@@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 {
5858
_0 = move ((_5 as Some).0: u32);
5959
StorageDead(_6);
6060
StorageDead(_5);
61-
goto -> bb8;
61+
goto -> bb7;
6262
}
6363

6464
bb6: {
65-
unreachable;
65+
StorageDead(_4);
66+
StorageDead(_3);
67+
StorageDead(_2);
68+
_0 = const 0_u32;
69+
goto -> bb7;
6670
}
6771

6872
bb7: {
69-
StorageDead(_4);
70-
_0 = const 0_u32;
71-
goto -> bb8;
73+
return;
7274
}
7375

7476
bb8: {
75-
return;
77+
unreachable;
7678
}
7779
}

Diff for: tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir

+12-10
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ fn num_to_digit(_1: char) -> u32 {
3333
_3 = &_2;
3434
StorageLive(_4);
3535
_4 = discriminant(_2);
36-
StorageDead(_3);
37-
StorageDead(_2);
38-
switchInt(move _4) -> [1: bb2, otherwise: bb7];
36+
switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8];
3937
}
4038

4139
bb2: {
4240
StorageDead(_4);
41+
StorageDead(_3);
42+
StorageDead(_2);
4343
StorageLive(_5);
4444
_5 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind continue];
4545
}
4646

4747
bb3: {
4848
StorageLive(_6);
4949
_6 = discriminant(_5);
50-
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6];
50+
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8];
5151
}
5252

5353
bb4: {
@@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 {
5858
_0 = move ((_5 as Some).0: u32);
5959
StorageDead(_6);
6060
StorageDead(_5);
61-
goto -> bb8;
61+
goto -> bb7;
6262
}
6363

6464
bb6: {
65-
unreachable;
65+
StorageDead(_4);
66+
StorageDead(_3);
67+
StorageDead(_2);
68+
_0 = const 0_u32;
69+
goto -> bb7;
6670
}
6771

6872
bb7: {
69-
StorageDead(_4);
70-
_0 = const 0_u32;
71-
goto -> bb8;
73+
return;
7274
}
7375

7476
bb8: {
75-
return;
77+
unreachable;
7678
}
7779
}

Diff for: tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
bb1: {
3030
_4 = discriminant((_1.1: std::option::Option<T>));
31-
switchInt(move _4) -> [0: bb2, otherwise: bb3];
31+
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5];
3232
}
3333

3434
bb2: {

Diff for: tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
bb1: {
3030
_4 = discriminant((_1.1: std::option::Option<T>));
31-
switchInt(move _4) -> [0: bb2, otherwise: bb3];
31+
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5];
3232
}
3333

3434
bb2: {

Diff for: tests/mir-opt/uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.panic-abort.diff

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616
StorageLive(_2);
1717
_2 = Test4::C;
1818
_3 = discriminant(_2);
19-
- switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, otherwise: bb2];
20-
+ switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, 3: bb2, otherwise: bb8];
19+
- switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, otherwise: bb1];
20+
+ switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, 3: bb1, otherwise: bb8];
2121
}
2222

2323
bb1: {
24-
switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb2];
25-
}
26-
27-
bb2: {
2824
StorageLive(_7);
2925
_7 = const "A(other)D";
3026
_1 = &(*_7);
3127
StorageDead(_7);
3228
goto -> bb7;
3329
}
3430

31+
bb2: {
32+
switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb1];
33+
}
34+
3535
bb3: {
3636
_1 = const "A(1)";
3737
goto -> bb7;

Diff for: tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-abort.diff

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let mut _0: ();
66
let _1: &str;
77
let mut _2: Test5<T>;
8-
let mut _3: isize;
8+
let mut _3: i8;
99
let _4: &str;
1010
let _5: &str;
1111
let _6: &str;
@@ -15,7 +15,8 @@
1515
StorageLive(_2);
1616
_2 = Test5::<T>::C;
1717
_3 = discriminant(_2);
18-
switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
18+
- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1];
19+
+ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb7];
1920
}
2021

2122
bb1: {
@@ -56,6 +57,10 @@
5657
StorageDead(_1);
5758
_0 = const ();
5859
return;
60+
+ }
61+
+
62+
+ bb7: {
63+
+ unreachable;
5964
}
6065
}
6166

Diff for: tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-unwind.diff

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let mut _0: ();
66
let _1: &str;
77
let mut _2: Test5<T>;
8-
let mut _3: isize;
8+
let mut _3: i8;
99
let _4: &str;
1010
let _5: &str;
1111
let _6: &str;
@@ -15,7 +15,8 @@
1515
StorageLive(_2);
1616
_2 = Test5::<T>::C;
1717
_3 = discriminant(_2);
18-
switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
18+
- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1];
19+
+ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb8];
1920
}
2021

2122
bb1: {
@@ -60,6 +61,10 @@
6061

6162
bb7 (cleanup): {
6263
resume;
64+
+ }
65+
+
66+
+ bb8: {
67+
+ unreachable;
6368
}
6469
}
6570

Diff for: tests/mir-opt/uninhabited_enum_branching.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ enum Test4 {
3232
D,
3333
}
3434

35+
#[repr(i8)]
3536
enum Test5<T> {
36-
A(T),
37-
B(T),
38-
C,
39-
D,
37+
A(T) = -1,
38+
B(T) = 0,
39+
C = 5,
40+
D = 3,
4041
}
4142

4243
struct Plop {
@@ -165,10 +166,9 @@ fn otherwise_t4() {
165166
fn otherwise_t5_uninhabited_default<T>() {
166167
// CHECK-LABEL: fn otherwise_t5_uninhabited_default(
167168
// CHECK: [[discr:_.*]] = discriminant(
168-
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
169-
// CHECK: bb1: {
170-
// CHECK-NOT: unreachable;
171-
// CHECK: }
169+
// CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
170+
// CHECK: [[unreachable]]: {
171+
// CHECK-NEXT: unreachable;
172172
match Test5::<T>::C {
173173
Test5::A(_) => "A(T)",
174174
Test5::B(_) => "B(T)",

0 commit comments

Comments
 (0)