Skip to content

Commit f726dbe

Browse files
committed
Auto merge of rust-lang#85603 - ogoffart:fix-uninhabited-enum-branching-pass, r=wesleywiser
Fix uninhabited enum branching pass when the discriminant is taken with some projection.
2 parents 4733f32 + 5bece28 commit f726dbe

4 files changed

+305
-7
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// MIR for `main` after SimplifyCfg-after-uninhabited-enum-branching
2+
3+
fn main() -> () {
4+
let mut _0: (); // return place in scope 0 at $DIR/uninhabited_enum_branching2.rs:18:11: 18:11
5+
let _1: Plop; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
6+
let mut _2: Test1; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
7+
let _3: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
8+
let mut _4: &Test1; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
9+
let mut _5: isize; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
10+
let _6: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
11+
let _7: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
12+
let _8: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
13+
let _9: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
14+
let mut _10: isize; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
15+
let _11: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
16+
let _12: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
17+
let _13: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
18+
scope 1 {
19+
debug plop => _1; // in scope 1 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
20+
}
21+
22+
bb0: {
23+
StorageLive(_1); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
24+
StorageLive(_2); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
25+
discriminant(_2) = 2; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
26+
(_1.0: u32) = const 51_u32; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:16: 19:48
27+
(_1.1: Test1) = move _2; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:16: 19:48
28+
StorageDead(_2); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:47: 19:48
29+
StorageLive(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
30+
StorageLive(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
31+
_4 = &(_1.1: Test1); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
32+
_5 = discriminant((*_4)); // 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
34+
}
35+
36+
bb1: {
37+
StorageLive(_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
38+
_8 = const "D"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
39+
// ty::Const
40+
// + ty: &str
41+
// + 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 })
42+
// mir::Constant
43+
// + span: $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
44+
// + 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 }) }
45+
_3 = &(*_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
46+
StorageDead(_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:23: 25:24
47+
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
48+
}
49+
50+
bb2: {
51+
StorageLive(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
52+
_7 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
53+
// ty::Const
54+
// + ty: &str
55+
// + 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 })
56+
// mir::Constant
57+
// + span: $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
58+
// + 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 }) }
59+
_3 = &(*_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
60+
StorageDead(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:23: 24:24
61+
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
62+
}
63+
64+
bb3: {
65+
StorageDead(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
66+
StorageDead(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
67+
StorageLive(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
68+
_10 = discriminant((_1.1: Test1)); // 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
70+
}
71+
72+
bb4: {
73+
StorageLive(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
74+
_13 = const "D"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
75+
// ty::Const
76+
// + ty: &str
77+
// + 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 })
78+
// mir::Constant
79+
// + span: $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
80+
// + 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 }) }
81+
_9 = &(*_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
82+
StorageDead(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:23: 32:24
83+
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
84+
}
85+
86+
bb5: {
87+
StorageLive(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
88+
_12 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
89+
// ty::Const
90+
// + ty: &str
91+
// + 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 })
92+
// mir::Constant
93+
// + span: $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
94+
// + 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 }) }
95+
_9 = &(*_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
96+
StorageDead(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:23: 31:24
97+
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
98+
}
99+
100+
bb6: {
101+
StorageDead(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:33:6: 33:7
102+
_0 = const (); // scope 0 at $DIR/uninhabited_enum_branching2.rs:18:11: 34:2
103+
StorageDead(_1); // scope 0 at $DIR/uninhabited_enum_branching2.rs:34:1: 34:2
104+
return; // scope 0 at $DIR/uninhabited_enum_branching2.rs:34:2: 34:2
105+
}
106+
}

0 commit comments

Comments
 (0)