1
Fork 0

Fix switch on discriminant detection in a presence of coverage counters

This commit is contained in:
Tomasz Miąsko 2022-02-10 00:00:00 +00:00
parent 502d6aa47b
commit 63bf601537
2 changed files with 32 additions and 13 deletions

View file

@ -710,25 +710,28 @@ fn switch_on_enum_discriminant<'mir, 'tcx>(
block: &'mir mir::BasicBlockData<'tcx>, block: &'mir mir::BasicBlockData<'tcx>,
switch_on: mir::Place<'tcx>, switch_on: mir::Place<'tcx>,
) -> Option<(mir::Place<'tcx>, &'tcx ty::AdtDef)> { ) -> Option<(mir::Place<'tcx>, &'tcx ty::AdtDef)> {
match block.statements.last().map(|stmt| &stmt.kind) { for statement in block.statements.iter().rev() {
Some(mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated)))) match &statement.kind {
mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated)))
if *lhs == switch_on => if *lhs == switch_on =>
{ {
match &discriminated.ty(body, tcx).ty.kind() { match &discriminated.ty(body, tcx).ty.kind() {
ty::Adt(def, _) => Some((*discriminated, def)), ty::Adt(def, _) => return Some((*discriminated, def)),
// `Rvalue::Discriminant` is also used to get the active yield point for a // `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 // generator, but we do not need edge-specific effects in that case. This may
// change in the future. // change in the future.
ty::Generator(..) => None, ty::Generator(..) => return None,
t => bug!("`discriminant` called on unexpected type {:?}", t), t => bug!("`discriminant` called on unexpected type {:?}", t),
} }
} }
mir::StatementKind::Coverage(_) => continue,
_ => None, _ => return None,
} }
} }
None
}
struct OnMutBorrow<F>(F); struct OnMutBorrow<F>(F);

View file

@ -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),
}
}