Skip to content

Commit af2420c

Browse files
committed
Get all variants to eliminate the default branching if we cannot get the layout of type
1 parent e8c09fd commit af2420c

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs

Lines changed: 6 additions & 0 deletions
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
};

tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.diff

Lines changed: 7 additions & 2 deletions
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

tests/mir-opt/uninhabited_enum_branching.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ enum Test4 {
3030
D,
3131
}
3232

33+
#[repr(i8)]
3334
enum Test5<T> {
34-
A(T),
35-
B(T),
36-
C,
37-
D,
35+
A(T) = -1,
36+
B(T) = 0,
37+
C = 5,
38+
D = 3,
3839
}
3940

4041
struct Plop {
@@ -163,10 +164,9 @@ fn otherwise_t4() {
163164
fn otherwise_t5_uninhabited_default<T>() {
164165
// CHECK-LABEL: fn otherwise_t5_uninhabited_default(
165166
// CHECK: [[discr:_.*]] = discriminant(
166-
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
167-
// CHECK: bb1: {
168-
// CHECK-NOT: unreachable;
169-
// CHECK: }
167+
// CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
168+
// CHECK: [[unreachable]]: {
169+
// CHECK-NEXT: unreachable;
170170
match Test5::<T>::C {
171171
Test5::A(_) => "A(T)",
172172
Test5::B(_) => "B(T)",

0 commit comments

Comments
 (0)