diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index b1ce0069b43a8..766837d778681 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -54,13 +54,13 @@ pub(super) fn extract_refined_covspans<'tcx>( return false; } - // Each pushed covspan should have the same context as the body span. - // If it somehow doesn't, discard the covspan, or panic in debug builds. - if !body_span.eq_ctxt(covspan_span) { - debug_assert!( - false, - "span context mismatch: body_span={body_span:?}, covspan.span={covspan_span:?}" - ); + // Each covspan should have the same context as the body span. + // For nested macro expansions, contexts might differ. In that case, + // we check if the covspan resulted from a macro expansion that happened + // inside the function body. + if !body_span.eq_ctxt(covspan_span) + && covspan_span.find_ancestor_inside(body_span).is_none() + { return false; } diff --git a/tests/ui/instrument-coverage/nested-macro-coverage-issue-147339.rs b/tests/ui/instrument-coverage/nested-macro-coverage-issue-147339.rs new file mode 100644 index 0000000000000..1e6dd2d66a9f1 --- /dev/null +++ b/tests/ui/instrument-coverage/nested-macro-coverage-issue-147339.rs @@ -0,0 +1,19 @@ +//@ check-pass +//@ compile-flags: -Cinstrument-coverage -Zno-profiler-runtime +//@ edition: 2024 + +// Regression test for issue #147339 +// Nested macro expansions should not cause ICE during coverage instrumentation + +macro_rules! foo { + ($($m:ident $($f:ident $v:tt)+),*) => { + $($(macro_rules! $f { () => { $v } })+)* + $(macro_rules! $m { () => { $(fn $f() -> i32 { $v })+ } })* + } +} + +foo!(m a 1 b 2, n c 3); +m!(); +n!(); + +fn main() {}