@@ -1211,20 +1211,12 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
1211
1211
}
1212
1212
1213
1213
pub ( super ) fn hir_module_items ( tcx : TyCtxt < ' _ > , module_id : LocalDefId ) -> ModuleItems {
1214
- let mut collector = ModuleCollector {
1215
- tcx,
1216
- submodules : Vec :: default ( ) ,
1217
- items : Vec :: default ( ) ,
1218
- trait_items : Vec :: default ( ) ,
1219
- impl_items : Vec :: default ( ) ,
1220
- foreign_items : Vec :: default ( ) ,
1221
- body_owners : Vec :: default ( ) ,
1222
- } ;
1214
+ let mut collector = ItemCollector :: new ( tcx, false ) ;
1223
1215
1224
1216
let ( hir_mod, span, hir_id) = tcx. hir ( ) . get_module ( module_id) ;
1225
1217
collector. visit_mod ( hir_mod, span, hir_id) ;
1226
1218
1227
- let ModuleCollector {
1219
+ let ItemCollector {
1228
1220
submodules,
1229
1221
items,
1230
1222
trait_items,
@@ -1241,90 +1233,14 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
1241
1233
foreign_items : foreign_items. into_boxed_slice ( ) ,
1242
1234
body_owners : body_owners. into_boxed_slice ( ) ,
1243
1235
} ;
1244
-
1245
- struct ModuleCollector < ' tcx > {
1246
- tcx : TyCtxt < ' tcx > ,
1247
- submodules : Vec < LocalDefId > ,
1248
- items : Vec < ItemId > ,
1249
- trait_items : Vec < TraitItemId > ,
1250
- impl_items : Vec < ImplItemId > ,
1251
- foreign_items : Vec < ForeignItemId > ,
1252
- body_owners : Vec < LocalDefId > ,
1253
- }
1254
-
1255
- impl < ' hir > Visitor < ' hir > for ModuleCollector < ' hir > {
1256
- type NestedFilter = nested_filter:: All ;
1257
-
1258
- fn nested_visit_map ( & mut self ) -> Self :: Map {
1259
- self . tcx . hir ( )
1260
- }
1261
-
1262
- fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1263
- if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1264
- self . body_owners . push ( item. def_id ) ;
1265
- }
1266
-
1267
- self . items . push ( item. item_id ( ) ) ;
1268
-
1269
- if let ItemKind :: Mod ( ..) = item. kind {
1270
- // If this declares another module, do not recurse inside it.
1271
- self . submodules . push ( item. def_id ) ;
1272
- } else {
1273
- intravisit:: walk_item ( self , item)
1274
- }
1275
- }
1276
-
1277
- fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1278
- if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1279
- self . body_owners . push ( item. def_id ) ;
1280
- }
1281
-
1282
- self . trait_items . push ( item. trait_item_id ( ) ) ;
1283
- intravisit:: walk_trait_item ( self , item)
1284
- }
1285
-
1286
- fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1287
- if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1288
- self . body_owners . push ( item. def_id ) ;
1289
- }
1290
-
1291
- self . impl_items . push ( item. impl_item_id ( ) ) ;
1292
- intravisit:: walk_impl_item ( self , item)
1293
- }
1294
-
1295
- fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1296
- self . foreign_items . push ( item. foreign_item_id ( ) ) ;
1297
- intravisit:: walk_foreign_item ( self , item)
1298
- }
1299
-
1300
- fn visit_expr ( & mut self , ex : & ' hir Expr < ' hir > ) {
1301
- if matches ! ( ex. kind, ExprKind :: Closure { .. } ) {
1302
- self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( ex. hir_id ) ) ;
1303
- }
1304
- intravisit:: walk_expr ( self , ex)
1305
- }
1306
-
1307
- fn visit_anon_const ( & mut self , c : & ' hir AnonConst ) {
1308
- self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( c. hir_id ) ) ;
1309
- intravisit:: walk_anon_const ( self , c)
1310
- }
1311
- }
1312
1236
}
1313
1237
1314
1238
pub ( crate ) fn hir_crate_items ( tcx : TyCtxt < ' _ > , _: ( ) ) -> ModuleItems {
1315
- let mut collector = CrateCollector {
1316
- tcx,
1317
- submodules : Vec :: default ( ) ,
1318
- items : Vec :: default ( ) ,
1319
- trait_items : Vec :: default ( ) ,
1320
- impl_items : Vec :: default ( ) ,
1321
- foreign_items : Vec :: default ( ) ,
1322
- body_owners : Vec :: default ( ) ,
1323
- } ;
1239
+ let mut collector = ItemCollector :: new ( tcx, true ) ;
1324
1240
1325
1241
tcx. hir ( ) . walk_toplevel_module ( & mut collector) ;
1326
1242
1327
- let CrateCollector {
1243
+ let ItemCollector {
1328
1244
submodules,
1329
1245
items,
1330
1246
trait_items,
@@ -1342,71 +1258,95 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
1342
1258
foreign_items : foreign_items. into_boxed_slice ( ) ,
1343
1259
body_owners : body_owners. into_boxed_slice ( ) ,
1344
1260
} ;
1261
+ }
1345
1262
1346
- struct CrateCollector < ' tcx > {
1347
- tcx : TyCtxt < ' tcx > ,
1348
- submodules : Vec < LocalDefId > ,
1349
- items : Vec < ItemId > ,
1350
- trait_items : Vec < TraitItemId > ,
1351
- impl_items : Vec < ImplItemId > ,
1352
- foreign_items : Vec < ForeignItemId > ,
1353
- body_owners : Vec < LocalDefId > ,
1263
+ struct ItemCollector < ' tcx > {
1264
+ // When true, it collects all items in the create,
1265
+ // otherwise it collects items in some module.
1266
+ crate_collector : bool ,
1267
+ tcx : TyCtxt < ' tcx > ,
1268
+ submodules : Vec < LocalDefId > ,
1269
+ items : Vec < ItemId > ,
1270
+ trait_items : Vec < TraitItemId > ,
1271
+ impl_items : Vec < ImplItemId > ,
1272
+ foreign_items : Vec < ForeignItemId > ,
1273
+ body_owners : Vec < LocalDefId > ,
1274
+ }
1275
+
1276
+ impl < ' tcx > ItemCollector < ' tcx > {
1277
+ fn new ( tcx : TyCtxt < ' tcx > , crate_collector : bool ) -> ItemCollector < ' tcx > {
1278
+ ItemCollector {
1279
+ crate_collector,
1280
+ tcx,
1281
+ submodules : Vec :: default ( ) ,
1282
+ items : Vec :: default ( ) ,
1283
+ trait_items : Vec :: default ( ) ,
1284
+ impl_items : Vec :: default ( ) ,
1285
+ foreign_items : Vec :: default ( ) ,
1286
+ body_owners : Vec :: default ( ) ,
1287
+ }
1354
1288
}
1289
+ }
1290
+
1291
+ impl < ' hir > Visitor < ' hir > for ItemCollector < ' hir > {
1292
+ type NestedFilter = nested_filter:: All ;
1355
1293
1356
- impl < ' hir > Visitor < ' hir > for CrateCollector < ' hir > {
1357
- type NestedFilter = nested_filter:: All ;
1294
+ fn nested_visit_map ( & mut self ) -> Self :: Map {
1295
+ self . tcx . hir ( )
1296
+ }
1358
1297
1359
- fn nested_visit_map ( & mut self ) -> Self :: Map {
1360
- self . tcx . hir ( )
1298
+ fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1299
+ if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1300
+ self . body_owners . push ( item. def_id ) ;
1361
1301
}
1362
1302
1363
- fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1364
- if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1365
- self . body_owners . push ( item. def_id ) ;
1366
- }
1303
+ self . items . push ( item. item_id ( ) ) ;
1367
1304
1368
- self . items . push ( item. item_id ( ) ) ;
1305
+ if !self . crate_collector && let ItemKind :: Mod ( ..) = item. kind {
1306
+ // If this declares another module, do not recurse inside it.
1307
+ self . submodules . push ( item. def_id ) ;
1308
+ } else {
1369
1309
intravisit:: walk_item ( self , item)
1370
1310
}
1311
+ }
1371
1312
1372
- fn visit_mod ( & mut self , m : & ' hir Mod < ' hir > , _s : Span , n : HirId ) {
1373
- self . submodules . push ( n. owner ) ;
1374
- intravisit:: walk_mod ( self , m, n) ;
1375
- }
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
+ }
1376
1317
1377
- fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1378
- self . foreign_items . push ( item. foreign_item_id ( ) ) ;
1379
- intravisit:: walk_foreign_item ( self , item)
1380
- }
1318
+ fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1319
+ self . foreign_items . push ( item. foreign_item_id ( ) ) ;
1320
+ intravisit:: walk_foreign_item ( self , item)
1321
+ }
1381
1322
1382
- fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1383
- if associated_body ( Node :: TraitItem ( item ) ) . is_some ( ) {
1384
- self . body_owners . push ( item . def_id ) ;
1385
- }
1323
+ fn visit_anon_const ( & mut self , c : & ' hir AnonConst ) {
1324
+ self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( c . hir_id ) ) ;
1325
+ intravisit :: walk_anon_const ( self , c )
1326
+ }
1386
1327
1387
- self . trait_items . push ( item. trait_item_id ( ) ) ;
1388
- intravisit:: walk_trait_item ( self , item)
1328
+ fn visit_expr ( & mut self , ex : & ' hir Expr < ' hir > ) {
1329
+ if matches ! ( ex. kind, ExprKind :: Closure { .. } ) {
1330
+ self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( ex. hir_id ) ) ;
1389
1331
}
1332
+ intravisit:: walk_expr ( self , ex)
1333
+ }
1390
1334
1391
- fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1392
- if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1393
- self . body_owners . push ( item. def_id ) ;
1394
- }
1395
-
1396
- self . impl_items . push ( item. impl_item_id ( ) ) ;
1397
- intravisit:: walk_impl_item ( self , item)
1335
+ fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1336
+ if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1337
+ self . body_owners . push ( item. def_id ) ;
1398
1338
}
1399
1339
1400
- fn visit_expr ( & mut self , ex : & ' hir Expr < ' hir > ) {
1401
- if matches ! ( ex. kind, ExprKind :: Closure { .. } ) {
1402
- self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( ex. hir_id ) ) ;
1403
- }
1404
- intravisit:: walk_expr ( self , ex)
1405
- }
1340
+ self . trait_items . push ( item. trait_item_id ( ) ) ;
1341
+ intravisit:: walk_trait_item ( self , item)
1342
+ }
1406
1343
1407
- fn visit_anon_const ( & mut self , c : & ' hir AnonConst ) {
1408
- self . body_owners . push ( self . tcx . hir ( ) . local_def_id ( c . hir_id ) ) ;
1409
- intravisit :: walk_anon_const ( self , c )
1344
+ fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1345
+ if associated_body ( Node :: ImplItem ( item ) ) . is_some ( ) {
1346
+ self . body_owners . push ( item . def_id ) ;
1410
1347
}
1348
+
1349
+ self . impl_items . push ( item. impl_item_id ( ) ) ;
1350
+ intravisit:: walk_impl_item ( self , item)
1411
1351
}
1412
1352
}
0 commit comments