@@ -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,10 +227,14 @@ 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 > {
231
- let node = self . find ( hir_id) ?;
230
+ pub fn def_kind ( & self , local_def_id : LocalDefId ) -> DefKind {
231
+ // FIXME(eddyb) support `find` on the crate root.
232
+ if local_def_id. to_def_id ( ) . index == CRATE_DEF_INDEX {
233
+ return DefKind :: Mod ;
234
+ }
232
235
233
- Some ( match node {
236
+ let hir_id = self . local_def_id_to_hir_id ( local_def_id) ;
237
+ match self . get ( hir_id) {
234
238
Node :: Item ( item) => match item. kind {
235
239
ItemKind :: Static ( ..) => DefKind :: Static ,
236
240
ItemKind :: Const ( ..) => DefKind :: Const ,
@@ -243,11 +247,11 @@ impl<'hir> Map<'hir> {
243
247
ItemKind :: Union ( ..) => DefKind :: Union ,
244
248
ItemKind :: Trait ( ..) => DefKind :: Trait ,
245
249
ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
246
- ItemKind :: ExternCrate ( _)
247
- | ItemKind :: Use ( ..)
248
- | ItemKind :: ForeignMod ( ..)
249
- | ItemKind :: GlobalAsm ( ..)
250
- | ItemKind :: Impl { .. } => return None ,
250
+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
251
+ ItemKind :: Use ( ..) => DefKind :: Use ,
252
+ ItemKind :: ForeignMod ( ..) => DefKind :: ForeignMod ,
253
+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
254
+ ItemKind :: Impl { .. } => DefKind :: Impl ,
251
255
} ,
252
256
Node :: ForeignItem ( item) => match item. kind {
253
257
ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -268,7 +272,7 @@ impl<'hir> Map<'hir> {
268
272
Node :: Variant ( _) => DefKind :: Variant ,
269
273
Node :: Ctor ( variant_data) => {
270
274
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
271
- variant_data. ctor_hir_id ( ) ? ;
275
+ assert_ne ! ( variant_data. ctor_hir_id( ) , None ) ;
272
276
273
277
let ctor_of = match self . find ( self . get_parent_node ( hir_id) ) {
274
278
Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
@@ -277,10 +281,20 @@ impl<'hir> Map<'hir> {
277
281
} ;
278
282
DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
279
283
}
280
- Node :: AnonConst ( _)
281
- | Node :: Field ( _)
282
- | Node :: Expr ( _)
283
- | Node :: Stmt ( _)
284
+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
285
+ Node :: Field ( _) => DefKind :: Field ,
286
+ Node :: Expr ( expr) => match expr. kind {
287
+ ExprKind :: Closure ( .., None ) => DefKind :: Closure ,
288
+ ExprKind :: Closure ( .., Some ( _) ) => DefKind :: Generator ,
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,14 +306,8 @@ 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
- } ,
302
- } )
309
+ | Node :: Crate ( _) => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
310
+ }
303
311
}
304
312
305
313
fn find_entry ( & self , id : HirId ) -> Option < Entry < ' hir > > {
@@ -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