Skip to content

Commit 0045310

Browse files
committed
coverage: Unexpand spans with find_ancestor_inside_same_ctxt
1 parent df5d535 commit 0045310

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::mir::{
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_span::def_id::LocalDefId;
2525
use rustc_span::source_map::SourceMap;
26-
use rustc_span::{ExpnKind, Span, Symbol};
26+
use rustc_span::{DesugaringKind, ExpnKind, Span, Symbol};
2727

2828
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
2929
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
@@ -346,20 +346,15 @@ fn get_body_span<'tcx>(
346346
let mut body_span = hir_body.value.span;
347347

348348
if tcx.is_closure(def_id.to_def_id()) {
349-
// If the MIR function is a closure, and if the closure body span
350-
// starts from a macro, but it's content is not in that macro, try
351-
// to find a non-macro callsite, and instrument the spans there
352-
// instead.
353-
loop {
354-
let expn_data = body_span.ctxt().outer_expn_data();
355-
if expn_data.is_root() {
356-
break;
357-
}
358-
if let ExpnKind::Macro { .. } = expn_data.kind {
359-
body_span = expn_data.call_site;
360-
} else {
361-
break;
362-
}
349+
// If the current function is a closure, and its "body" span was created
350+
// by macro expansion or async desugaring, try to walk backwards to the
351+
// pre-expansion call site or body.
352+
while let expn_data = body_span.ctxt().outer_expn_data()
353+
&& !expn_data.is_root()
354+
&& let ExpnKind::Macro { .. } | ExpnKind::Desugaring(DesugaringKind::Async) =
355+
expn_data.kind
356+
{
357+
body_span = expn_data.call_site;
363358
}
364359
}
365360

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,5 @@ fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
204204
/// etc.).
205205
#[inline]
206206
fn unexpand_into_body_span(span: Span, body_span: Span) -> Option<Span> {
207-
use rustc_span::source_map::original_sp;
208-
209-
// FIXME(#118525): Consider switching from `original_sp` to `Span::find_ancestor_inside`,
210-
// which is similar but gives slightly different results in some edge cases.
211-
let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
212-
body_span.contains(original_span).then_some(original_span)
207+
span.find_ancestor_inside_same_ctxt(body_span)
213208
}

0 commit comments

Comments
 (0)