@@ -107,9 +107,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
107
107
fn next ( & mut self ) -> Option < Self :: Item > {
108
108
if self . current_id . local_id . index ( ) != 0 {
109
109
self . current_id . local_id = ItemLocalId :: new ( 0 ) ;
110
- if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
111
- return Some ( ( self . current_id . owner , node) ) ;
112
- }
110
+ let node = self . map . tcx . hir_owner_node ( self . current_id . owner ) ;
111
+ return Some ( ( self . current_id . owner , node) ) ;
113
112
}
114
113
if self . current_id == CRATE_HIR_ID {
115
114
return None ;
@@ -125,22 +124,37 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
125
124
self . current_id = HirId :: make_owner ( parent_id. def_id ) ;
126
125
127
126
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
128
- if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
129
- return Some ( ( self . current_id . owner , node) ) ;
130
- }
127
+ let node = self . map . tcx . hir_owner_node ( self . current_id . owner ) ;
128
+ return Some ( ( self . current_id . owner , node) ) ;
131
129
}
132
130
}
133
131
}
134
132
135
133
impl < ' tcx > TyCtxt < ' tcx > {
136
134
#[ inline]
137
- fn hir_owner ( self , owner : OwnerId ) -> Option < OwnerNode < ' tcx > > {
138
- Some ( self . opt_hir_owner_nodes ( owner. def_id ) ?. node ( ) )
135
+ pub fn hir_owner_nodes ( self , owner_id : OwnerId ) -> & ' tcx OwnerNodes < ' tcx > {
136
+ self . opt_hir_owner_nodes ( owner_id. def_id )
137
+ . unwrap_or_else ( || span_bug ! ( self . def_span( owner_id) , "{owner_id:?} is not an owner" ) )
138
+ }
139
+
140
+ #[ inline]
141
+ fn opt_hir_owner_node ( self , def_id : LocalDefId ) -> Option < OwnerNode < ' tcx > > {
142
+ self . opt_hir_owner_nodes ( def_id) . map ( |nodes| nodes. node ( ) )
143
+ }
144
+
145
+ #[ inline]
146
+ fn expect_hir_owner_node ( self , def_id : LocalDefId ) -> OwnerNode < ' tcx > {
147
+ self . hir_owner_nodes ( OwnerId { def_id } ) . node ( )
148
+ }
149
+
150
+ #[ inline]
151
+ fn hir_owner_node ( self , owner_id : OwnerId ) -> OwnerNode < ' tcx > {
152
+ self . hir_owner_nodes ( owner_id) . node ( )
139
153
}
140
154
141
155
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
142
156
pub fn opt_hir_node ( self , id : HirId ) -> Option < Node < ' tcx > > {
143
- let owner = self . opt_hir_owner_nodes ( id. owner ) ? ;
157
+ let owner = self . hir_owner_nodes ( id. owner ) ;
144
158
let node = owner. nodes [ id. local_id ] . as_ref ( ) ?;
145
159
Some ( node. node )
146
160
}
@@ -174,8 +188,8 @@ impl<'hir> Map<'hir> {
174
188
175
189
#[ inline]
176
190
pub fn root_module ( self ) -> & ' hir Mod < ' hir > {
177
- match self . tcx . hir_owner ( CRATE_OWNER_ID ) {
178
- Some ( OwnerNode :: Crate ( item) ) => item,
191
+ match self . tcx . hir_owner_node ( CRATE_OWNER_ID ) {
192
+ OwnerNode :: Crate ( item) => item,
179
193
_ => bug ! ( ) ,
180
194
}
181
195
}
@@ -213,7 +227,7 @@ impl<'hir> Map<'hir> {
213
227
if id. local_id == ItemLocalId :: from_u32 ( 0 ) {
214
228
Some ( self . tcx . hir_owner_parent ( id. owner ) )
215
229
} else {
216
- let owner = self . tcx . opt_hir_owner_nodes ( id. owner ) ? ;
230
+ let owner = self . tcx . hir_owner_nodes ( id. owner ) ;
217
231
let node = owner. nodes [ id. local_id ] . as_ref ( ) ?;
218
232
let hir_id = HirId { owner : id. owner , local_id : node. parent } ;
219
233
// HIR indexing should have checked that.
@@ -241,32 +255,31 @@ impl<'hir> Map<'hir> {
241
255
}
242
256
243
257
pub fn get_generics ( self , id : LocalDefId ) -> Option < & ' hir Generics < ' hir > > {
244
- let node = self . tcx . hir_owner ( OwnerId { def_id : id } ) ?;
245
- node. generics ( )
258
+ self . tcx . opt_hir_owner_node ( id) ?. generics ( )
246
259
}
247
260
248
261
pub fn owner ( self , id : OwnerId ) -> OwnerNode < ' hir > {
249
- self . tcx . hir_owner ( id) . unwrap_or_else ( || bug ! ( "expected owner for {:?}" , id ) )
262
+ self . tcx . hir_owner_node ( id)
250
263
}
251
264
252
265
pub fn item ( self , id : ItemId ) -> & ' hir Item < ' hir > {
253
- self . tcx . hir_owner ( id. owner_id ) . unwrap ( ) . expect_item ( )
266
+ self . tcx . hir_owner_node ( id. owner_id ) . expect_item ( )
254
267
}
255
268
256
269
pub fn trait_item ( self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > {
257
- self . tcx . hir_owner ( id. owner_id ) . unwrap ( ) . expect_trait_item ( )
270
+ self . tcx . hir_owner_node ( id. owner_id ) . expect_trait_item ( )
258
271
}
259
272
260
273
pub fn impl_item ( self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > {
261
- self . tcx . hir_owner ( id. owner_id ) . unwrap ( ) . expect_impl_item ( )
274
+ self . tcx . hir_owner_node ( id. owner_id ) . expect_impl_item ( )
262
275
}
263
276
264
277
pub fn foreign_item ( self , id : ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
265
- self . tcx . hir_owner ( id. owner_id ) . unwrap ( ) . expect_foreign_item ( )
278
+ self . tcx . hir_owner_node ( id. owner_id ) . expect_foreign_item ( )
266
279
}
267
280
268
281
pub fn body ( self , id : BodyId ) -> & ' hir Body < ' hir > {
269
- self . tcx . opt_hir_owner_nodes ( id. hir_id . owner ) . unwrap ( ) . bodies [ & id. hir_id . local_id ]
282
+ self . tcx . hir_owner_nodes ( id. hir_id . owner ) . bodies [ & id. hir_id . local_id ]
270
283
}
271
284
272
285
#[ track_caller]
@@ -436,9 +449,9 @@ impl<'hir> Map<'hir> {
436
449
437
450
pub fn get_module ( self , module : LocalModDefId ) -> ( & ' hir Mod < ' hir > , Span , HirId ) {
438
451
let hir_id = HirId :: make_owner ( module. to_local_def_id ( ) ) ;
439
- match self . tcx . hir_owner ( hir_id. owner ) {
440
- Some ( OwnerNode :: Item ( & Item { span, kind : ItemKind :: Mod ( m) , .. } ) ) => ( m, span, hir_id) ,
441
- Some ( OwnerNode :: Crate ( item) ) => ( item, item. spans . inner_span , hir_id) ,
452
+ match self . tcx . hir_owner_node ( hir_id. owner ) {
453
+ OwnerNode :: Item ( & Item { span, kind : ItemKind :: Mod ( m) , .. } ) => ( m, span, hir_id) ,
454
+ OwnerNode :: Crate ( item) => ( item, item. spans . inner_span , hir_id) ,
442
455
node => panic ! ( "not a module: {node:?}" ) ,
443
456
}
444
457
}
@@ -726,8 +739,8 @@ impl<'hir> Map<'hir> {
726
739
727
740
pub fn get_foreign_abi ( self , hir_id : HirId ) -> Abi {
728
741
let parent = self . get_parent_item ( hir_id) ;
729
- if let Some ( node ) = self . tcx . hir_owner ( parent )
730
- && let OwnerNode :: Item ( Item { kind : ItemKind :: ForeignMod { abi , .. } , .. } ) = node
742
+ if let OwnerNode :: Item ( Item { kind : ItemKind :: ForeignMod { abi , .. } , .. } ) =
743
+ self . tcx . hir_owner_node ( parent )
731
744
{
732
745
return * abi;
733
746
}
@@ -738,37 +751,32 @@ impl<'hir> Map<'hir> {
738
751
}
739
752
740
753
pub fn expect_owner ( self , def_id : LocalDefId ) -> OwnerNode < ' hir > {
741
- self . tcx
742
- . hir_owner ( OwnerId { def_id } )
743
- . unwrap_or_else ( || bug ! ( "expected owner for {:?}" , def_id) )
754
+ self . tcx . expect_hir_owner_node ( def_id)
744
755
}
745
756
746
757
pub fn expect_item ( self , id : LocalDefId ) -> & ' hir Item < ' hir > {
747
- match self . tcx . hir_owner ( OwnerId { def_id : id } ) {
748
- Some ( OwnerNode :: Item ( item) ) => item,
758
+ match self . tcx . expect_hir_owner_node ( id ) {
759
+ OwnerNode :: Item ( item) => item,
749
760
_ => bug ! ( "expected item, found {}" , self . node_to_string( HirId :: make_owner( id) ) ) ,
750
761
}
751
762
}
752
763
753
764
pub fn expect_impl_item ( self , id : LocalDefId ) -> & ' hir ImplItem < ' hir > {
754
- match self . tcx . hir_owner ( OwnerId { def_id : id } ) {
755
- Some ( OwnerNode :: ImplItem ( item) ) => item,
765
+ match self . tcx . expect_hir_owner_node ( id ) {
766
+ OwnerNode :: ImplItem ( item) => item,
756
767
_ => bug ! ( "expected impl item, found {}" , self . node_to_string( HirId :: make_owner( id) ) ) ,
757
768
}
758
769
}
759
770
760
771
pub fn expect_trait_item ( self , id : LocalDefId ) -> & ' hir TraitItem < ' hir > {
761
- match self . tcx . hir_owner ( OwnerId { def_id : id } ) {
762
- Some ( OwnerNode :: TraitItem ( item) ) => item,
772
+ match self . tcx . expect_hir_owner_node ( id ) {
773
+ OwnerNode :: TraitItem ( item) => item,
763
774
_ => bug ! ( "expected trait item, found {}" , self . node_to_string( HirId :: make_owner( id) ) ) ,
764
775
}
765
776
}
766
777
767
778
pub fn get_fn_output ( self , def_id : LocalDefId ) -> Option < & ' hir FnRetTy < ' hir > > {
768
- match self . tcx . hir_owner ( OwnerId { def_id } ) {
769
- Some ( node) => node. fn_decl ( ) . map ( |fn_decl| & fn_decl. output ) ,
770
- _ => None ,
771
- }
779
+ Some ( & self . tcx . opt_hir_owner_node ( def_id) ?. fn_decl ( ) ?. output )
772
780
}
773
781
774
782
pub fn expect_variant ( self , id : HirId ) -> & ' hir Variant < ' hir > {
@@ -779,8 +787,8 @@ impl<'hir> Map<'hir> {
779
787
}
780
788
781
789
pub fn expect_foreign_item ( self , id : OwnerId ) -> & ' hir ForeignItem < ' hir > {
782
- match self . tcx . hir_owner ( id) {
783
- Some ( OwnerNode :: ForeignItem ( item) ) => item,
790
+ match self . tcx . hir_owner_node ( id) {
791
+ OwnerNode :: ForeignItem ( item) => item,
784
792
_ => {
785
793
bug ! (
786
794
"expected foreign item, found {}" ,
0 commit comments