1
1
use self :: collector:: NodeCollector ;
2
2
3
- use crate :: hir:: { AttributeMap , IndexedHir } ;
3
+ use crate :: hir:: { AttributeMap , IndexedHir , Owner } ;
4
4
use crate :: ty:: TyCtxt ;
5
5
use rustc_ast as ast;
6
6
use rustc_data_structures:: fingerprint:: Fingerprint ;
@@ -121,13 +121,13 @@ pub struct ParentOwnerIterator<'map, 'hir> {
121
121
}
122
122
123
123
impl < ' hir > Iterator for ParentOwnerIterator < ' _ , ' hir > {
124
- type Item = ( HirId , Node < ' hir > ) ;
124
+ type Item = ( HirId , OwnerNode < ' hir > ) ;
125
125
126
126
fn next ( & mut self ) -> Option < Self :: Item > {
127
127
if self . current_id . local_id . index ( ) != 0 {
128
128
self . current_id . local_id = ItemLocalId :: new ( 0 ) ;
129
- if let Some ( node) = self . map . find ( self . current_id ) {
130
- return Some ( ( self . current_id , node) ) ;
129
+ if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
130
+ return Some ( ( self . current_id , node. node ) ) ;
131
131
}
132
132
}
133
133
if self . current_id == CRATE_HIR_ID {
@@ -144,8 +144,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> {
144
144
self . current_id = HirId :: make_owner ( parent_id) ;
145
145
146
146
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
147
- if let Some ( node) = self . map . find ( self . current_id ) {
148
- return Some ( ( self . current_id , node) ) ;
147
+ if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
148
+ return Some ( ( self . current_id , node. node ) ) ;
149
149
}
150
150
}
151
151
}
@@ -331,10 +331,12 @@ impl<'hir> Map<'hir> {
331
331
}
332
332
333
333
pub fn get_generics ( & self , id : DefId ) -> Option < & ' hir Generics < ' hir > > {
334
- self . get_if_local ( id) . and_then ( |node| match & node {
335
- Node :: ImplItem ( impl_item) => Some ( & impl_item. generics ) ,
336
- Node :: TraitItem ( trait_item) => Some ( & trait_item. generics ) ,
337
- Node :: Item ( Item {
334
+ let id = id. as_local ( ) ?;
335
+ let node = self . tcx . hir_owner ( id) ?;
336
+ match node. node {
337
+ OwnerNode :: ImplItem ( impl_item) => Some ( & impl_item. generics ) ,
338
+ OwnerNode :: TraitItem ( trait_item) => Some ( & trait_item. generics ) ,
339
+ OwnerNode :: Item ( Item {
338
340
kind :
339
341
ItemKind :: Fn ( _, generics, _)
340
342
| ItemKind :: TyAlias ( _, generics)
@@ -347,35 +349,23 @@ impl<'hir> Map<'hir> {
347
349
..
348
350
} ) => Some ( generics) ,
349
351
_ => None ,
350
- } )
352
+ }
351
353
}
352
354
353
355
pub fn item ( & self , id : ItemId ) -> & ' hir Item < ' hir > {
354
- match self . find ( id. hir_id ( ) ) . unwrap ( ) {
355
- Node :: Item ( item) => item,
356
- _ => bug ! ( ) ,
357
- }
356
+ self . tcx . hir_owner ( id. def_id ) . unwrap ( ) . node . expect_item ( )
358
357
}
359
358
360
359
pub fn trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > {
361
- match self . find ( id. hir_id ( ) ) . unwrap ( ) {
362
- Node :: TraitItem ( item) => item,
363
- _ => bug ! ( ) ,
364
- }
360
+ self . tcx . hir_owner ( id. def_id ) . unwrap ( ) . node . expect_trait_item ( )
365
361
}
366
362
367
363
pub fn impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > {
368
- match self . find ( id. hir_id ( ) ) . unwrap ( ) {
369
- Node :: ImplItem ( item) => item,
370
- _ => bug ! ( ) ,
371
- }
364
+ self . tcx . hir_owner ( id. def_id ) . unwrap ( ) . node . expect_impl_item ( )
372
365
}
373
366
374
367
pub fn foreign_item ( & self , id : ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
375
- match self . find ( id. hir_id ( ) ) . unwrap ( ) {
376
- Node :: ForeignItem ( item) => item,
377
- _ => bug ! ( ) ,
378
- }
368
+ self . tcx . hir_owner ( id. def_id ) . unwrap ( ) . node . expect_foreign_item ( )
379
369
}
380
370
381
371
pub fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > {
@@ -519,10 +509,12 @@ impl<'hir> Map<'hir> {
519
509
}
520
510
521
511
pub fn get_module ( & self , module : LocalDefId ) -> ( & ' hir Mod < ' hir > , Span , HirId ) {
522
- let hir_id = self . local_def_id_to_hir_id ( module) ;
523
- match self . get ( hir_id) {
524
- Node :: Item ( & Item { span, kind : ItemKind :: Mod ( ref m) , .. } ) => ( m, span, hir_id) ,
525
- Node :: Crate ( item) => ( & item, item. inner , hir_id) ,
512
+ let hir_id = HirId :: make_owner ( module) ;
513
+ match self . tcx . hir_owner ( module) . map ( |o| o. node ) {
514
+ Some ( OwnerNode :: Item ( & Item { span, kind : ItemKind :: Mod ( ref m) , .. } ) ) => {
515
+ ( m, span, hir_id)
516
+ }
517
+ Some ( OwnerNode :: Crate ( item) ) => ( item, item. inner , hir_id) ,
526
518
node => panic ! ( "not a module: {:?}" , node) ,
527
519
}
528
520
}
@@ -659,24 +651,20 @@ impl<'hir> Map<'hir> {
659
651
/// in the HIR which is recorded by the map and is an item, either an item
660
652
/// in a module, trait, or impl.
661
653
pub fn get_parent_item ( & self , hir_id : HirId ) -> HirId {
662
- for ( hir_id, node) in self . parent_owner_iter ( hir_id) {
663
- if let Node :: Crate ( _)
664
- | Node :: Item ( _)
665
- | Node :: ForeignItem ( _)
666
- | Node :: TraitItem ( _)
667
- | Node :: ImplItem ( _) = node
668
- {
669
- return hir_id;
670
- }
654
+ if let Some ( ( hir_id, _node) ) = self . parent_owner_iter ( hir_id) . next ( ) {
655
+ // A MacroDef does not have children.
656
+ debug_assert ! ( !matches!( _node, OwnerNode :: MacroDef ( _) ) ) ;
657
+ hir_id
658
+ } else {
659
+ CRATE_HIR_ID
671
660
}
672
- CRATE_HIR_ID
673
661
}
674
662
675
663
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
676
664
/// module parent is in this map.
677
665
pub ( super ) fn get_module_parent_node ( & self , hir_id : HirId ) -> HirId {
678
666
for ( hir_id, node) in self . parent_owner_iter ( hir_id) {
679
- if let Node :: Item ( & Item { kind : ItemKind :: Mod ( _) , .. } ) = node {
667
+ if let OwnerNode :: Item ( & Item { kind : ItemKind :: Mod ( _) , .. } ) = node {
680
668
return hir_id;
681
669
}
682
670
}
@@ -749,31 +737,32 @@ impl<'hir> Map<'hir> {
749
737
750
738
pub fn get_foreign_abi ( & self , hir_id : HirId ) -> Abi {
751
739
let parent = self . get_parent_item ( hir_id) ;
752
- if let Some ( node) = self . find ( parent) {
753
- if let Node :: Item ( Item { kind : ItemKind :: ForeignMod { abi, .. } , .. } ) = node {
740
+ if let Some ( node) = self . tcx . hir_owner ( self . local_def_id ( parent) ) {
741
+ if let OwnerNode :: Item ( Item { kind : ItemKind :: ForeignMod { abi, .. } , .. } ) = node. node
742
+ {
754
743
return * abi;
755
744
}
756
745
}
757
746
bug ! ( "expected foreign mod or inlined parent, found {}" , self . node_to_string( parent) )
758
747
}
759
748
760
749
pub fn expect_item ( & self , id : HirId ) -> & ' hir Item < ' hir > {
761
- match self . find ( id) {
762
- Some ( Node :: Item ( item) ) => item,
750
+ match self . tcx . hir_owner ( id. expect_owner ( ) ) {
751
+ Some ( Owner { node : OwnerNode :: Item ( item) } ) => item,
763
752
_ => bug ! ( "expected item, found {}" , self . node_to_string( id) ) ,
764
753
}
765
754
}
766
755
767
756
pub fn expect_impl_item ( & self , id : HirId ) -> & ' hir ImplItem < ' hir > {
768
- match self . find ( id) {
769
- Some ( Node :: ImplItem ( item) ) => item,
757
+ match self . tcx . hir_owner ( id. expect_owner ( ) ) {
758
+ Some ( Owner { node : OwnerNode :: ImplItem ( item) } ) => item,
770
759
_ => bug ! ( "expected impl item, found {}" , self . node_to_string( id) ) ,
771
760
}
772
761
}
773
762
774
763
pub fn expect_trait_item ( & self , id : HirId ) -> & ' hir TraitItem < ' hir > {
775
- match self . find ( id) {
776
- Some ( Node :: TraitItem ( item) ) => item,
764
+ match self . tcx . hir_owner ( id. expect_owner ( ) ) {
765
+ Some ( Owner { node : OwnerNode :: TraitItem ( item) } ) => item,
777
766
_ => bug ! ( "expected trait item, found {}" , self . node_to_string( id) ) ,
778
767
}
779
768
}
@@ -786,15 +775,15 @@ impl<'hir> Map<'hir> {
786
775
}
787
776
788
777
pub fn expect_foreign_item ( & self , id : HirId ) -> & ' hir ForeignItem < ' hir > {
789
- match self . find ( id) {
790
- Some ( Node :: ForeignItem ( item) ) => item,
778
+ match self . tcx . hir_owner ( id. expect_owner ( ) ) {
779
+ Some ( Owner { node : OwnerNode :: ForeignItem ( item) } ) => item,
791
780
_ => bug ! ( "expected foreign item, found {}" , self . node_to_string( id) ) ,
792
781
}
793
782
}
794
783
795
784
pub fn expect_macro_def ( & self , id : HirId ) -> & ' hir MacroDef < ' hir > {
796
- match self . find ( id) {
797
- Some ( Node :: MacroDef ( macro_def) ) => macro_def,
785
+ match self . tcx . hir_owner ( id. expect_owner ( ) ) {
786
+ Some ( Owner { node : OwnerNode :: MacroDef ( macro_def) } ) => macro_def,
798
787
_ => bug ! ( "expected macro def, found {}" , self . node_to_string( id) ) ,
799
788
}
800
789
}
0 commit comments