@@ -4,6 +4,7 @@ use crate::hir::{Owner, OwnerNodes};
44use crate :: ty:: query:: Providers ;
55use crate :: ty:: TyCtxt ;
66use rustc_ast:: ast:: { self , Name , NodeId } ;
7+ use rustc_data_structures:: fx:: FxHashMap ;
78use rustc_data_structures:: svh:: Svh ;
89use rustc_hir:: def:: { DefKind , Res } ;
910use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
@@ -89,6 +90,7 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
8990pub ( super ) struct HirOwnerData < ' hir > {
9091 pub ( super ) signature : Option < & ' hir Owner < ' hir > > ,
9192 pub ( super ) with_bodies : Option < & ' hir mut OwnerNodes < ' hir > > ,
93+ pub ( super ) defs : Option < & ' hir mut FxHashMap < ItemLocalId , LocalDefId > > ,
9294}
9395
9496pub struct IndexedHir < ' hir > {
@@ -184,8 +186,14 @@ impl<'hir> Map<'hir> {
184186
185187 #[ inline]
186188 pub fn opt_local_def_id ( & self , hir_id : HirId ) -> Option < DefId > {
187- let node_id = self . hir_id_to_node_id ( hir_id) ;
188- self . opt_local_def_id_from_node_id ( node_id)
189+ if hir_id. local_id == ItemLocalId :: from_u32 ( 0 ) {
190+ // Every HirId owner has a DefId, so we can just return it directly here
191+ Some ( hir_id. owner . to_def_id ( ) )
192+ } else {
193+ self . tcx
194+ . hir_owner_defs ( hir_id. owner )
195+ . and_then ( |map| map. get ( & hir_id. local_id ) . map ( |id| id. to_def_id ( ) ) )
196+ }
189197 }
190198
191199 #[ inline]
@@ -200,7 +208,7 @@ impl<'hir> Map<'hir> {
200208
201209 #[ inline]
202210 pub fn as_local_hir_id ( & self , def_id : DefId ) -> Option < HirId > {
203- self . tcx . definitions . as_local_hir_id ( def_id)
211+ def_id . as_local ( ) . and_then ( |def_id| self . tcx . local_def_id_to_hir_id ( def_id) )
204212 }
205213
206214 #[ inline]
0 commit comments