@@ -21,44 +21,6 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
21
21
use rustc_span:: { ErrorGuaranteed , Span } ;
22
22
use rustc_target:: spec:: abi:: Abi ;
23
23
24
- #[ inline]
25
- pub fn associated_body ( node : Node < ' _ > ) -> Option < ( LocalDefId , BodyId ) > {
26
- match node {
27
- Node :: Item ( Item {
28
- owner_id,
29
- kind : ItemKind :: Const ( _, _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn ( .., body) ,
30
- ..
31
- } )
32
- | Node :: TraitItem ( TraitItem {
33
- owner_id,
34
- kind :
35
- TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) ,
36
- ..
37
- } )
38
- | Node :: ImplItem ( ImplItem {
39
- owner_id,
40
- kind : ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) ,
41
- ..
42
- } ) => Some ( ( owner_id. def_id , * body) ) ,
43
-
44
- Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } ) => {
45
- Some ( ( * def_id, * body) )
46
- }
47
-
48
- Node :: AnonConst ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
49
- Node :: ConstBlock ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
50
-
51
- _ => None ,
52
- }
53
- }
54
-
55
- fn is_body_owner ( node : Node < ' _ > , hir_id : HirId ) -> bool {
56
- match associated_body ( node) {
57
- Some ( ( _, b) ) => b. hir_id == hir_id,
58
- None => false ,
59
- }
60
- }
61
-
62
24
// FIXME: the structure was necessary in the past but now it
63
25
// only serves as "namespace" for HIR-related methods, and can be
64
26
// removed if all the methods are reasonably renamed and moved to tcx
@@ -283,7 +245,7 @@ impl<'hir> Map<'hir> {
283
245
#[ track_caller]
284
246
pub fn enclosing_body_owner ( self , hir_id : HirId ) -> LocalDefId {
285
247
for ( _, node) in self . parent_iter ( hir_id) {
286
- if let Some ( ( def_id, _) ) = associated_body ( node ) {
248
+ if let Some ( ( def_id, _) ) = node . associated_body ( ) {
287
249
return def_id;
288
250
}
289
251
}
@@ -296,20 +258,19 @@ impl<'hir> Map<'hir> {
296
258
/// item (possibly associated), a closure, or a `hir::AnonConst`.
297
259
pub fn body_owner ( self , BodyId { hir_id } : BodyId ) -> HirId {
298
260
let parent = self . tcx . parent_hir_id ( hir_id) ;
299
- assert ! ( is_body_owner ( self . tcx. hir_node( parent) , hir_id) , "{hir_id:?}" ) ;
261
+ assert_eq ! ( self . tcx. hir_node( parent) . body_id ( ) . unwrap ( ) . hir_id , hir_id, "{hir_id:?}" ) ;
300
262
parent
301
263
}
302
264
303
265
pub fn body_owner_def_id ( self , BodyId { hir_id } : BodyId ) -> LocalDefId {
304
- associated_body ( self . tcx . parent_hir_node ( hir_id) ) . unwrap ( ) . 0
266
+ self . tcx . parent_hir_node ( hir_id) . associated_body ( ) . unwrap ( ) . 0
305
267
}
306
268
307
269
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
308
270
/// if the node is a body owner, otherwise returns `None`.
309
271
pub fn maybe_body_owned_by ( self , id : LocalDefId ) -> Option < BodyId > {
310
272
let node = self . tcx . opt_hir_node_by_def_id ( id) ?;
311
- let ( _, body_id) = associated_body ( node) ?;
312
- Some ( body_id)
273
+ node. body_id ( )
313
274
}
314
275
315
276
/// Given a body owner's id, returns the `BodyId` associated with it.
@@ -1322,7 +1283,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1322
1283
}
1323
1284
1324
1285
fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1325
- if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1286
+ if Node :: Item ( item) . associated_body ( ) . is_some ( ) {
1326
1287
self . body_owners . push ( item. owner_id . def_id ) ;
1327
1288
}
1328
1289
@@ -1363,7 +1324,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1363
1324
}
1364
1325
1365
1326
fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1366
- if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1327
+ if Node :: TraitItem ( item) . associated_body ( ) . is_some ( ) {
1367
1328
self . body_owners . push ( item. owner_id . def_id ) ;
1368
1329
}
1369
1330
@@ -1372,7 +1333,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1372
1333
}
1373
1334
1374
1335
fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1375
- if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1336
+ if Node :: ImplItem ( item) . associated_body ( ) . is_some ( ) {
1376
1337
self . body_owners . push ( item. owner_id . def_id ) ;
1377
1338
}
1378
1339
0 commit comments