diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2025-02-19 00:54:47 +1100 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2025-02-19 14:01:46 +1100 |
| commit | 51f704f0ff22965d6f21cc7e6888d55e5141932d (patch) | |
| tree | 60ba785956aaf93878cae3024cdd4dfc9c1b221c | |
| parent | d9b91de00c3716e577aeb02929bee69f8028f3a0 (diff) | |
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.
| -rw-r--r-- | compiler/rustc_mir_transform/src/coverage/mod.rs | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 264995efe8f..774f47a35aa 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -352,19 +352,20 @@ fn extract_hole_spans_from_hir<'tcx>( } impl<'hir, F: FnMut(Span)> Visitor<'hir> for HolesVisitor<'hir, F> { - /// - We need `NestedFilter::INTRA = true` so that `visit_item` will be called. - /// - Bodies of nested items don't actually get visited, because of the - /// `visit_item` override. - /// - For nested bodies that are not part of an item, we do want to visit any - /// items contained within them. - type NestedFilter = nested_filter::All; + /// We have special handling for nested items, but we still want to + /// traverse into nested bodies of things that are not considered items, + /// such as "anon consts" (e.g. array lengths). + type NestedFilter = nested_filter::OnlyBodies; fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { self.tcx } - fn visit_item(&mut self, item: &'hir hir::Item<'hir>) { - (self.visit_hole_span)(item.span); + /// We override `visit_nested_item` instead of `visit_item` because we + /// only need the item's span, not the item itself. + fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result { + let span = self.tcx.def_span(id.owner_id.def_id); + (self.visit_hole_span)(span); // Having visited this item, we don't care about its children, // so don't call `walk_item`. } |
