@@ -20,44 +20,6 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
20
20
use rustc_span:: { ErrorGuaranteed , Span } ;
21
21
use rustc_target:: spec:: abi:: Abi ;
22
22
23
- #[ inline]
24
- pub fn associated_body ( node : Node < ' _ > ) -> Option < ( LocalDefId , BodyId ) > {
25
- match node {
26
- Node :: Item ( Item {
27
- owner_id,
28
- kind : ItemKind :: Const ( _, _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn ( .., body) ,
29
- ..
30
- } )
31
- | Node :: TraitItem ( TraitItem {
32
- owner_id,
33
- kind :
34
- TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) ,
35
- ..
36
- } )
37
- | Node :: ImplItem ( ImplItem {
38
- owner_id,
39
- kind : ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) ,
40
- ..
41
- } ) => Some ( ( owner_id. def_id , * body) ) ,
42
-
43
- Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } ) => {
44
- Some ( ( * def_id, * body) )
45
- }
46
-
47
- Node :: AnonConst ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
48
- Node :: ConstBlock ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
49
-
50
- _ => None ,
51
- }
52
- }
53
-
54
- fn is_body_owner ( node : Node < ' _ > , hir_id : HirId ) -> bool {
55
- match associated_body ( node) {
56
- Some ( ( _, b) ) => b. hir_id == hir_id,
57
- None => false ,
58
- }
59
- }
60
-
61
23
// FIXME: the structure was necessary in the past but now it
62
24
// only serves as "namespace" for HIR-related methods, and can be
63
25
// removed if all the methods are reasonably renamed and moved to tcx
@@ -273,7 +235,7 @@ impl<'hir> Map<'hir> {
273
235
#[ track_caller]
274
236
pub fn enclosing_body_owner ( self , hir_id : HirId ) -> LocalDefId {
275
237
for ( _, node) in self . parent_iter ( hir_id) {
276
- if let Some ( ( def_id, _) ) = associated_body ( node ) {
238
+ if let Some ( ( def_id, _) ) = node . associated_body ( ) {
277
239
return def_id;
278
240
}
279
241
}
@@ -286,20 +248,18 @@ impl<'hir> Map<'hir> {
286
248
/// item (possibly associated), a closure, or a `hir::AnonConst`.
287
249
pub fn body_owner ( self , BodyId { hir_id } : BodyId ) -> HirId {
288
250
let parent = self . tcx . parent_hir_id ( hir_id) ;
289
- assert ! ( is_body_owner ( self . tcx. hir_node( parent) , hir_id) , "{hir_id:?}" ) ;
251
+ assert_eq ! ( self . tcx. hir_node( parent) . body_id ( ) . unwrap ( ) . hir_id , hir_id, "{hir_id:?}" ) ;
290
252
parent
291
253
}
292
254
293
255
pub fn body_owner_def_id ( self , BodyId { hir_id } : BodyId ) -> LocalDefId {
294
- associated_body ( self . tcx . parent_hir_node ( hir_id) ) . unwrap ( ) . 0
256
+ self . tcx . parent_hir_node ( hir_id) . associated_body ( ) . unwrap ( ) . 0
295
257
}
296
258
297
259
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
298
260
/// if the node is a body owner, otherwise returns `None`.
299
261
pub fn maybe_body_owned_by ( self , id : LocalDefId ) -> Option < BodyId > {
300
- let node = self . tcx . hir_node_by_def_id ( id) ;
301
- let ( _, body_id) = associated_body ( node) ?;
302
- Some ( body_id)
262
+ self . tcx . hir_node_by_def_id ( id) . body_id ( )
303
263
}
304
264
305
265
/// Given a body owner's id, returns the `BodyId` associated with it.
@@ -1314,7 +1274,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1314
1274
}
1315
1275
1316
1276
fn visit_item ( & mut self , item : & ' hir Item < ' hir > ) {
1317
- if associated_body ( Node :: Item ( item) ) . is_some ( ) {
1277
+ if Node :: Item ( item) . associated_body ( ) . is_some ( ) {
1318
1278
self . body_owners . push ( item. owner_id . def_id ) ;
1319
1279
}
1320
1280
@@ -1355,7 +1315,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1355
1315
}
1356
1316
1357
1317
fn visit_trait_item ( & mut self , item : & ' hir TraitItem < ' hir > ) {
1358
- if associated_body ( Node :: TraitItem ( item) ) . is_some ( ) {
1318
+ if Node :: TraitItem ( item) . associated_body ( ) . is_some ( ) {
1359
1319
self . body_owners . push ( item. owner_id . def_id ) ;
1360
1320
}
1361
1321
@@ -1364,7 +1324,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1364
1324
}
1365
1325
1366
1326
fn visit_impl_item ( & mut self , item : & ' hir ImplItem < ' hir > ) {
1367
- if associated_body ( Node :: ImplItem ( item) ) . is_some ( ) {
1327
+ if Node :: ImplItem ( item) . associated_body ( ) . is_some ( ) {
1368
1328
self . body_owners . push ( item. owner_id . def_id ) ;
1369
1329
}
1370
1330
0 commit comments