@@ -1238,6 +1238,10 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
1238
1238
pub ( crate ) fn hir_crate_items ( tcx : TyCtxt < ' _ > , _: ( ) ) -> ModuleItems {
1239
1239
let mut collector = ItemCollector :: new ( tcx, true ) ;
1240
1240
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 ) ;
1241
1245
tcx. hir ( ) . walk_toplevel_module ( & mut collector) ;
1242
1246
1243
1247
let ItemCollector {
@@ -1302,19 +1306,18 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1302
1306
1303
1307
self . items . push ( item. item_id ( ) ) ;
1304
1308
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 {
1307
1311
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
+ }
1308
1316
} else {
1309
1317
intravisit:: walk_item ( self , item)
1310
1318
}
1311
1319
}
1312
1320
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
-
1318
1321
fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1319
1322
self . foreign_items . push ( item. foreign_item_id ( ) ) ;
1320
1323
intravisit:: walk_foreign_item ( self , item)
0 commit comments