2023-10-21 13:45:25 +00:00
|
|
|
//@ unit-test: UninhabitedEnumBranching
|
2024-02-14 09:19:31 +08:00
|
|
|
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
|
|
|
|
|
2023-10-21 13:45:25 +00:00
|
|
|
enum Empty {}
|
2019-10-20 23:48:31 -04:00
|
|
|
|
|
|
|
// test matching an enum with uninhabited variants
|
|
|
|
enum Test1 {
|
|
|
|
A(Empty),
|
|
|
|
B(Empty),
|
2023-10-21 13:45:25 +00:00
|
|
|
C,
|
2019-10-20 23:48:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test an enum where the discriminants don't match the variant indexes
|
|
|
|
// (the optimization should do nothing here)
|
|
|
|
enum Test2 {
|
|
|
|
D = 4,
|
|
|
|
E = 5,
|
|
|
|
}
|
|
|
|
|
2023-10-21 13:45:25 +00:00
|
|
|
// test matching an enum with uninhabited variants and multiple inhabited
|
|
|
|
enum Test3 {
|
|
|
|
A(Empty),
|
|
|
|
B(Empty),
|
|
|
|
C,
|
|
|
|
D,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Plop {
|
|
|
|
xx: u32,
|
|
|
|
test3: Test3,
|
|
|
|
}
|
|
|
|
|
|
|
|
// EMIT_MIR uninhabited_enum_branching.simple.UninhabitedEnumBranching.diff
|
|
|
|
fn simple() {
|
|
|
|
// CHECK-LABEL: fn simple(
|
|
|
|
// CHECK: [[discr:_.*]] = discriminant(
|
2024-02-12 04:56:03 +01:00
|
|
|
// CHECK: switchInt(move [[discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: bb2, otherwise: [[unreachable]]];
|
2023-10-21 13:45:25 +00:00
|
|
|
// CHECK: [[unreachable]]: {
|
|
|
|
// CHECK-NEXT: unreachable;
|
2019-10-20 23:48:31 -04:00
|
|
|
match Test1::C {
|
|
|
|
Test1::A(_) => "A(Empty)",
|
|
|
|
Test1::B(_) => "B(Empty)",
|
|
|
|
Test1::C => "C",
|
|
|
|
};
|
2023-10-21 13:45:25 +00:00
|
|
|
}
|
2019-10-20 23:48:31 -04:00
|
|
|
|
2023-10-21 13:45:25 +00:00
|
|
|
// EMIT_MIR uninhabited_enum_branching.custom_discriminant.UninhabitedEnumBranching.diff
|
|
|
|
fn custom_discriminant() {
|
|
|
|
// CHECK-LABEL: fn custom_discriminant(
|
|
|
|
// CHECK: [[discr:_.*]] = discriminant(
|
2024-02-12 04:56:03 +01:00
|
|
|
// CHECK: switchInt(move [[discr]]) -> [4: bb3, 5: bb2, otherwise: bb5];
|
2023-10-21 13:45:25 +00:00
|
|
|
// CHECK: bb5: {
|
|
|
|
// CHECK-NEXT: unreachable;
|
2019-10-20 23:48:31 -04:00
|
|
|
match Test2::D {
|
|
|
|
Test2::D => "D",
|
|
|
|
Test2::E => "E",
|
|
|
|
};
|
|
|
|
}
|
2023-10-21 13:45:25 +00:00
|
|
|
|
|
|
|
// EMIT_MIR uninhabited_enum_branching.byref.UninhabitedEnumBranching.diff
|
|
|
|
fn byref() {
|
|
|
|
// CHECK-LABEL: fn byref(
|
|
|
|
let plop = Plop { xx: 51, test3: Test3::C };
|
|
|
|
|
|
|
|
// CHECK: [[ref_discr:_.*]] = discriminant((*
|
2024-02-12 04:56:03 +01:00
|
|
|
// CHECK: switchInt(move [[ref_discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: bb5, 3: bb2, otherwise: [[unreachable]]];
|
2023-10-21 13:45:25 +00:00
|
|
|
match &plop.test3 {
|
|
|
|
Test3::A(_) => "A(Empty)",
|
|
|
|
Test3::B(_) => "B(Empty)",
|
|
|
|
Test3::C => "C",
|
|
|
|
Test3::D => "D",
|
|
|
|
};
|
|
|
|
|
|
|
|
// CHECK: [[discr:_.*]] = discriminant(
|
|
|
|
// CHECK: switchInt(move [[discr]]) -> [0: [[unreachable]], 1: [[unreachable]], 2: bb10, 3: bb7, otherwise: [[unreachable]]];
|
|
|
|
match plop.test3 {
|
|
|
|
Test3::A(_) => "A(Empty)",
|
|
|
|
Test3::B(_) => "B(Empty)",
|
|
|
|
Test3::C => "C",
|
|
|
|
Test3::D => "D",
|
|
|
|
};
|
|
|
|
|
|
|
|
// CHECK: [[unreachable]]: {
|
|
|
|
// CHECK-NEXT: unreachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
simple();
|
|
|
|
custom_discriminant();
|
|
|
|
byref();
|
|
|
|
}
|