@@ -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 }
@@ -340,11 +340,23 @@ impl<'hir> Map<'hir> {
340340 }
341341 }
342342
343+ /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
344+ #[ inline]
345+ pub fn find_by_def_id ( & self , id : LocalDefId ) -> Option < Node < ' hir > > {
346+ self . find ( self . local_def_id_to_hir_id ( id) )
347+ }
348+
343349 /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
344350 pub fn get ( & self , id : HirId ) -> Node < ' hir > {
345351 self . find ( id) . unwrap_or_else ( || bug ! ( "couldn't find hir id {} in the HIR map" , id) )
346352 }
347353
354+ /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
355+ #[ inline]
356+ pub fn get_by_def_id ( & self , id : LocalDefId ) -> Node < ' hir > {
357+ self . find_by_def_id ( id) . unwrap_or_else ( || bug ! ( "couldn't find {:?} in the HIR map" , id) )
358+ }
359+
348360 pub fn get_if_local ( & self , id : DefId ) -> Option < Node < ' hir > > {
349361 id. as_local ( ) . and_then ( |id| self . find ( self . local_def_id_to_hir_id ( id) ) )
350362 }
@@ -780,23 +792,23 @@ impl<'hir> Map<'hir> {
780792 /// parent item is in this map. The "parent item" is the closest parent node
781793 /// in the HIR which is recorded by the map and is an item, either an item
782794 /// in a module, trait, or impl.
783- pub fn get_parent_item ( & self , hir_id : HirId ) -> HirId {
784- if let Some ( ( hir_id , _node) ) = self . parent_owner_iter ( hir_id) . next ( ) {
785- hir_id
795+ pub fn get_parent_item ( & self , hir_id : HirId ) -> LocalDefId {
796+ if let Some ( ( def_id , _node) ) = self . parent_owner_iter ( hir_id) . next ( ) {
797+ def_id
786798 } else {
787- CRATE_HIR_ID
799+ CRATE_DEF_ID
788800 }
789801 }
790802
791803 /// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
792804 /// module parent is in this map.
793- pub ( super ) fn get_module_parent_node ( & self , hir_id : HirId ) -> HirId {
794- for ( hir_id , node) in self . parent_owner_iter ( hir_id) {
805+ pub ( super ) fn get_module_parent_node ( & self , hir_id : HirId ) -> LocalDefId {
806+ for ( def_id , node) in self . parent_owner_iter ( hir_id) {
795807 if let OwnerNode :: Item ( & Item { kind : ItemKind :: Mod ( _) , .. } ) = node {
796- return hir_id ;
808+ return def_id ;
797809 }
798810 }
799- CRATE_HIR_ID
811+ CRATE_DEF_ID
800812 }
801813
802814 /// When on an if expression, a match arm tail expression or a match arm, give back
@@ -859,19 +871,18 @@ impl<'hir> Map<'hir> {
859871 }
860872 }
861873
862- pub fn get_parent_did ( & self , id : HirId ) -> LocalDefId {
863- self . local_def_id ( self . get_parent_item ( id) )
864- }
865-
866874 pub fn get_foreign_abi ( & self , hir_id : HirId ) -> Abi {
867875 let parent = self . get_parent_item ( hir_id) ;
868- if let Some ( node) = self . tcx . hir_owner ( self . local_def_id ( parent) ) {
876+ if let Some ( node) = self . tcx . hir_owner ( parent) {
869877 if let OwnerNode :: Item ( Item { kind : ItemKind :: ForeignMod { abi, .. } , .. } ) = node. node
870878 {
871879 return * abi;
872880 }
873881 }
874- bug ! ( "expected foreign mod or inlined parent, found {}" , self . node_to_string( parent) )
882+ bug ! (
883+ "expected foreign mod or inlined parent, found {}" ,
884+ self . node_to_string( HirId :: make_owner( parent) )
885+ )
875886 }
876887
877888 pub fn expect_item ( & self , id : LocalDefId ) -> & ' hir Item < ' hir > {
@@ -929,7 +940,7 @@ impl<'hir> Map<'hir> {
929940 Node :: Lifetime ( lt) => lt. name . ident ( ) . name ,
930941 Node :: GenericParam ( param) => param. name . ident ( ) . name ,
931942 Node :: Binding ( & Pat { kind : PatKind :: Binding ( _, _, l, _) , .. } ) => l. name ,
932- Node :: Ctor ( ..) => self . name ( self . get_parent_item ( id) ) ,
943+ Node :: Ctor ( ..) => self . name ( HirId :: make_owner ( self . get_parent_item ( id) ) ) ,
933944 _ => return None ,
934945 } )
935946 }
0 commit comments