coverage: Defer the filtering of hole spans

This commit is contained in:
Zalathar 2025-03-04 13:53:24 +11:00
parent 83b56eb059
commit 7fdac5eef0
2 changed files with 15 additions and 15 deletions

View file

@ -273,8 +273,9 @@ struct ExtractedHirInfo {
/// Must have the same context and filename as the body span.
fn_sig_span_extended: Option<Span>,
body_span: Span,
/// "Holes" are regions within the body span that should not be included in
/// coverage spans for this function (e.g. closures and nested items).
/// "Holes" are regions within the function body (or its expansions) that
/// should not be included in coverage spans for this function
/// (e.g. closures and nested items).
hole_spans: Vec<Span>,
}
@ -323,7 +324,7 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
let function_source_hash = hash_mir_source(tcx, hir_body);
let hole_spans = extract_hole_spans_from_hir(tcx, body_span, hir_body);
let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
ExtractedHirInfo {
function_source_hash,
@ -340,14 +341,9 @@ fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) ->
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
}
fn extract_hole_spans_from_hir<'tcx>(
tcx: TyCtxt<'tcx>,
body_span: Span, // Usually `hir_body.value.span`, but not always
hir_body: &hir::Body<'tcx>,
) -> Vec<Span> {
fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
struct HolesVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
body_span: Span,
hole_spans: Vec<Span>,
}
@ -387,14 +383,11 @@ fn extract_hole_spans_from_hir<'tcx>(
}
impl HolesVisitor<'_> {
fn visit_hole_span(&mut self, hole_span: Span) {
// Discard any holes that aren't directly visible within the body span.
if self.body_span.contains(hole_span) && self.body_span.eq_ctxt(hole_span) {
self.hole_spans.push(hole_span);
}
self.hole_spans.push(hole_span);
}
}
let mut visitor = HolesVisitor { tcx, body_span, hole_spans: vec![] };
let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
visitor.visit_body(hir_body);
visitor.hole_spans

View file

@ -69,7 +69,14 @@ pub(super) fn extract_refined_covspans(
covspans.dedup_by(|b, a| a.span.source_equal(b.span));
// Sort the holes, and merge overlapping/adjacent holes.
let mut holes = hir_info.hole_spans.iter().map(|&span| Hole { span }).collect::<Vec<_>>();
let mut holes = hir_info
.hole_spans
.iter()
.copied()
// Discard any holes that aren't directly visible within the body span.
.filter(|&hole_span| body_span.contains(hole_span) && body_span.eq_ctxt(hole_span))
.map(|span| Hole { span })
.collect::<Vec<_>>();
holes.sort_by(|a, b| compare_spans(a.span, b.span));
holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));