diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 3240360a5398..1ce7668e7b76 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1211,20 +1211,12 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String { } pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems { - let mut collector = ModuleCollector { - tcx, - submodules: Vec::default(), - items: Vec::default(), - trait_items: Vec::default(), - impl_items: Vec::default(), - foreign_items: Vec::default(), - body_owners: Vec::default(), - }; + let mut collector = ItemCollector::new(tcx, false); let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id); collector.visit_mod(hir_mod, span, hir_id); - let ModuleCollector { + let ItemCollector { submodules, items, trait_items, @@ -1241,90 +1233,14 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module foreign_items: foreign_items.into_boxed_slice(), body_owners: body_owners.into_boxed_slice(), }; - - struct ModuleCollector<'tcx> { - tcx: TyCtxt<'tcx>, - submodules: Vec, - items: Vec, - trait_items: Vec, - impl_items: Vec, - foreign_items: Vec, - body_owners: Vec, - } - - impl<'hir> Visitor<'hir> for ModuleCollector<'hir> { - type NestedFilter = nested_filter::All; - - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() - } - - fn visit_item(&mut self, item: &'hir Item<'hir>) { - if associated_body(Node::Item(item)).is_some() { - self.body_owners.push(item.def_id); - } - - self.items.push(item.item_id()); - - if let ItemKind::Mod(..) = item.kind { - // If this declares another module, do not recurse inside it. - self.submodules.push(item.def_id); - } else { - intravisit::walk_item(self, item) - } - } - - fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) { - if associated_body(Node::TraitItem(item)).is_some() { - self.body_owners.push(item.def_id); - } - - self.trait_items.push(item.trait_item_id()); - intravisit::walk_trait_item(self, item) - } - - fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) { - if associated_body(Node::ImplItem(item)).is_some() { - self.body_owners.push(item.def_id); - } - - self.impl_items.push(item.impl_item_id()); - intravisit::walk_impl_item(self, item) - } - - fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) { - self.foreign_items.push(item.foreign_item_id()); - intravisit::walk_foreign_item(self, item) - } - - fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { - if matches!(ex.kind, ExprKind::Closure { .. }) { - self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id)); - } - intravisit::walk_expr(self, ex) - } - - fn visit_anon_const(&mut self, c: &'hir AnonConst) { - self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id)); - intravisit::walk_anon_const(self, c) - } - } } pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems { - let mut collector = CrateCollector { - tcx, - submodules: Vec::default(), - items: Vec::default(), - trait_items: Vec::default(), - impl_items: Vec::default(), - foreign_items: Vec::default(), - body_owners: Vec::default(), - }; + let mut collector = ItemCollector::new(tcx, true); tcx.hir().walk_toplevel_module(&mut collector); - let CrateCollector { + let ItemCollector { submodules, items, trait_items, @@ -1342,71 +1258,95 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems { foreign_items: foreign_items.into_boxed_slice(), body_owners: body_owners.into_boxed_slice(), }; +} - struct CrateCollector<'tcx> { - tcx: TyCtxt<'tcx>, - submodules: Vec, - items: Vec, - trait_items: Vec, - impl_items: Vec, - foreign_items: Vec, - body_owners: Vec, +struct ItemCollector<'tcx> { + // When true, it collects all items in the create, + // otherwise it collects items in some module. + crate_collector: bool, + tcx: TyCtxt<'tcx>, + submodules: Vec, + items: Vec, + trait_items: Vec, + impl_items: Vec, + foreign_items: Vec, + body_owners: Vec, +} + +impl<'tcx> ItemCollector<'tcx> { + fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> { + ItemCollector { + crate_collector, + tcx, + submodules: Vec::default(), + items: Vec::default(), + trait_items: Vec::default(), + impl_items: Vec::default(), + foreign_items: Vec::default(), + body_owners: Vec::default(), + } } +} + +impl<'hir> Visitor<'hir> for ItemCollector<'hir> { + type NestedFilter = nested_filter::All; - impl<'hir> Visitor<'hir> for CrateCollector<'hir> { - type NestedFilter = nested_filter::All; + fn nested_visit_map(&mut self) -> Self::Map { + self.tcx.hir() + } - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn visit_item(&mut self, item: &'hir Item<'hir>) { + if associated_body(Node::Item(item)).is_some() { + self.body_owners.push(item.def_id); } - fn visit_item(&mut self, item: &'hir Item<'hir>) { - if associated_body(Node::Item(item)).is_some() { - self.body_owners.push(item.def_id); - } + self.items.push(item.item_id()); - self.items.push(item.item_id()); + if !self.crate_collector && let ItemKind::Mod(..) = item.kind { + // If this declares another module, do not recurse inside it. + self.submodules.push(item.def_id); + } else { intravisit::walk_item(self, item) } + } - fn visit_mod(&mut self, m: &'hir Mod<'hir>, _s: Span, n: HirId) { - self.submodules.push(n.owner); - intravisit::walk_mod(self, m, n); - } + fn visit_mod(&mut self, m: &'hir Mod<'hir>, _s: Span, n: HirId) { + self.submodules.push(n.owner); + intravisit::walk_mod(self, m, n); + } - fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) { - self.foreign_items.push(item.foreign_item_id()); - intravisit::walk_foreign_item(self, item) - } + fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) { + self.foreign_items.push(item.foreign_item_id()); + intravisit::walk_foreign_item(self, item) + } - fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) { - if associated_body(Node::TraitItem(item)).is_some() { - self.body_owners.push(item.def_id); - } + fn visit_anon_const(&mut self, c: &'hir AnonConst) { + self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id)); + intravisit::walk_anon_const(self, c) + } - self.trait_items.push(item.trait_item_id()); - intravisit::walk_trait_item(self, item) + fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { + if matches!(ex.kind, ExprKind::Closure { .. }) { + self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id)); } + intravisit::walk_expr(self, ex) + } - fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) { - if associated_body(Node::ImplItem(item)).is_some() { - self.body_owners.push(item.def_id); - } - - self.impl_items.push(item.impl_item_id()); - intravisit::walk_impl_item(self, item) + fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) { + if associated_body(Node::TraitItem(item)).is_some() { + self.body_owners.push(item.def_id); } - fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { - if matches!(ex.kind, ExprKind::Closure { .. }) { - self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id)); - } - intravisit::walk_expr(self, ex) - } + self.trait_items.push(item.trait_item_id()); + intravisit::walk_trait_item(self, item) + } - fn visit_anon_const(&mut self, c: &'hir AnonConst) { - self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id)); - intravisit::walk_anon_const(self, c) + fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) { + if associated_body(Node::ImplItem(item)).is_some() { + self.body_owners.push(item.def_id); } + + self.impl_items.push(item.impl_item_id()); + intravisit::walk_impl_item(self, item) } }