@@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
6
6
use rustc_ast:: ast:: { self , Name , NodeId } ;
7
7
use rustc_data_structures:: svh:: Svh ;
8
8
use rustc_hir:: def:: { DefKind , Res } ;
9
- use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
9
+ use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
10
10
use rustc_hir:: definitions:: { DefKey , DefPath , Definitions } ;
11
11
use rustc_hir:: intravisit;
12
12
use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
@@ -227,7 +227,12 @@ impl<'hir> Map<'hir> {
227
227
self . tcx . definitions . opt_local_def_id_to_hir_id ( def_id)
228
228
}
229
229
230
- pub fn def_kind ( & self , hir_id : HirId ) -> Option < DefKind > {
230
+ pub fn def_kind ( & self , local_def_id : LocalDefId ) -> Option < DefKind > {
231
+ if local_def_id. to_def_id ( ) . index == CRATE_DEF_INDEX {
232
+ return Some ( DefKind :: Mod ) ;
233
+ }
234
+
235
+ let hir_id = self . local_def_id_to_hir_id ( local_def_id) ;
231
236
let node = self . find ( hir_id) ?;
232
237
233
238
Some ( match node {
@@ -243,11 +248,11 @@ impl<'hir> Map<'hir> {
243
248
ItemKind :: Union ( ..) => DefKind :: Union ,
244
249
ItemKind :: Trait ( ..) => DefKind :: Trait ,
245
250
ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
246
- ItemKind :: ExternCrate ( _)
247
- | ItemKind :: Use ( ..)
248
- | ItemKind :: ForeignMod ( ..)
249
- | ItemKind :: GlobalAsm ( ..)
250
- | ItemKind :: Impl { .. } => return None ,
251
+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
252
+ ItemKind :: Use ( ..) => DefKind :: Use ,
253
+ ItemKind :: ForeignMod ( ..) => DefKind :: ForeignMod ,
254
+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
255
+ ItemKind :: Impl { .. } => DefKind :: Impl ,
251
256
} ,
252
257
Node :: ForeignItem ( item) => match item. kind {
253
258
ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -277,10 +282,19 @@ impl<'hir> Map<'hir> {
277
282
} ;
278
283
DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
279
284
}
280
- Node :: AnonConst ( _)
281
- | Node :: Field ( _)
282
- | Node :: Expr ( _)
283
- | Node :: Stmt ( _)
285
+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
286
+ Node :: Field ( _) => DefKind :: Field ,
287
+ Node :: Expr ( expr) => match expr. kind {
288
+ ExprKind :: Closure { .. } => DefKind :: Closure ,
289
+ _ => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
290
+ } ,
291
+ Node :: MacroDef ( _) => DefKind :: Macro ( MacroKind :: Bang ) ,
292
+ Node :: GenericParam ( param) => match param. kind {
293
+ GenericParamKind :: Lifetime { .. } => DefKind :: LifetimeParam ,
294
+ GenericParamKind :: Type { .. } => DefKind :: TyParam ,
295
+ GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
296
+ } ,
297
+ Node :: Stmt ( _)
284
298
| Node :: PathSegment ( _)
285
299
| Node :: Ty ( _)
286
300
| Node :: TraitRef ( _)
@@ -292,13 +306,7 @@ impl<'hir> Map<'hir> {
292
306
| Node :: Lifetime ( _)
293
307
| Node :: Visibility ( _)
294
308
| Node :: Block ( _)
295
- | Node :: Crate ( _) => return None ,
296
- Node :: MacroDef ( _) => DefKind :: Macro ( MacroKind :: Bang ) ,
297
- Node :: GenericParam ( param) => match param. kind {
298
- GenericParamKind :: Lifetime { .. } => return None ,
299
- GenericParamKind :: Type { .. } => DefKind :: TyParam ,
300
- GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
301
- } ,
309
+ | Node :: Crate ( _) => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
302
310
} )
303
311
}
304
312
@@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
1082
1090
}
1083
1091
1084
1092
pub fn provide ( providers : & mut Providers < ' _ > ) {
1085
- providers. def_kind =
1086
- |tcx, def_id| tcx. hir ( ) . def_kind ( tcx. hir ( ) . as_local_hir_id ( def_id. expect_local ( ) ) ) ;
1093
+ providers. def_kind = |tcx, def_id| tcx. hir ( ) . def_kind ( def_id. expect_local ( ) ) ;
1087
1094
}
0 commit comments