Skip to content

Fix uninhabited enum branching pass #85603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions compiler/rustc_mir/src/transform/uninhabited_enum_branching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {
/// discriminant is read from. Otherwise, returns None.
fn get_switched_on_type<'tcx>(
block_data: &BasicBlockData<'tcx>,
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
) -> Option<Ty<'tcx>> {
let terminator = block_data.terminator();
Expand All @@ -36,12 +37,9 @@ fn get_switched_on_type<'tcx>(
if let Some(StatementKind::Assign(box (l, Rvalue::Discriminant(place)))) = stmt_before_term
{
if l.as_local() == Some(local) {
if let Some(r_local) = place.as_local() {
let ty = body.local_decls[r_local].ty;

if ty.is_enum() {
return Some(ty);
}
let ty = place.ty(body, tcx).ty;
if ty.is_enum() {
return Some(ty);
}
}
}
Expand Down Expand Up @@ -86,7 +84,7 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
trace!("processing block {:?}", bb);

let discriminant_ty =
if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], body) {
if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) {
ty
} else {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// MIR for `main` after SimplifyCfg-after-uninhabited-enum-branching

fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/uninhabited_enum_branching2.rs:18:11: 18:11
let _1: Plop; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
let mut _2: Test1; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
let _3: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
let mut _4: &Test1; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
let mut _5: isize; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
let _6: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:23:24: 23:34
let _7: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
let _8: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
let _9: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
let mut _10: isize; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
let _11: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:30:24: 30:34
let _12: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
let _13: &str; // in scope 0 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
scope 1 {
debug plop => _1; // in scope 1 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:9: 19:13
StorageLive(_2); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
discriminant(_2) = 2; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:38: 19:46
(_1.0: u32) = const 51_u32; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:16: 19:48
(_1.1: Test1) = move _2; // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:16: 19:48
StorageDead(_2); // scope 0 at $DIR/uninhabited_enum_branching2.rs:19:47: 19:48
StorageLive(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
StorageLive(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
_4 = &(_1.1: Test1); // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:11: 21:22
_5 = discriminant((*_4)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
switchInt(move _5) -> [2_isize: bb2, otherwise: bb1]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:22:9: 22:20
}

bb1: {
StorageLive(_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
_8 = const "D"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
// ty::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 })
// mir::Constant
// + span: $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
// + 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 }) }
_3 = &(*_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:21: 25:24
StorageDead(_8); // scope 1 at $DIR/uninhabited_enum_branching2.rs:25:23: 25:24
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
}

bb2: {
StorageLive(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
_7 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
// ty::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 })
// mir::Constant
// + span: $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
// + 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 }) }
_3 = &(*_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:21: 24:24
StorageDead(_7); // scope 1 at $DIR/uninhabited_enum_branching2.rs:24:23: 24:24
goto -> bb3; // scope 1 at $DIR/uninhabited_enum_branching2.rs:21:5: 26:6
}

bb3: {
StorageDead(_4); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
StorageDead(_3); // scope 1 at $DIR/uninhabited_enum_branching2.rs:26:6: 26:7
StorageLive(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
_10 = discriminant((_1.1: Test1)); // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
switchInt(move _10) -> [2_isize: bb5, otherwise: bb4]; // scope 1 at $DIR/uninhabited_enum_branching2.rs:29:9: 29:20
}

bb4: {
StorageLive(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
_13 = const "D"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
// ty::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 })
// mir::Constant
// + span: $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
// + 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 }) }
_9 = &(*_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:21: 32:24
StorageDead(_13); // scope 1 at $DIR/uninhabited_enum_branching2.rs:32:23: 32:24
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
}

bb5: {
StorageLive(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
_12 = const "C"; // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
// ty::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 })
// mir::Constant
// + span: $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
// + 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 }) }
_9 = &(*_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:21: 31:24
StorageDead(_12); // scope 1 at $DIR/uninhabited_enum_branching2.rs:31:23: 31:24
goto -> bb6; // scope 1 at $DIR/uninhabited_enum_branching2.rs:28:5: 33:6
}

bb6: {
StorageDead(_9); // scope 1 at $DIR/uninhabited_enum_branching2.rs:33:6: 33:7
_0 = const (); // scope 0 at $DIR/uninhabited_enum_branching2.rs:18:11: 34:2
StorageDead(_1); // scope 0 at $DIR/uninhabited_enum_branching2.rs:34:1: 34:2
return; // scope 0 at $DIR/uninhabited_enum_branching2.rs:34:2: 34:2
}
}
Loading