/
uninhabited_enum_branching.rs
216 lines (196 loc) · 5.99 KB
/
uninhabited_enum_branching.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// unit-test: UninhabitedEnumBranching
enum Empty {}
// test matching an enum with uninhabited variants
enum Test1 {
A(Empty),
B(Empty),
C,
}
// test an enum where the discriminants don't match the variant indexes
// (the optimization should do nothing here)
enum Test2 {
D = 4,
E = 5,
}
// test matching an enum with uninhabited variants and multiple inhabited
enum Test3 {
A(Empty),
B(Empty),
C,
D,
}
enum Test4 {
A(i32),
B(i32),
C,
D,
}
#[repr(i8)]
enum Test5<T> {
A(T) = -1,
B(T) = 0,
C = 5,
D = 3,
}
struct Plop {
xx: u32,
test3: Test3,
}
// EMIT_MIR uninhabited_enum_branching.simple.UninhabitedEnumBranching.diff
fn simple() {
// CHECK-LABEL: fn simple(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: bb1, otherwise: [[unreachable]]];
// CHECK: [[unreachable]]: {
// CHECK-NEXT: unreachable;
match Test1::C {
Test1::A(_) => "A(Empty)",
Test1::B(_) => "B(Empty)",
Test1::C => "C",
};
}
// EMIT_MIR uninhabited_enum_branching.custom_discriminant.UninhabitedEnumBranching.diff
fn custom_discriminant() {
// CHECK-LABEL: fn custom_discriminant(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [4: bb3, 5: bb1, otherwise: bb5];
// CHECK: bb5: {
// CHECK-NEXT: unreachable;
match Test2::D {
Test2::D => "D",
Test2::E => "E",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t1.UninhabitedEnumBranching.diff
fn otherwise_t1() {
// CHECK-LABEL: fn otherwise_t1(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb5, 1: bb5, 2: bb1, otherwise: bb5];
// CHECK: bb5: {
// CHECK-NEXT: unreachable;
match Test1::C {
Test1::A(_) => "A(Empty)",
Test1::B(_) => "B(Empty)",
_ => "C",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t2.UninhabitedEnumBranching.diff
fn otherwise_t2() {
// CHECK-LABEL: fn otherwise_t2(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [4: bb2, 5: bb1, otherwise: bb4];
// CHECK: bb4: {
// CHECK-NEXT: unreachable;
match Test2::D {
Test2::D => "D",
_ => "E",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t3.UninhabitedEnumBranching.diff
fn otherwise_t3() {
// CHECK-LABEL: fn otherwise_t3(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb5, 1: bb5, otherwise: bb1];
// CHECK: bb1: {
// CHECK-NOT: unreachable;
// CHECK: }
// CHECK: bb5: {
// CHECK-NEXT: unreachable;
match Test3::C {
Test3::A(_) => "A(Empty)",
Test3::B(_) => "B(Empty)",
_ => "C",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4_uninhabited_default.UninhabitedEnumBranching.diff
fn otherwise_t4_uninhabited_default() {
// CHECK-LABEL: fn otherwise_t4_uninhabited_default(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, 3: bb1, otherwise: bb6];
// CHECK: bb6: {
// CHECK-NEXT: unreachable;
match Test4::C {
Test4::A(_) => "A(i32)",
Test4::B(_) => "B(i32)",
Test4::C => "C",
_ => "D",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.diff
fn otherwise_t4_uninhabited_default_2() {
// CHECK-LABEL: fn otherwise_t4_uninhabited_default_2(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb1, 1: bb5, 2: bb6, 3: bb2, otherwise: bb8];
// CHECK: bb8: {
// CHECK-NEXT: unreachable;
match Test4::C {
Test4::A(1) => "A(1)",
Test4::A(2) => "A(2)",
Test4::B(_) => "B(i32)",
Test4::C => "C",
_ => "A(other)D",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4.UninhabitedEnumBranching.diff
fn otherwise_t4() {
// CHECK-LABEL: fn otherwise_t4(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, otherwise: bb1];
// CHECK: bb1: {
// CHECK-NOT: unreachable;
// CHECK: }
match Test4::C {
Test4::A(_) => "A(i32)",
Test4::B(_) => "B(i32)",
_ => "CD",
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.diff
fn otherwise_t5_uninhabited_default<T>() {
// CHECK-LABEL: fn otherwise_t5_uninhabited_default(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
// CHECK: [[unreachable]]: {
// CHECK-NEXT: unreachable;
match Test5::<T>::C {
Test5::A(_) => "A(T)",
Test5::B(_) => "B(T)",
Test5::C => "C",
_ => "D",
};
}
// 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((*
// CHECK: switchInt(move [[ref_discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: bb5, 3: bb1, otherwise: [[unreachable]]];
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();
otherwise_t1();
otherwise_t2();
otherwise_t3();
otherwise_t4_uninhabited_default();
otherwise_t4_uninhabited_default_2();
otherwise_t4();
otherwise_t5_uninhabited_default::<i32>();
byref();
}