@@ -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 ;
@@ -229,10 +229,14 @@ impl<'hir> Map<'hir> {
229
229
self . tcx . definitions . opt_local_def_id_to_hir_id ( def_id)
230
230
}
231
231
232
- pub fn def_kind ( & self , hir_id : HirId ) -> Option < DefKind > {
233
- let node = self . find ( hir_id) ?;
232
+ pub fn def_kind ( & self , local_def_id : LocalDefId ) -> DefKind {
233
+ // FIXME(eddyb) support `find` on the crate root.
234
+ if local_def_id. to_def_id ( ) . index == CRATE_DEF_INDEX {
235
+ return DefKind :: Mod ;
236
+ }
234
237
235
- Some ( match node {
238
+ let hir_id = self . local_def_id_to_hir_id ( local_def_id) ;
239
+ match self . get ( hir_id) {
236
240
Node :: Item ( item) => match item. kind {
237
241
ItemKind :: Static ( ..) => DefKind :: Static ,
238
242
ItemKind :: Const ( ..) => DefKind :: Const ,
@@ -245,11 +249,11 @@ impl<'hir> Map<'hir> {
245
249
ItemKind :: Union ( ..) => DefKind :: Union ,
246
250
ItemKind :: Trait ( ..) => DefKind :: Trait ,
247
251
ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
248
- ItemKind :: ExternCrate ( _)
249
- | ItemKind :: Use ( ..)
250
- | ItemKind :: ForeignMod ( ..)
251
- | ItemKind :: GlobalAsm ( ..)
252
- | ItemKind :: Impl { .. } => return None ,
252
+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
253
+ ItemKind :: Use ( ..) => DefKind :: Use ,
254
+ ItemKind :: ForeignMod ( ..) => DefKind :: ForeignMod ,
255
+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
256
+ ItemKind :: Impl { .. } => DefKind :: Impl ,
253
257
} ,
254
258
Node :: ForeignItem ( item) => match item. kind {
255
259
ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -270,7 +274,7 @@ impl<'hir> Map<'hir> {
270
274
Node :: Variant ( _) => DefKind :: Variant ,
271
275
Node :: Ctor ( variant_data) => {
272
276
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
273
- variant_data. ctor_hir_id ( ) ? ;
277
+ assert_ne ! ( variant_data. ctor_hir_id( ) , None ) ;
274
278
275
279
let ctor_of = match self . find ( self . get_parent_node ( hir_id) ) {
276
280
Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
@@ -279,10 +283,20 @@ impl<'hir> Map<'hir> {
279
283
} ;
280
284
DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
281
285
}
282
- Node :: AnonConst ( _)
283
- | Node :: Field ( _)
284
- | Node :: Expr ( _)
285
- | Node :: Stmt ( _)
286
+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
287
+ Node :: Field ( _) => DefKind :: Field ,
288
+ Node :: Expr ( expr) => match expr. kind {
289
+ ExprKind :: Closure ( .., None ) => DefKind :: Closure ,
290
+ ExprKind :: Closure ( .., Some ( _) ) => DefKind :: Generator ,
291
+ _ => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
292
+ } ,
293
+ Node :: MacroDef ( _) => DefKind :: Macro ( MacroKind :: Bang ) ,
294
+ Node :: GenericParam ( param) => match param. kind {
295
+ GenericParamKind :: Lifetime { .. } => DefKind :: LifetimeParam ,
296
+ GenericParamKind :: Type { .. } => DefKind :: TyParam ,
297
+ GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
298
+ } ,
299
+ Node :: Stmt ( _)
286
300
| Node :: PathSegment ( _)
287
301
| Node :: Ty ( _)
288
302
| Node :: TraitRef ( _)
@@ -294,14 +308,8 @@ impl<'hir> Map<'hir> {
294
308
| Node :: Lifetime ( _)
295
309
| Node :: Visibility ( _)
296
310
| Node :: Block ( _)
297
- | Node :: Crate ( _) => return None ,
298
- Node :: MacroDef ( _) => DefKind :: Macro ( MacroKind :: Bang ) ,
299
- Node :: GenericParam ( param) => match param. kind {
300
- GenericParamKind :: Lifetime { .. } => return None ,
301
- GenericParamKind :: Type { .. } => DefKind :: TyParam ,
302
- GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
303
- } ,
304
- } )
311
+ | Node :: Crate ( _) => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
312
+ }
305
313
}
306
314
307
315
fn find_entry ( & self , id : HirId ) -> Option < Entry < ' hir > > {
@@ -1084,11 +1092,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
1084
1092
}
1085
1093
1086
1094
pub fn provide ( providers : & mut Providers < ' _ > ) {
1087
- providers. def_kind = |tcx, def_id| {
1088
- if let Some ( hir_id) = tcx. hir ( ) . as_local_hir_id ( def_id) {
1089
- tcx. hir ( ) . def_kind ( hir_id)
1090
- } else {
1091
- bug ! ( "calling local def_kind query provider for upstream DefId: {:?}" , def_id) ;
1092
- }
1093
- } ;
1095
+ providers. def_kind = |tcx, def_id| tcx. hir ( ) . def_kind ( def_id. expect_local ( ) ) ;
1094
1096
}
0 commit comments