Skip to content

Commit 63bf601

Browse files
committed
Fix switch on discriminant detection in a presence of coverage counters
1 parent 502d6aa commit 63bf601

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

compiler/rustc_mir_dataflow/src/impls/mod.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -710,24 +710,27 @@ fn switch_on_enum_discriminant<'mir, 'tcx>(
710710
block: &'mir mir::BasicBlockData<'tcx>,
711711
switch_on: mir::Place<'tcx>,
712712
) -> Option<(mir::Place<'tcx>, &'tcx ty::AdtDef)> {
713-
match block.statements.last().map(|stmt| &stmt.kind) {
714-
Some(mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated))))
715-
if *lhs == switch_on =>
716-
{
717-
match &discriminated.ty(body, tcx).ty.kind() {
718-
ty::Adt(def, _) => Some((*discriminated, def)),
719-
720-
// `Rvalue::Discriminant` is also used to get the active yield point for a
721-
// generator, but we do not need edge-specific effects in that case. This may
722-
// change in the future.
723-
ty::Generator(..) => None,
724-
725-
t => bug!("`discriminant` called on unexpected type {:?}", t),
713+
for statement in block.statements.iter().rev() {
714+
match &statement.kind {
715+
mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated)))
716+
if *lhs == switch_on =>
717+
{
718+
match &discriminated.ty(body, tcx).ty.kind() {
719+
ty::Adt(def, _) => return Some((*discriminated, def)),
720+
721+
// `Rvalue::Discriminant` is also used to get the active yield point for a
722+
// generator, but we do not need edge-specific effects in that case. This may
723+
// change in the future.
724+
ty::Generator(..) => return None,
725+
726+
t => bug!("`discriminant` called on unexpected type {:?}", t),
727+
}
726728
}
729+
mir::StatementKind::Coverage(_) => continue,
730+
_ => return None,
727731
}
728-
729-
_ => None,
730732
}
733+
None
731734
}
732735

733736
struct OnMutBorrow<F>(F);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Checks that code coverage doesn't interfere with const_precise_live_drops.
2+
// Regression test for issue #93848.
3+
//
4+
// check-pass
5+
// compile-flags: --crate-type=lib -Cinstrument-coverage -Zno-profiler-runtime
6+
7+
#![feature(const_precise_live_drops)]
8+
9+
#[inline]
10+
pub const fn transpose<T, E>(this: Option<Result<T, E>>) -> Result<Option<T>, E> {
11+
match this {
12+
Some(Ok(x)) => Ok(Some(x)),
13+
Some(Err(e)) => Err(e),
14+
None => Ok(None),
15+
}
16+
}

0 commit comments

Comments
 (0)