@@ -1238,6 +1238,10 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
12381238pub ( crate ) fn hir_crate_items ( tcx : TyCtxt < ' _ > , _: ( ) ) -> ModuleItems {
12391239 let mut collector = ItemCollector :: new ( tcx, true ) ;
12401240
1241+ // A "crate collector" and "module collector" start at a
1242+ // module item (the former starts at the crate root) but only
1243+ // the former needs to collect it. ItemCollector does not do this for us.
1244+ collector. submodules . push ( CRATE_DEF_ID ) ;
12411245 tcx. hir ( ) . walk_toplevel_module ( & mut collector) ;
12421246
12431247 let ItemCollector {
@@ -1302,19 +1306,18 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13021306
13031307 self . items . push ( item. item_id ( ) ) ;
13041308
1305- if ! self . crate_collector && let ItemKind :: Mod ( .. ) = item . kind {
1306- // If this declares another module, do not recurse inside it.
1309+ // Items that are modules are handled here instead of in visit_mod.
1310+ if let ItemKind :: Mod ( module) = & item . kind {
13071311 self . submodules . push ( item. def_id ) ;
1312+ // A module collector does not recurse inside nested modules.
1313+ if self . crate_collector {
1314+ intravisit:: walk_mod ( self , module, item. hir_id ( ) ) ;
1315+ }
13081316 } else {
13091317 intravisit:: walk_item ( self , item)
13101318 }
13111319 }
13121320
1313- fn visit_mod ( & mut self , m : & ' hir Mod < ' hir > , _s : Span , n : HirId ) {
1314- self . submodules . push ( n. owner ) ;
1315- intravisit:: walk_mod ( self , m, n) ;
1316- }
1317-
13181321 fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
13191322 self . foreign_items . push ( item. foreign_item_id ( ) ) ;
13201323 intravisit:: walk_foreign_item ( self , item)
0 commit comments