Skip to content

Commit 5bece28

Browse files
committed
unhinabited_enum_branching: Fix the pass when the enum is taken indirectly
If there is a projection on the place of the discriminent, the pass wouldn't trigger
1 parent 5ee6897 commit 5bece28

3 files changed

+19
-69
lines changed

compiler/rustc_mir/src/transform/uninhabited_enum_branching.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {
2424
/// discriminant is read from. Otherwise, returns None.
2525
fn get_switched_on_type<'tcx>(
2626
block_data: &BasicBlockData<'tcx>,
27+
tcx: TyCtxt<'tcx>,
2728
body: &Body<'tcx>,
2829
) -> Option<Ty<'tcx>> {
2930
let terminator = block_data.terminator();
@@ -36,12 +37,9 @@ fn get_switched_on_type<'tcx>(
3637
if let Some(StatementKind::Assign(box (l, Rvalue::Discriminant(place)))) = stmt_before_term
3738
{
3839
if l.as_local() == Some(local) {
39-
if let Some(r_local) = place.as_local() {
40-
let ty = body.local_decls[r_local].ty;
41-
42-
if ty.is_enum() {
43-
return Some(ty);
44-
}
40+
let ty = place.ty(body, tcx).ty;
41+
if ty.is_enum() {
42+
return Some(ty);
4543
}
4644
}
4745
}
@@ -86,7 +84,7 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
8684
trace!("processing block {:?}", bb);
8785

8886
let discriminant_ty =
89-
if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], body) {
87+
if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) {
9088
ty
9189
} else {
9290
continue;

src/test/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir

+10-60
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> () {
3030
StorageLive(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
3131
_4 = &(_1.1: Test1); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
3232
_5 = discriminant((*_4)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
33-
switchInt(move _5) -> [0_isize: bb2, 1_isize: bb3, 2_isize: bb4, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
33+
switchInt(move _5) -> [2_isize: bb2, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
3434
}
3535

3636
bb1: {
@@ -44,35 +44,10 @@ fn main() -> () {
4444
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [68], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1], len: Size { raw: 1 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 1 }) }
4545
_3 = &(*_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
4646
StorageDead(_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:23: 25:24
47-
goto -> bb5; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
47+
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
4848
}
4949

5050
bb2: {
51-
_3 = const "A(Empty)"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:24: 22:34
52-
// ty::Const
53-
// + ty: &str
54-
// + val: Value(Slice { data: Allocation { bytes: [65, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 })
55-
// mir::Constant
56-
// + span: $DIR/uninhabited_enum_branching2.rs:22:24: 22:34
57-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [65, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }) }
58-
goto -> bb5; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
59-
}
60-
61-
bb3: {
62-
StorageLive(_6); // scope 1 at $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
63-
_6 = const "B(Empty)"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
64-
// ty::Const
65-
// + ty: &str
66-
// + val: Value(Slice { data: Allocation { bytes: [66, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 })
67-
// mir::Constant
68-
// + span: $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
69-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [66, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }) }
70-
_3 = &(*_6); // scope 1 at $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
71-
StorageDead(_6); // scope 1 at $DIR/uninhabited_enum_branching2.rs:23:33: 23:34
72-
goto -> bb5; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
73-
}
74-
75-
bb4: {
7651
StorageLive(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
7752
_7 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
7853
// ty::Const
@@ -83,18 +58,18 @@ fn main() -> () {
8358
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [67], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1], len: Size { raw: 1 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 1 }) }
8459
_3 = &(*_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
8560
StorageDead(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:23: 24:24
86-
goto -> bb5; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
61+
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
8762
}
8863

89-
bb5: {
64+
bb3: {
9065
StorageDead(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
9166
StorageDead(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
9267
StorageLive(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
9368
_10 = discriminant((_1.1: Test1)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
94-
switchInt(move _10) -> [0_isize: bb7, 1_isize: bb8, 2_isize: bb9, otherwise: bb6]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
69+
switchInt(move _10) -> [2_isize: bb5, otherwise: bb4]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
9570
}
9671

97-
bb6: {
72+
bb4: {
9873
StorageLive(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
9974
_13 = const "D"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
10075
// ty::Const
@@ -105,35 +80,10 @@ fn main() -> () {
10580
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [68], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1], len: Size { raw: 1 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 1 }) }
10681
_9 = &(*_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
10782
StorageDead(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:23: 32:24
108-
goto -> bb10; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
109-
}
110-
111-
bb7: {
112-
_9 = const "A(Empty)"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:24: 29:34
113-
// ty::Const
114-
// + ty: &str
115-
// + val: Value(Slice { data: Allocation { bytes: [65, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 })
116-
// mir::Constant
117-
// + span: $DIR/uninhabited_enum_branching2.rs:29:24: 29:34
118-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [65, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }) }
119-
goto -> bb10; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
83+
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
12084
}
12185

122-
bb8: {
123-
StorageLive(_11); // scope 1 at $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
124-
_11 = const "B(Empty)"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
125-
// ty::Const
126-
// + ty: &str
127-
// + val: Value(Slice { data: Allocation { bytes: [66, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 })
128-
// mir::Constant
129-
// + span: $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
130-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [66, 40, 69, 109, 112, 116, 121, 41], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }) }
131-
_9 = &(*_11); // scope 1 at $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
132-
StorageDead(_11); // scope 1 at $DIR/uninhabited_enum_branching2.rs:30:33: 30:34
133-
goto -> bb10; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
134-
}
135-
136-
bb9: {
86+
bb5: {
13787
StorageLive(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
13888
_12 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
13989
// ty::Const
@@ -144,10 +94,10 @@ fn main() -> () {
14494
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [67], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1], len: Size { raw: 1 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 1 }) }
14595
_9 = &(*_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
14696
StorageDead(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:23: 31:24
147-
goto -> bb10; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
97+
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
14898
}
14999

150-
bb10: {
100+
bb6: {
151101
StorageDead(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:33:6: 33:7
152102
_0 = const (); // scope 0 at $DIR/uninhabited_enum_branching2.rs:18:11: 34:2
153103
StorageDead(_1); // scope 0 at $DIR/uninhabited_enum_branching2.rs:34:1: 34:2

src/test/mir-opt/uninhabited_enum_branching2.main.UninhabitedEnumBranching.diff

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
StorageLive(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
3232
_4 = &(_1.1: Test1); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
3333
_5 = discriminant((*_4)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
34-
switchInt(move _5) -> [0_isize: bb2, 1_isize: bb3, 2_isize: bb4, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
34+
- switchInt(move _5) -> [0_isize: bb2, 1_isize: bb3, 2_isize: bb4, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
35+
+ switchInt(move _5) -> [2_isize: bb4, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
3536
}
3637

3738
bb1: {
@@ -92,7 +93,8 @@
9293
StorageDead(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
9394
StorageLive(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
9495
_10 = discriminant((_1.1: Test1)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
95-
switchInt(move _10) -> [0_isize: bb7, 1_isize: bb8, 2_isize: bb9, otherwise: bb6]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
96+
- switchInt(move _10) -> [0_isize: bb7, 1_isize: bb8, 2_isize: bb9, otherwise: bb6]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
97+
+ switchInt(move _10) -> [2_isize: bb9, otherwise: bb6]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
9698
}
9799

98100
bb6: {

0 commit comments

Comments
 (0)