@@ -11,12 +11,12 @@ use std::fmt::Debug;
11
11
12
12
use self :: Namespace :: * ;
13
13
14
- /// Encodes if a `Def ::Ctor` is the constructor of an enum variant or a struct.
14
+ /// Encodes if a `DefKind ::Ctor` is the constructor of an enum variant or a struct.
15
15
#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
16
16
pub enum CtorOf {
17
- /// This `Def ::Ctor` is a synthesized constructor of a tuple or unit struct.
17
+ /// This `DefKind ::Ctor` is a synthesized constructor of a tuple or unit struct.
18
18
Struct ,
19
- /// This `Def ::Ctor` is a synthesized constructor of a tuple or unit variant.
19
+ /// This `DefKind ::Ctor` is a synthesized constructor of a tuple or unit variant.
20
20
Variant ,
21
21
}
22
22
@@ -45,52 +45,62 @@ pub enum NonMacroAttrKind {
45
45
}
46
46
47
47
#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
48
- pub enum Def < Id = hir :: HirId > {
48
+ pub enum DefKind {
49
49
// Type namespace
50
- Mod ( DefId ) ,
51
- /// `DefId` refers to the struct itself, `Def ::Ctor` refers to its constructor if it exists.
52
- Struct ( DefId ) ,
53
- Union ( DefId ) ,
54
- Enum ( DefId ) ,
55
- /// `DefId` refers to the variant itself, `Def ::Ctor` refers to its constructor if it exists.
56
- Variant ( DefId ) ,
57
- Trait ( DefId ) ,
50
+ Mod ,
51
+ /// Refers to the struct itself, `DefKind ::Ctor` refers to its constructor if it exists.
52
+ Struct ,
53
+ Union ,
54
+ Enum ,
55
+ /// Refers to the variant itself, `DefKind ::Ctor` refers to its constructor if it exists.
56
+ Variant ,
57
+ Trait ,
58
58
/// `existential type Foo: Bar;`
59
- Existential ( DefId ) ,
59
+ Existential ,
60
60
/// `type Foo = Bar;`
61
- TyAlias ( DefId ) ,
62
- ForeignTy ( DefId ) ,
63
- TraitAlias ( DefId ) ,
64
- AssociatedTy ( DefId ) ,
61
+ TyAlias ,
62
+ ForeignTy ,
63
+ TraitAlias ,
64
+ AssociatedTy ,
65
65
/// `existential type Foo: Bar;`
66
- AssociatedExistential ( DefId ) ,
66
+ AssociatedExistential ,
67
+ TyParam ,
68
+
69
+ // Value namespace
70
+ Fn ,
71
+ Const ,
72
+ ConstParam ,
73
+ Static ,
74
+ /// Refers to the struct or enum variant's constructor.
75
+ Ctor ( CtorOf , CtorKind ) ,
76
+ Method ,
77
+ AssociatedConst ,
78
+
79
+ // Macro namespace
80
+ Macro ( MacroKind ) ,
81
+ }
82
+
83
+ #[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
84
+ pub enum Def < Id = hir:: HirId > {
85
+ Def ( DefKind , DefId ) ,
86
+
87
+ // Type namespace
67
88
PrimTy ( hir:: PrimTy ) ,
68
- TyParam ( DefId ) ,
69
89
SelfTy ( Option < DefId > /* trait */ , Option < DefId > /* impl */ ) ,
70
90
ToolMod , // e.g., `rustfmt` in `#[rustfmt::skip]`
71
91
72
92
// Value namespace
73
- Fn ( DefId ) ,
74
- Const ( DefId ) ,
75
- ConstParam ( DefId ) ,
76
- Static ( DefId ) ,
77
- /// `DefId` refers to the struct or enum variant's constructor.
78
- Ctor ( DefId , CtorOf , CtorKind ) ,
79
93
SelfCtor ( DefId /* impl */ ) , // `DefId` refers to the impl
80
- Method ( DefId ) ,
81
- AssociatedConst ( DefId ) ,
82
-
83
94
Local ( Id ) ,
84
95
Upvar ( Id , // `HirId` of closed over local
85
96
usize , // index in the `freevars` list of the closure
86
97
ast:: NodeId ) , // expr node that creates the closure
87
98
Label ( ast:: NodeId ) ,
88
99
89
100
// Macro namespace
90
- Macro ( DefId , MacroKind ) ,
91
101
NonMacroAttr ( NonMacroAttrKind ) , // e.g., `#[inline]` or `#[rustfmt::skip]`
92
102
93
- // Both namespaces
103
+ // All namespaces
94
104
Err ,
95
105
}
96
106
@@ -291,15 +301,7 @@ impl<Id> Def<Id> {
291
301
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
292
302
pub fn opt_def_id ( & self ) -> Option < DefId > {
293
303
match * self {
294
- Def :: Fn ( id) | Def :: Mod ( id) | Def :: Static ( id) |
295
- Def :: Variant ( id) | Def :: Ctor ( id, ..) | Def :: Enum ( id) |
296
- Def :: TyAlias ( id) | Def :: TraitAlias ( id) |
297
- Def :: AssociatedTy ( id) | Def :: TyParam ( id) | Def :: ConstParam ( id) | Def :: Struct ( id) |
298
- Def :: Union ( id) | Def :: Trait ( id) | Def :: Method ( id) | Def :: Const ( id) |
299
- Def :: AssociatedConst ( id) | Def :: Macro ( id, ..) |
300
- Def :: Existential ( id) | Def :: AssociatedExistential ( id) | Def :: ForeignTy ( id) => {
301
- Some ( id)
302
- }
304
+ Def :: Def ( _, id) => Some ( id) ,
303
305
304
306
Def :: Local ( ..) |
305
307
Def :: Upvar ( ..) |
@@ -318,47 +320,47 @@ impl<Id> Def<Id> {
318
320
/// Return the `DefId` of this `Def` if it represents a module.
319
321
pub fn mod_def_id ( & self ) -> Option < DefId > {
320
322
match * self {
321
- Def :: Mod ( id) => Some ( id) ,
323
+ Def :: Def ( DefKind :: Mod , id) => Some ( id) ,
322
324
_ => None ,
323
325
}
324
326
}
325
327
326
328
/// A human readable name for the def kind ("function", "module", etc.).
327
329
pub fn kind_name ( & self ) -> & ' static str {
328
330
match * self {
329
- Def :: Fn ( .. ) => "function" ,
330
- Def :: Mod ( .. ) => "module" ,
331
- Def :: Static ( .. ) => "static" ,
332
- Def :: Enum ( .. ) => "enum" ,
333
- Def :: Variant ( .. ) => "variant" ,
334
- Def :: Ctor ( _ , CtorOf :: Variant , CtorKind :: Fn ) => "tuple variant" ,
335
- Def :: Ctor ( _ , CtorOf :: Variant , CtorKind :: Const ) => "unit variant" ,
336
- Def :: Ctor ( _ , CtorOf :: Variant , CtorKind :: Fictive ) => "struct variant" ,
337
- Def :: Struct ( .. ) => "struct" ,
338
- Def :: Ctor ( _ , CtorOf :: Struct , CtorKind :: Fn ) => "tuple struct" ,
339
- Def :: Ctor ( _ , CtorOf :: Struct , CtorKind :: Const ) => "unit struct" ,
340
- Def :: Ctor ( _ , CtorOf :: Struct , CtorKind :: Fictive ) =>
331
+ Def :: Def ( DefKind :: Fn , _ ) => "function" ,
332
+ Def :: Def ( DefKind :: Mod , _ ) => "module" ,
333
+ Def :: Def ( DefKind :: Static , _ ) => "static" ,
334
+ Def :: Def ( DefKind :: Enum , _ ) => "enum" ,
335
+ Def :: Def ( DefKind :: Variant , _ ) => "variant" ,
336
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Fn ) , _ ) => "tuple variant" ,
337
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , _ ) => "unit variant" ,
338
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Fictive ) , _ ) => "struct variant" ,
339
+ Def :: Def ( DefKind :: Struct , _ ) => "struct" ,
340
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Struct , CtorKind :: Fn ) , _ ) => "tuple struct" ,
341
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Struct , CtorKind :: Const ) , _ ) => "unit struct" ,
342
+ Def :: Def ( DefKind :: Ctor ( CtorOf :: Struct , CtorKind :: Fictive ) , _ ) =>
341
343
bug ! ( "impossible struct constructor" ) ,
342
- Def :: Existential ( .. ) => "existential type" ,
343
- Def :: TyAlias ( .. ) => "type alias" ,
344
- Def :: TraitAlias ( .. ) => "trait alias" ,
345
- Def :: AssociatedTy ( .. ) => "associated type" ,
346
- Def :: AssociatedExistential ( .. ) => "associated existential type" ,
344
+ Def :: Def ( DefKind :: Existential , _ ) => "existential type" ,
345
+ Def :: Def ( DefKind :: TyAlias , _ ) => "type alias" ,
346
+ Def :: Def ( DefKind :: TraitAlias , _ ) => "trait alias" ,
347
+ Def :: Def ( DefKind :: AssociatedTy , _ ) => "associated type" ,
348
+ Def :: Def ( DefKind :: AssociatedExistential , _ ) => "associated existential type" ,
347
349
Def :: SelfCtor ( ..) => "self constructor" ,
348
- Def :: Union ( .. ) => "union" ,
349
- Def :: Trait ( .. ) => "trait" ,
350
- Def :: ForeignTy ( .. ) => "foreign type" ,
351
- Def :: Method ( .. ) => "method" ,
352
- Def :: Const ( .. ) => "constant" ,
353
- Def :: AssociatedConst ( .. ) => "associated constant" ,
354
- Def :: TyParam ( .. ) => "type parameter" ,
355
- Def :: ConstParam ( .. ) => "const parameter" ,
350
+ Def :: Def ( DefKind :: Union , _ ) => "union" ,
351
+ Def :: Def ( DefKind :: Trait , _ ) => "trait" ,
352
+ Def :: Def ( DefKind :: ForeignTy , _ ) => "foreign type" ,
353
+ Def :: Def ( DefKind :: Method , _ ) => "method" ,
354
+ Def :: Def ( DefKind :: Const , _ ) => "constant" ,
355
+ Def :: Def ( DefKind :: AssociatedConst , _ ) => "associated constant" ,
356
+ Def :: Def ( DefKind :: TyParam , _ ) => "type parameter" ,
357
+ Def :: Def ( DefKind :: ConstParam , _ ) => "const parameter" ,
356
358
Def :: PrimTy ( ..) => "builtin type" ,
357
359
Def :: Local ( ..) => "local variable" ,
358
360
Def :: Upvar ( ..) => "closure capture" ,
359
361
Def :: Label ( ..) => "label" ,
360
362
Def :: SelfTy ( ..) => "self type" ,
361
- Def :: Macro ( .. , macro_kind ) => macro_kind. descr ( ) ,
363
+ Def :: Def ( DefKind :: Macro ( macro_kind ) , _ ) => macro_kind. descr ( ) ,
362
364
Def :: ToolMod => "tool module" ,
363
365
Def :: NonMacroAttr ( attr_kind) => attr_kind. descr ( ) ,
364
366
Def :: Err => "unresolved item" ,
@@ -368,36 +370,21 @@ impl<Id> Def<Id> {
368
370
/// An English article for the def.
369
371
pub fn article ( & self ) -> & ' static str {
370
372
match * self {
371
- Def :: AssociatedTy ( ..) | Def :: AssociatedConst ( ..) | Def :: AssociatedExistential ( ..) |
372
- Def :: Enum ( ..) | Def :: Existential ( ..) | Def :: Err => "an" ,
373
- Def :: Macro ( .., macro_kind) => macro_kind. article ( ) ,
373
+ Def :: Def ( DefKind :: AssociatedTy , _)
374
+ | Def :: Def ( DefKind :: AssociatedConst , _)
375
+ | Def :: Def ( DefKind :: AssociatedExistential , _)
376
+ | Def :: Def ( DefKind :: Enum , _)
377
+ | Def :: Def ( DefKind :: Existential , _)
378
+ | Def :: Err => "an" ,
379
+ Def :: Def ( DefKind :: Macro ( macro_kind) , _) => macro_kind. article ( ) ,
374
380
_ => "a" ,
375
381
}
376
382
}
377
383
378
384
pub fn map_id < R > ( self , mut map : impl FnMut ( Id ) -> R ) -> Def < R > {
379
385
match self {
380
- Def :: Fn ( id) => Def :: Fn ( id) ,
381
- Def :: Mod ( id) => Def :: Mod ( id) ,
382
- Def :: Static ( id) => Def :: Static ( id) ,
383
- Def :: Enum ( id) => Def :: Enum ( id) ,
384
- Def :: Variant ( id) => Def :: Variant ( id) ,
385
- Def :: Ctor ( a, b, c) => Def :: Ctor ( a, b, c) ,
386
- Def :: Struct ( id) => Def :: Struct ( id) ,
387
- Def :: Existential ( id) => Def :: Existential ( id) ,
388
- Def :: TyAlias ( id) => Def :: TyAlias ( id) ,
389
- Def :: TraitAlias ( id) => Def :: TraitAlias ( id) ,
390
- Def :: AssociatedTy ( id) => Def :: AssociatedTy ( id) ,
391
- Def :: AssociatedExistential ( id) => Def :: AssociatedExistential ( id) ,
386
+ Def :: Def ( kind, id) => Def :: Def ( kind, id) ,
392
387
Def :: SelfCtor ( id) => Def :: SelfCtor ( id) ,
393
- Def :: Union ( id) => Def :: Union ( id) ,
394
- Def :: Trait ( id) => Def :: Trait ( id) ,
395
- Def :: ForeignTy ( id) => Def :: ForeignTy ( id) ,
396
- Def :: Method ( id) => Def :: Method ( id) ,
397
- Def :: Const ( id) => Def :: Const ( id) ,
398
- Def :: AssociatedConst ( id) => Def :: AssociatedConst ( id) ,
399
- Def :: TyParam ( id) => Def :: TyParam ( id) ,
400
- Def :: ConstParam ( id) => Def :: ConstParam ( id) ,
401
388
Def :: PrimTy ( id) => Def :: PrimTy ( id) ,
402
389
Def :: Local ( id) => Def :: Local ( map ( id) ) ,
403
390
Def :: Upvar ( id, index, closure) => Def :: Upvar (
@@ -407,7 +394,6 @@ impl<Id> Def<Id> {
407
394
) ,
408
395
Def :: Label ( id) => Def :: Label ( id) ,
409
396
Def :: SelfTy ( a, b) => Def :: SelfTy ( a, b) ,
410
- Def :: Macro ( id, macro_kind) => Def :: Macro ( id, macro_kind) ,
411
397
Def :: ToolMod => Def :: ToolMod ,
412
398
Def :: NonMacroAttr ( attr_kind) => Def :: NonMacroAttr ( attr_kind) ,
413
399
Def :: Err => Def :: Err ,
0 commit comments