Skip to content

Commit fdd758a

Browse files
committed
coverage: Compare span source files without involving Lrc<SourceFile>
If we want to know whether two byte positions are in the same file, we don't need to clone and compare `Lrc<SourceFile>`; we can just get their indices and compare those instead.
1 parent b72c472 commit fdd758a

File tree

1 file changed

+14
-11
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+14
-11
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use self::spans::CoverageSpans;
1313

1414
use crate::MirPass;
1515

16-
use rustc_data_structures::sync::Lrc;
1716
use rustc_middle::hir;
1817
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1918
use rustc_middle::mir::coverage::*;
@@ -306,23 +305,27 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
306305
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
307306
// to HIR for it.
308307

309-
let source_map = tcx.sess.source_map();
310-
311308
let hir_node = tcx.hir_node_by_def_id(def_id);
312309
let (_, fn_body_id) =
313310
hir::map::associated_body(hir_node).expect("HIR node is a function with body");
314311
let hir_body = tcx.hir().body(fn_body_id);
315312

316313
let body_span = get_body_span(tcx, hir_body, def_id);
317314

318-
let source_file = source_map.lookup_source_file(body_span.lo());
319-
let fn_sig_span = match hir_node.fn_sig().filter(|fn_sig| {
320-
fn_sig.span.eq_ctxt(body_span)
321-
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
322-
}) {
323-
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
324-
None => body_span.shrink_to_lo(),
325-
};
315+
// The actual signature span is only used if it has the same context and
316+
// filename as the body.
317+
let maybe_fn_sig_span = hir_node.fn_sig().map(|fn_sig| fn_sig.span);
318+
let fn_sig_span = maybe_fn_sig_span
319+
.filter(|&fn_sig_span| {
320+
let source_map = tcx.sess.source_map();
321+
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
322+
323+
fn_sig_span.eq_ctxt(body_span) && file_idx(fn_sig_span) == file_idx(body_span)
324+
})
325+
// If so, extend it to the start of the body span.
326+
.map(|fn_sig_span| fn_sig_span.with_hi(body_span.lo()))
327+
// Otherwise, create a dummy signature span at the start of the body.
328+
.unwrap_or_else(|| body_span.shrink_to_lo());
326329

327330
let function_source_hash = hash_mir_source(tcx, hir_body);
328331

0 commit comments

Comments
 (0)