@@ -4,6 +4,7 @@ use crate::hir::{Owner, OwnerNodes};
4
4
use crate :: ty:: query:: Providers ;
5
5
use crate :: ty:: TyCtxt ;
6
6
use rustc_ast:: ast:: { self , Name , NodeId } ;
7
+ use rustc_data_structures:: fx:: FxHashMap ;
7
8
use rustc_data_structures:: svh:: Svh ;
8
9
use rustc_hir:: def:: { DefKind , Res } ;
9
10
use 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 {
89
90
pub ( super ) struct HirOwnerData < ' hir > {
90
91
pub ( super ) signature : Option < & ' hir Owner < ' hir > > ,
91
92
pub ( super ) with_bodies : Option < & ' hir mut OwnerNodes < ' hir > > ,
93
+ pub ( super ) defs : Option < & ' hir mut FxHashMap < ItemLocalId , LocalDefId > > ,
92
94
}
93
95
94
96
pub struct IndexedHir < ' hir > {
@@ -184,8 +186,14 @@ impl<'hir> Map<'hir> {
184
186
185
187
#[ inline]
186
188
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
+ }
189
197
}
190
198
191
199
#[ inline]
@@ -200,7 +208,7 @@ impl<'hir> Map<'hir> {
200
208
201
209
#[ inline]
202
210
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) )
204
212
}
205
213
206
214
#[ inline]
0 commit comments