Skip to content

Commit 7fdac5e

Browse files
committed
coverage: Defer the filtering of hole spans
1 parent 83b56eb commit 7fdac5e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ struct ExtractedHirInfo {
273273
/// Must have the same context and filename as the body span.
274274
fn_sig_span_extended: Option<Span>,
275275
body_span: Span,
276-
/// "Holes" are regions within the body span that should not be included in
277-
/// coverage spans for this function (e.g. closures and nested items).
276+
/// "Holes" are regions within the function body (or its expansions) that
277+
/// should not be included in coverage spans for this function
278+
/// (e.g. closures and nested items).
278279
hole_spans: Vec<Span>,
279280
}
280281

@@ -323,7 +324,7 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
323324

324325
let function_source_hash = hash_mir_source(tcx, hir_body);
325326

326-
let hole_spans = extract_hole_spans_from_hir(tcx, body_span, hir_body);
327+
let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
327328

328329
ExtractedHirInfo {
329330
function_source_hash,
@@ -340,14 +341,9 @@ fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) ->
340341
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
341342
}
342343

343-
fn extract_hole_spans_from_hir<'tcx>(
344-
tcx: TyCtxt<'tcx>,
345-
body_span: Span, // Usually `hir_body.value.span`, but not always
346-
hir_body: &hir::Body<'tcx>,
347-
) -> Vec<Span> {
344+
fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
348345
struct HolesVisitor<'tcx> {
349346
tcx: TyCtxt<'tcx>,
350-
body_span: Span,
351347
hole_spans: Vec<Span>,
352348
}
353349

@@ -387,14 +383,11 @@ fn extract_hole_spans_from_hir<'tcx>(
387383
}
388384
impl HolesVisitor<'_> {
389385
fn visit_hole_span(&mut self, hole_span: Span) {
390-
// Discard any holes that aren't directly visible within the body span.
391-
if self.body_span.contains(hole_span) && self.body_span.eq_ctxt(hole_span) {
392-
self.hole_spans.push(hole_span);
393-
}
386+
self.hole_spans.push(hole_span);
394387
}
395388
}
396389

397-
let mut visitor = HolesVisitor { tcx, body_span, hole_spans: vec![] };
390+
let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
398391

399392
visitor.visit_body(hir_body);
400393
visitor.hole_spans

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ pub(super) fn extract_refined_covspans(
6969
covspans.dedup_by(|b, a| a.span.source_equal(b.span));
7070

7171
// Sort the holes, and merge overlapping/adjacent holes.
72-
let mut holes = hir_info.hole_spans.iter().map(|&span| Hole { span }).collect::<Vec<_>>();
72+
let mut holes = hir_info
73+
.hole_spans
74+
.iter()
75+
.copied()
76+
// Discard any holes that aren't directly visible within the body span.
77+
.filter(|&hole_span| body_span.contains(hole_span) && body_span.eq_ctxt(hole_span))
78+
.map(|span| Hole { span })
79+
.collect::<Vec<_>>();
7380
holes.sort_by(|a, b| compare_spans(a.span, b.span));
7481
holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
7582

0 commit comments

Comments
 (0)