@@ -493,18 +493,7 @@ impl<'hir> Map<'hir> {
493493 /// crate. If you would prefer to iterate over the bodies
494494 /// themselves, you can do `self.hir().krate().body_ids.iter()`.
495495 pub fn body_owners ( self ) -> impl Iterator < Item = LocalDefId > + ' hir {
496- self . krate ( )
497- . owners
498- . iter_enumerated ( )
499- . flat_map ( move |( owner, owner_info) | {
500- let bodies = & owner_info. as_owner ( ) ?. nodes . bodies ;
501- Some ( bodies. iter ( ) . map ( move |& ( local_id, _) | {
502- let hir_id = HirId { owner, local_id } ;
503- let body_id = BodyId { hir_id } ;
504- self . body_owner_def_id ( body_id)
505- } ) )
506- } )
507- . flatten ( )
496+ self . tcx . hir_crate_items ( ( ) ) . body_owners . iter ( ) . copied ( )
508497 }
509498
510499 pub fn par_body_owners < F : Fn ( LocalDefId ) + Sync + Send > ( self , f : F ) {
@@ -1239,19 +1228,28 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
12391228 trait_items : Vec :: default ( ) ,
12401229 impl_items : Vec :: default ( ) ,
12411230 foreign_items : Vec :: default ( ) ,
1231+ body_owners : Vec :: default ( ) ,
12421232 } ;
12431233
12441234 let ( hir_mod, span, hir_id) = tcx. hir ( ) . get_module ( module_id) ;
12451235 collector. visit_mod ( hir_mod, span, hir_id) ;
12461236
1247- let ModuleCollector { submodules, items, trait_items, impl_items, foreign_items, .. } =
1248- collector;
1237+ let ModuleCollector {
1238+ submodules,
1239+ items,
1240+ trait_items,
1241+ impl_items,
1242+ foreign_items,
1243+ body_owners,
1244+ ..
1245+ } = collector;
12491246 return ModuleItems {
12501247 submodules : submodules. into_boxed_slice ( ) ,
12511248 items : items. into_boxed_slice ( ) ,
12521249 trait_items : trait_items. into_boxed_slice ( ) ,
12531250 impl_items : impl_items. into_boxed_slice ( ) ,
12541251 foreign_items : foreign_items. into_boxed_slice ( ) ,
1252+ body_owners : body_owners. into_boxed_slice ( ) ,
12551253 } ;
12561254
12571255 struct ModuleCollector < ' tcx > {
@@ -1261,6 +1259,7 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
12611259 trait_items : Vec < TraitItemId > ,
12621260 impl_items : Vec < ImplItemId > ,
12631261 foreign_items : Vec < ForeignItemId > ,
1262+ body_owners : Vec < LocalDefId > ,
12641263 }
12651264
12661265 impl < ' hir > Visitor < ' hir > for ModuleCollector < ' hir > {
@@ -1271,7 +1270,16 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
12711270 }
12721271
12731272 fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1273+ if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1274+ self . body_owners . push ( item. def_id ) ;
1275+ }
1276+
12741277 self . items . push ( item. item_id ( ) ) ;
1278+
1279+ if self . tcx . hir ( ) . is_body_owner ( item. def_id ) {
1280+ self . body_owners . push ( item. def_id ) ;
1281+ }
1282+
12751283 if let ItemKind :: Mod ( ..) = item. kind {
12761284 // If this declares another module, do not recurse inside it.
12771285 self . submodules . push ( item. def_id ) ;
@@ -1281,19 +1289,47 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
12811289 }
12821290
12831291 fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1292+ if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1293+ self . body_owners . push ( item. def_id ) ;
1294+ }
1295+
12841296 self . trait_items . push ( item. trait_item_id ( ) ) ;
12851297 intravisit:: walk_trait_item ( self , item)
12861298 }
12871299
12881300 fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1301+ if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1302+ self . body_owners . push ( item. def_id ) ;
1303+ }
1304+
12891305 self . impl_items . push ( item. impl_item_id ( ) ) ;
12901306 intravisit:: walk_impl_item ( self , item)
12911307 }
12921308
12931309 fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1310+ if associated_body ( Node :: ForeignItem ( item) ) . is_some ( ) {
1311+ self . body_owners . push ( item. def_id ) ;
1312+ }
1313+
12941314 self . foreign_items . push ( item. foreign_item_id ( ) ) ;
12951315 intravisit:: walk_foreign_item ( self , item)
12961316 }
1317+
1318+ fn visit_expr ( & mut self , ex : & ' hir Expr < ' hir > ) {
1319+ if matches ! ( ex. kind, ExprKind :: Closure { .. } )
1320+ && associated_body ( Node :: Expr ( ex) ) . is_some ( )
1321+ {
1322+ self . body_owners . push ( ex. hir_id . owner ) ;
1323+ }
1324+ intravisit:: walk_expr ( self , ex)
1325+ }
1326+
1327+ fn visit_anon_const ( & mut self , c : & ' hir AnonConst ) {
1328+ if associated_body ( Node :: AnonConst ( c) ) . is_some ( ) {
1329+ self . body_owners . push ( c. hir_id . owner ) ;
1330+ }
1331+ intravisit:: walk_anon_const ( self , c)
1332+ }
12971333 }
12981334}
12991335
@@ -1305,19 +1341,28 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
13051341 trait_items : Vec :: default ( ) ,
13061342 impl_items : Vec :: default ( ) ,
13071343 foreign_items : Vec :: default ( ) ,
1344+ body_owners : Vec :: default ( ) ,
13081345 } ;
13091346
13101347 tcx. hir ( ) . walk_toplevel_module ( & mut collector) ;
13111348
1312- let CrateCollector { submodules, items, trait_items, impl_items, foreign_items, .. } =
1313- collector;
1349+ let CrateCollector {
1350+ submodules,
1351+ items,
1352+ trait_items,
1353+ impl_items,
1354+ foreign_items,
1355+ body_owners,
1356+ ..
1357+ } = collector;
13141358
13151359 return ModuleItems {
13161360 submodules : submodules. into_boxed_slice ( ) ,
13171361 items : items. into_boxed_slice ( ) ,
13181362 trait_items : trait_items. into_boxed_slice ( ) ,
13191363 impl_items : impl_items. into_boxed_slice ( ) ,
13201364 foreign_items : foreign_items. into_boxed_slice ( ) ,
1365+ body_owners : body_owners. into_boxed_slice ( ) ,
13211366 } ;
13221367
13231368 struct CrateCollector < ' tcx > {
@@ -1327,6 +1372,7 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
13271372 trait_items : Vec < TraitItemId > ,
13281373 impl_items : Vec < ImplItemId > ,
13291374 foreign_items : Vec < ForeignItemId > ,
1375+ body_owners : Vec < LocalDefId > ,
13301376 }
13311377
13321378 impl < ' hir > Visitor < ' hir > for CrateCollector < ' hir > {
@@ -1337,6 +1383,10 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
13371383 }
13381384
13391385 fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1386+ if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1387+ self . body_owners . push ( item. def_id ) ;
1388+ }
1389+
13401390 self . items . push ( item. item_id ( ) ) ;
13411391 intravisit:: walk_item ( self , item)
13421392 }
@@ -1347,18 +1397,46 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
13471397 }
13481398
13491399 fn visit_foreign_item ( & mut self , item : & ' hir ForeignItem < ' hir > ) {
1400+ if associated_body ( Node :: ForeignItem ( item) ) . is_some ( ) {
1401+ self . body_owners . push ( item. def_id ) ;
1402+ }
1403+
13501404 self . foreign_items . push ( item. foreign_item_id ( ) ) ;
13511405 intravisit:: walk_foreign_item ( self , item)
13521406 }
13531407
13541408 fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1409+ if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1410+ self . body_owners . push ( item. def_id ) ;
1411+ }
1412+
13551413 self . trait_items . push ( item. trait_item_id ( ) ) ;
13561414 intravisit:: walk_trait_item ( self , item)
13571415 }
13581416
13591417 fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1418+ if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1419+ self . body_owners . push ( item. def_id ) ;
1420+ }
1421+
13601422 self . impl_items . push ( item. impl_item_id ( ) ) ;
13611423 intravisit:: walk_impl_item ( self , item)
13621424 }
1425+
1426+ fn visit_expr ( & mut self , ex : & ' hir Expr < ' hir > ) {
1427+ if matches ! ( ex. kind, ExprKind :: Closure { .. } )
1428+ && associated_body ( Node :: Expr ( ex) ) . is_some ( )
1429+ {
1430+ self . body_owners . push ( ex. hir_id . owner ) ;
1431+ }
1432+ intravisit:: walk_expr ( self , ex)
1433+ }
1434+
1435+ fn visit_anon_const ( & mut self , c : & ' hir AnonConst ) {
1436+ if associated_body ( Node :: AnonConst ( c) ) . is_some ( ) {
1437+ self . body_owners . push ( c. hir_id . owner ) ;
1438+ }
1439+ intravisit:: walk_anon_const ( self , c)
1440+ }
13631441 }
13641442}
0 commit comments