@@ -117,13 +117,13 @@ pub struct ParentOwnerIterator<'hir> {
117117}
118118
119119impl < ' hir > Iterator for ParentOwnerIterator < ' hir > {
120- type Item = ( HirId , OwnerNode < ' hir > ) ;
120+ type Item = ( LocalDefId , OwnerNode < ' hir > ) ;
121121
122122 fn next ( & mut self ) -> Option < Self :: Item > {
123123 if self . current_id . local_id . index ( ) != 0 {
124124 self . current_id . local_id = ItemLocalId :: new ( 0 ) ;
125125 if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
126- return Some ( ( self . current_id , node. node ) ) ;
126+ return Some ( ( self . current_id . owner , node. node ) ) ;
127127 }
128128 }
129129 if self . current_id == CRATE_HIR_ID {
@@ -141,7 +141,7 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
141141
142142 // If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
143143 if let Some ( node) = self . map . tcx . hir_owner ( self . current_id . owner ) {
144- return Some ( ( self . current_id , node. node ) ) ;
144+ return Some ( ( self . current_id . owner , node. node ) ) ;
145145 }
146146 }
147147 }
@@ -328,11 +328,32 @@ impl<'hir> Map<'hir> {
328328 }
329329 }
330330
331+ /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
332+ pub fn find_def ( & self , id : LocalDefId ) -> Option < Node < ' hir > > {
333+ if let Some ( owner) = self . tcx . hir_owner ( id) {
334+ return Some ( owner. node . into ( ) ) ;
335+ }
336+
337+ let id = self . local_def_id_to_hir_id ( id) ;
338+ if id. local_id == ItemLocalId :: from_u32 ( 0 ) {
339+ None
340+ } else {
341+ let owner = self . tcx . hir_owner_nodes ( id. owner ) ?;
342+ let node = owner. nodes [ id. local_id ] . as_ref ( ) ?;
343+ Some ( node. node )
344+ }
345+ }
346+
331347 /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
332348 pub fn get ( & self , id : HirId ) -> Node < ' hir > {
333349 self . find ( id) . unwrap_or_else ( || bug ! ( "couldn't find hir id {} in the HIR map" , id) )
334350 }
335351
352+ /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
353+ pub fn get_def ( & self , id : LocalDefId ) -> Node < ' hir > {
354+ self . find_def ( id) . unwrap_or_else ( || bug ! ( "couldn't find {:?} in the HIR map" , id) )
355+ }
356+
336357 pub fn get_if_local ( & self , id : DefId ) -> Option < Node < ' hir > > {
337358 id. as_local ( ) . and_then ( |id| self . find ( self . local_def_id_to_hir_id ( id) ) )
338359 }
@@ -773,23 +794,23 @@ impl<'hir> Map<'hir> {
773794 /// parent item is in this map. The "parent item" is the closest parent node
774795 /// in the HIR which is recorded by the map and is an item, either an item
775796 /// in a module, trait, or impl.
776- pub fn get_parent_item ( & self , hir_id : HirId ) -> HirId {
777- if let Some ( ( hir_id , _node) ) = self . parent_owner_iter ( hir_id) . next ( ) {
778- hir_id
797+ pub fn get_parent_item ( & self , hir_id : HirId ) -> LocalDefId {
798+ if let Some ( ( def_id , _node) ) = self . parent_owner_iter ( hir_id) . next ( ) {
799+ def_id
779800 } else {
780- CRATE_HIR_ID
801+ CRATE_DEF_ID
781802 }
782803 }
783804
784805 /// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
785806 /// module parent is in this map.
786- pub ( super ) fn get_module_parent_node ( & self , hir_id : HirId ) -> HirId {
787- for ( hir_id , node) in self . parent_owner_iter ( hir_id) {
807+ pub ( super ) fn get_module_parent_node ( & self , hir_id : HirId ) -> LocalDefId {
808+ for ( def_id , node) in self . parent_owner_iter ( hir_id) {
788809 if let OwnerNode :: Item ( & Item { kind : ItemKind :: Mod ( _) , .. } ) = node {
789- return hir_id ;
810+ return def_id ;
790811 }
791812 }
792- CRATE_HIR_ID
813+ CRATE_DEF_ID
793814 }
794815
795816 /// When on an if expression, a match arm tail expression or a match arm, give back
@@ -852,19 +873,18 @@ impl<'hir> Map<'hir> {
852873 }
853874 }
854875
855- pub fn get_parent_did ( & self , id : HirId ) -> LocalDefId {
856- self . local_def_id ( self . get_parent_item ( id) )
857- }
858-
859876 pub fn get_foreign_abi ( & self , hir_id : HirId ) -> Abi {
860877 let parent = self . get_parent_item ( hir_id) ;
861- if let Some ( node) = self . tcx . hir_owner ( self . local_def_id ( parent) ) {
878+ if let Some ( node) = self . tcx . hir_owner ( parent) {
862879 if let OwnerNode :: Item ( Item { kind : ItemKind :: ForeignMod { abi, .. } , .. } ) = node. node
863880 {
864881 return * abi;
865882 }
866883 }
867- bug ! ( "expected foreign mod or inlined parent, found {}" , self . node_to_string( parent) )
884+ bug ! (
885+ "expected foreign mod or inlined parent, found {}" ,
886+ self . node_to_string( HirId :: make_owner( parent) )
887+ )
868888 }
869889
870890 pub fn expect_item ( & self , id : LocalDefId ) -> & ' hir Item < ' hir > {
@@ -922,7 +942,7 @@ impl<'hir> Map<'hir> {
922942 Node :: Lifetime ( lt) => lt. name . ident ( ) . name ,
923943 Node :: GenericParam ( param) => param. name . ident ( ) . name ,
924944 Node :: Binding ( & Pat { kind : PatKind :: Binding ( _, _, l, _) , .. } ) => l. name ,
925- Node :: Ctor ( ..) => self . name ( self . get_parent_item ( id) ) ,
945+ Node :: Ctor ( ..) => self . name ( HirId :: make_owner ( self . get_parent_item ( id) ) ) ,
926946 _ => return None ,
927947 } )
928948 }
0 commit comments