Skip to content

Commit 51f704f

Browse files
committed
coverage: Get hole spans from nested items without fully visiting them
It turns out that this visitor doesn't actually need `nested_filter::All` to handle nested items; it just needs to override `visit_nested_item` and look up the item's span.
1 parent d9b91de commit 51f704f

File tree

1 file changed

+9
-8
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+9
-8
lines changed

Diff for: compiler/rustc_mir_transform/src/coverage/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,20 @@ fn extract_hole_spans_from_hir<'tcx>(
352352
}
353353

354354
impl<'hir, F: FnMut(Span)> Visitor<'hir> for HolesVisitor<'hir, F> {
355-
/// - We need `NestedFilter::INTRA = true` so that `visit_item` will be called.
356-
/// - Bodies of nested items don't actually get visited, because of the
357-
/// `visit_item` override.
358-
/// - For nested bodies that are not part of an item, we do want to visit any
359-
/// items contained within them.
360-
type NestedFilter = nested_filter::All;
355+
/// We have special handling for nested items, but we still want to
356+
/// traverse into nested bodies of things that are not considered items,
357+
/// such as "anon consts" (e.g. array lengths).
358+
type NestedFilter = nested_filter::OnlyBodies;
361359

362360
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
363361
self.tcx
364362
}
365363

366-
fn visit_item(&mut self, item: &'hir hir::Item<'hir>) {
367-
(self.visit_hole_span)(item.span);
364+
/// We override `visit_nested_item` instead of `visit_item` because we
365+
/// only need the item's span, not the item itself.
366+
fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
367+
let span = self.tcx.def_span(id.owner_id.def_id);
368+
(self.visit_hole_span)(span);
368369
// Having visited this item, we don't care about its children,
369370
// so don't call `walk_item`.
370371
}

0 commit comments

Comments
 (0)