@@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
66use rustc_ast:: ast:: { self , Name , NodeId } ;
77use rustc_data_structures:: svh:: Svh ;
88use 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 } ;
1010use rustc_hir:: definitions:: { DefKey , DefPath , Definitions } ;
1111use rustc_hir:: intravisit;
1212use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
@@ -227,7 +227,12 @@ impl<'hir> Map<'hir> {
227227 self . tcx . definitions . opt_local_def_id_to_hir_id ( def_id)
228228 }
229229
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) ;
231236 let node = self . find ( hir_id) ?;
232237
233238 Some ( match node {
@@ -243,11 +248,11 @@ impl<'hir> Map<'hir> {
243248 ItemKind :: Union ( ..) => DefKind :: Union ,
244249 ItemKind :: Trait ( ..) => DefKind :: Trait ,
245250 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 ,
251256 } ,
252257 Node :: ForeignItem ( item) => match item. kind {
253258 ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -277,10 +282,19 @@ impl<'hir> Map<'hir> {
277282 } ;
278283 DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
279284 }
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 ( _)
284298 | Node :: PathSegment ( _)
285299 | Node :: Ty ( _)
286300 | Node :: TraitRef ( _)
@@ -292,13 +306,7 @@ impl<'hir> Map<'hir> {
292306 | Node :: Lifetime ( _)
293307 | Node :: Visibility ( _)
294308 | 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) ) ,
302310 } )
303311 }
304312
@@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10821090}
10831091
10841092pub 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 ( ) ) ;
10871094}
0 commit comments