@@ -17,6 +17,7 @@ use syntax::ext::base::MacroKind;
17
17
use syntax_pos:: { Span , DUMMY_SP } ;
18
18
19
19
use crate :: hir:: * ;
20
+ use crate :: hir:: DefKind ;
20
21
use crate :: hir:: itemlikevisit:: ItemLikeVisitor ;
21
22
use crate :: hir:: print:: Nested ;
22
23
use crate :: util:: nodemap:: FxHashMap ;
@@ -309,74 +310,68 @@ impl<'hir> Map<'hir> {
309
310
self . definitions . as_local_node_id ( def_id. to_def_id ( ) ) . unwrap ( )
310
311
}
311
312
312
- pub fn describe_def ( & self , node_id : NodeId ) -> Option < Def > {
313
+ fn def_kind ( & self , node_id : NodeId ) -> Option < DefKind > {
313
314
let node = if let Some ( node) = self . find ( node_id) {
314
315
node
315
316
} else {
316
317
return None
317
318
} ;
318
319
319
- match node {
320
+ Some ( match node {
320
321
Node :: Item ( item) => {
321
- let def_id = || self . local_def_id_from_hir_id ( item. hir_id ) ;
322
-
323
322
match item. node {
324
- ItemKind :: Static ( ..) => Some ( Def :: Static ( def_id ( ) ) ) ,
325
- ItemKind :: Const ( ..) => Some ( Def :: Const ( def_id ( ) ) ) ,
326
- ItemKind :: Fn ( ..) => Some ( Def :: Fn ( def_id ( ) ) ) ,
327
- ItemKind :: Mod ( ..) => Some ( Def :: Mod ( def_id ( ) ) ) ,
328
- ItemKind :: Existential ( ..) => Some ( Def :: Existential ( def_id ( ) ) ) ,
329
- ItemKind :: Ty ( ..) => Some ( Def :: TyAlias ( def_id ( ) ) ) ,
330
- ItemKind :: Enum ( ..) => Some ( Def :: Enum ( def_id ( ) ) ) ,
331
- ItemKind :: Struct ( ..) => Some ( Def :: Struct ( def_id ( ) ) ) ,
332
- ItemKind :: Union ( ..) => Some ( Def :: Union ( def_id ( ) ) ) ,
333
- ItemKind :: Trait ( ..) => Some ( Def :: Trait ( def_id ( ) ) ) ,
334
- ItemKind :: TraitAlias ( ..) => Some ( Def :: TraitAlias ( def_id ( ) ) ) ,
323
+ ItemKind :: Static ( ..) => DefKind :: Static ,
324
+ ItemKind :: Const ( ..) => DefKind :: Const ,
325
+ ItemKind :: Fn ( ..) => DefKind :: Fn ,
326
+ ItemKind :: Mod ( ..) => DefKind :: Mod ,
327
+ ItemKind :: Existential ( ..) => DefKind :: Existential ,
328
+ ItemKind :: Ty ( ..) => DefKind :: TyAlias ,
329
+ ItemKind :: Enum ( ..) => DefKind :: Enum ,
330
+ ItemKind :: Struct ( ..) => DefKind :: Struct ,
331
+ ItemKind :: Union ( ..) => DefKind :: Union ,
332
+ ItemKind :: Trait ( ..) => DefKind :: Trait ,
333
+ ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
335
334
ItemKind :: ExternCrate ( _) |
336
335
ItemKind :: Use ( ..) |
337
336
ItemKind :: ForeignMod ( ..) |
338
337
ItemKind :: GlobalAsm ( ..) |
339
- ItemKind :: Impl ( ..) => None ,
338
+ ItemKind :: Impl ( ..) => return None ,
340
339
}
341
340
}
342
341
Node :: ForeignItem ( item) => {
343
- let def_id = self . local_def_id_from_hir_id ( item. hir_id ) ;
344
342
match item. node {
345
- ForeignItemKind :: Fn ( ..) => Some ( Def :: Fn ( def_id ) ) ,
346
- ForeignItemKind :: Static ( ..) => Some ( Def :: Static ( def_id ) ) ,
347
- ForeignItemKind :: Type => Some ( Def :: ForeignTy ( def_id ) ) ,
343
+ ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
344
+ ForeignItemKind :: Static ( ..) => DefKind :: Static ,
345
+ ForeignItemKind :: Type => DefKind :: ForeignTy ,
348
346
}
349
347
}
350
348
Node :: TraitItem ( item) => {
351
- let def_id = self . local_def_id_from_hir_id ( item. hir_id ) ;
352
349
match item. node {
353
- TraitItemKind :: Const ( ..) => Some ( Def :: AssociatedConst ( def_id ) ) ,
354
- TraitItemKind :: Method ( ..) => Some ( Def :: Method ( def_id ) ) ,
355
- TraitItemKind :: Type ( ..) => Some ( Def :: AssociatedTy ( def_id ) ) ,
350
+ TraitItemKind :: Const ( ..) => DefKind :: AssociatedConst ,
351
+ TraitItemKind :: Method ( ..) => DefKind :: Method ,
352
+ TraitItemKind :: Type ( ..) => DefKind :: AssociatedTy ,
356
353
}
357
354
}
358
355
Node :: ImplItem ( item) => {
359
- let def_id = self . local_def_id_from_hir_id ( item. hir_id ) ;
360
356
match item. node {
361
- ImplItemKind :: Const ( ..) => Some ( Def :: AssociatedConst ( def_id ) ) ,
362
- ImplItemKind :: Method ( ..) => Some ( Def :: Method ( def_id ) ) ,
363
- ImplItemKind :: Type ( ..) => Some ( Def :: AssociatedTy ( def_id ) ) ,
364
- ImplItemKind :: Existential ( ..) => Some ( Def :: AssociatedExistential ( def_id ) ) ,
357
+ ImplItemKind :: Const ( ..) => DefKind :: AssociatedConst ,
358
+ ImplItemKind :: Method ( ..) => DefKind :: Method ,
359
+ ImplItemKind :: Type ( ..) => DefKind :: AssociatedTy ,
360
+ ImplItemKind :: Existential ( ..) => DefKind :: AssociatedExistential ,
365
361
}
366
362
}
367
- Node :: Variant ( variant) => {
368
- let def_id = self . local_def_id_from_hir_id ( variant. node . id ) ;
369
- Some ( Def :: Variant ( def_id) )
370
- }
363
+ Node :: Variant ( _) => DefKind :: Variant ,
371
364
Node :: Ctor ( variant_data) => {
365
+ // FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
366
+ if variant_data. ctor_hir_id ( ) . is_none ( ) {
367
+ return None ;
368
+ }
372
369
let ctor_of = match self . find ( self . get_parent_node ( node_id) ) {
373
370
Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
374
371
Some ( Node :: Variant ( ..) ) => def:: CtorOf :: Variant ,
375
372
_ => unreachable ! ( ) ,
376
373
} ;
377
- variant_data. ctor_hir_id ( )
378
- . map ( |hir_id| self . local_def_id_from_hir_id ( hir_id) )
379
- . map ( |def_id| Def :: Ctor ( def_id, ctor_of, def:: CtorKind :: from_hir ( variant_data) ) )
374
+ DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
380
375
}
381
376
Node :: AnonConst ( _) |
382
377
Node :: Field ( _) |
@@ -387,35 +382,20 @@ impl<'hir> Map<'hir> {
387
382
Node :: TraitRef ( _) |
388
383
Node :: Pat ( _) |
389
384
Node :: Binding ( _) |
385
+ Node :: Local ( _) |
390
386
Node :: Lifetime ( _) |
391
387
Node :: Visibility ( _) |
392
388
Node :: Block ( _) |
393
- Node :: Crate => None ,
394
- Node :: Local ( local) => {
395
- Some ( Def :: Local ( local. hir_id ) )
396
- }
397
- Node :: MacroDef ( macro_def) => {
398
- Some ( Def :: Macro ( self . local_def_id_from_hir_id ( macro_def. hir_id ) ,
399
- MacroKind :: Bang ) )
400
- }
389
+ Node :: Crate => return None ,
390
+ Node :: MacroDef ( _) => DefKind :: Macro ( MacroKind :: Bang ) ,
401
391
Node :: GenericParam ( param) => {
402
- Some ( match param. kind {
403
- GenericParamKind :: Lifetime { .. } => {
404
- Def :: Local ( param. hir_id )
405
- } ,
406
- GenericParamKind :: Type { .. } => Def :: TyParam (
407
- self . local_def_id_from_hir_id ( param. hir_id ) ) ,
408
- GenericParamKind :: Const { .. } => Def :: ConstParam (
409
- self . local_def_id_from_hir_id ( param. hir_id ) ) ,
410
- } )
392
+ match param. kind {
393
+ GenericParamKind :: Lifetime { .. } => return None ,
394
+ GenericParamKind :: Type { .. } => DefKind :: TyParam ,
395
+ GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
396
+ }
411
397
}
412
- }
413
- }
414
-
415
- // FIXME(@ljedrz): replace the NodeId variant
416
- pub fn describe_def_by_hir_id ( & self , hir_id : HirId ) -> Option < Def > {
417
- let node_id = self . hir_to_node_id ( hir_id) ;
418
- self . describe_def ( node_id)
398
+ } )
419
399
}
420
400
421
401
fn entry_count ( & self ) -> usize {
@@ -1473,11 +1453,11 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1473
1453
node_id_to_string ( map, node_id, include_id)
1474
1454
}
1475
1455
1476
- pub fn describe_def ( tcx : TyCtxt < ' _ , ' _ , ' _ > , def_id : DefId ) -> Option < Def > {
1456
+ pub fn def_kind ( tcx : TyCtxt < ' _ , ' _ , ' _ > , def_id : DefId ) -> Option < DefKind > {
1477
1457
if let Some ( node_id) = tcx. hir ( ) . as_local_node_id ( def_id) {
1478
- tcx. hir ( ) . describe_def ( node_id)
1458
+ tcx. hir ( ) . def_kind ( node_id)
1479
1459
} else {
1480
- bug ! ( "Calling local describe_def query provider for upstream DefId: {:?}" ,
1460
+ bug ! ( "Calling local def_kind query provider for upstream DefId: {:?}" ,
1481
1461
def_id)
1482
1462
}
1483
1463
}
0 commit comments