Skip to content

Commit 3b610c7

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 7de2156 commit 3b610c7

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

+14-11
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::*;
@@ -317,23 +316,27 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
317316
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
318317
// to HIR for it.
319318

320-
let source_map = tcx.sess.source_map();
321-
322319
let hir_node = tcx.hir_node_by_def_id(def_id);
323320
let (_, fn_body_id) =
324321
hir::map::associated_body(hir_node).expect("HIR node is a function with body");
325322
let hir_body = tcx.hir().body(fn_body_id);
326323

327324
let body_span = get_body_span(tcx, hir_body, def_id);
328325

329-
let source_file = source_map.lookup_source_file(body_span.lo());
330-
let fn_sig_span = match hir_node.fn_sig().filter(|fn_sig| {
331-
fn_sig.span.eq_ctxt(body_span)
332-
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
333-
}) {
334-
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
335-
None => body_span.shrink_to_lo(),
336-
};
326+
// The actual signature span is only used if it has the same context and
327+
// filename as the body.
328+
let maybe_fn_sig_span = hir_node.fn_sig().map(|fn_sig| fn_sig.span);
329+
let fn_sig_span = maybe_fn_sig_span
330+
.filter(|&fn_sig_span| {
331+
let source_map = tcx.sess.source_map();
332+
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
333+
334+
fn_sig_span.eq_ctxt(body_span) && file_idx(fn_sig_span) == file_idx(body_span)
335+
})
336+
// If so, extend it to the start of the body span.
337+
.map(|fn_sig_span| fn_sig_span.with_hi(body_span.lo()))
338+
// Otherwise, create a dummy signature span at the start of the body.
339+
.unwrap_or_else(|| body_span.shrink_to_lo());
337340

338341
let function_source_hash = hash_mir_source(tcx, hir_body);
339342

0 commit comments

Comments
 (0)