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 switch on discriminant detection in a presence of coverage counters #93870

Merged
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
33 changes: 18 additions & 15 deletions compiler/rustc_mir_dataflow/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,24 +710,27 @@ fn switch_on_enum_discriminant<'mir, 'tcx>(
block: &'mir mir::BasicBlockData<'tcx>,
switch_on: mir::Place<'tcx>,
) -> Option<(mir::Place<'tcx>, &'tcx ty::AdtDef)> {
match block.statements.last().map(|stmt| &stmt.kind) {
Some(mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated))))
if *lhs == switch_on =>
{
match &discriminated.ty(body, tcx).ty.kind() {
ty::Adt(def, _) => Some((*discriminated, def)),

// `Rvalue::Discriminant` is also used to get the active yield point for a
// generator, but we do not need edge-specific effects in that case. This may
// change in the future.
ty::Generator(..) => None,

t => bug!("`discriminant` called on unexpected type {:?}", t),
for statement in block.statements.iter().rev() {
match &statement.kind {
mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated)))
if *lhs == switch_on =>
{
match &discriminated.ty(body, tcx).ty.kind() {
ty::Adt(def, _) => return Some((*discriminated, def)),

// `Rvalue::Discriminant` is also used to get the active yield point for a
// generator, but we do not need edge-specific effects in that case. This may
// change in the future.
ty::Generator(..) => return None,

t => bug!("`discriminant` called on unexpected type {:?}", t),
}
}
mir::StatementKind::Coverage(_) => continue,
_ => return None,
}

_ => None,
}
None
}

struct OnMutBorrow<F>(F);
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/consts/precise-drop-with-coverage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Checks that code coverage doesn't interfere with const_precise_live_drops.
// Regression test for issue #93848.
//
// check-pass
// compile-flags: --crate-type=lib -Cinstrument-coverage -Zno-profiler-runtime

#![feature(const_precise_live_drops)]

#[inline]
pub const fn transpose<T, E>(this: Option<Result<T, E>>) -> Result<Option<T>, E> {
match this {
Some(Ok(x)) => Ok(Some(x)),
Some(Err(e)) => Err(e),
None => Ok(None),
}
}