Skip to content
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

Fix variant index / discriminant confusion in uninhabited enum branching #89764

Merged
merged 3 commits into from
Jan 20, 2022
Merged
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
17 changes: 6 additions & 11 deletions compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use crate::MirPass;
use rustc_data_structures::stable_set::FxHashSet;
use rustc_middle::mir::{
BasicBlock, BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, SwitchTargets,
TerminatorKind,
BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, SwitchTargets, TerminatorKind,
};
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{Ty, TyCtxt};
Expand Down Expand Up @@ -56,7 +55,10 @@ fn variant_discriminants<'tcx>(
match &layout.variants {
Variants::Single { index } => {
let mut res = FxHashSet::default();
res.insert(index.as_u32() as u128);
res.insert(
ty.discriminant_for_variant(tcx, *index)
.map_or(index.as_u32() as u128, |discr| discr.val),
);
res
}
Variants::Multiple { variants, .. } => variants
Expand All @@ -71,16 +73,9 @@ fn variant_discriminants<'tcx>(

impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if body.source.promoted.is_some() {
return;
}

trace!("UninhabitedEnumBranching starting for {:?}", body.source);

let basic_block_count = body.basic_blocks().len();

for bb in 0..basic_block_count {
let bb = BasicBlock::from_usize(bb);
for bb in body.basic_blocks().indices() {
trace!("processing block {:?}", bb);

let Some(discriminant_ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) else {
Expand Down