@@ -11,12 +11,12 @@ use std::fmt::Debug;
1111
1212use self :: Namespace :: * ;
1313
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.
1515#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
1616pub 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.
1818 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.
2020 Variant ,
2121}
2222
@@ -45,52 +45,62 @@ pub enum NonMacroAttrKind {
4545}
4646
4747#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
48- pub enum Def < Id = hir :: HirId > {
48+ pub enum DefKind {
4949 // 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 ,
5858 /// `existential type Foo: Bar;`
59- Existential ( DefId ) ,
59+ Existential ,
6060 /// `type Foo = Bar;`
61- TyAlias ( DefId ) ,
62- ForeignTy ( DefId ) ,
63- TraitAlias ( DefId ) ,
64- AssociatedTy ( DefId ) ,
61+ TyAlias ,
62+ ForeignTy ,
63+ TraitAlias ,
64+ AssociatedTy ,
6565 /// `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
6788 PrimTy ( hir:: PrimTy ) ,
68- TyParam ( DefId ) ,
6989 SelfTy ( Option < DefId > /* trait */ , Option < DefId > /* impl */ ) ,
7090 ToolMod , // e.g., `rustfmt` in `#[rustfmt::skip]`
7191
7292 // 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 ) ,
7993 SelfCtor ( DefId /* impl */ ) , // `DefId` refers to the impl
80- Method ( DefId ) ,
81- AssociatedConst ( DefId ) ,
82-
8394 Local ( Id ) ,
8495 Upvar ( Id , // `HirId` of closed over local
8596 usize , // index in the `freevars` list of the closure
8697 ast:: NodeId ) , // expr node that creates the closure
8798 Label ( ast:: NodeId ) ,
8899
89100 // Macro namespace
90- Macro ( DefId , MacroKind ) ,
91101 NonMacroAttr ( NonMacroAttrKind ) , // e.g., `#[inline]` or `#[rustfmt::skip]`
92102
93- // Both namespaces
103+ // All namespaces
94104 Err ,
95105}
96106
@@ -291,15 +301,7 @@ impl<Id> Def<Id> {
291301 /// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
292302 pub fn opt_def_id ( & self ) -> Option < DefId > {
293303 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) ,
303305
304306 Def :: Local ( ..) |
305307 Def :: Upvar ( ..) |
@@ -318,47 +320,47 @@ impl<Id> Def<Id> {
318320 /// Return the `DefId` of this `Def` if it represents a module.
319321 pub fn mod_def_id ( & self ) -> Option < DefId > {
320322 match * self {
321- Def :: Mod ( id) => Some ( id) ,
323+ Def :: Def ( DefKind :: Mod , id) => Some ( id) ,
322324 _ => None ,
323325 }
324326 }
325327
326328 /// A human readable name for the def kind ("function", "module", etc.).
327329 pub fn kind_name ( & self ) -> & ' static str {
328330 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 ) , _ ) =>
341343 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" ,
347349 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" ,
356358 Def :: PrimTy ( ..) => "builtin type" ,
357359 Def :: Local ( ..) => "local variable" ,
358360 Def :: Upvar ( ..) => "closure capture" ,
359361 Def :: Label ( ..) => "label" ,
360362 Def :: SelfTy ( ..) => "self type" ,
361- Def :: Macro ( .. , macro_kind ) => macro_kind. descr ( ) ,
363+ Def :: Def ( DefKind :: Macro ( macro_kind ) , _ ) => macro_kind. descr ( ) ,
362364 Def :: ToolMod => "tool module" ,
363365 Def :: NonMacroAttr ( attr_kind) => attr_kind. descr ( ) ,
364366 Def :: Err => "unresolved item" ,
@@ -368,36 +370,21 @@ impl<Id> Def<Id> {
368370 /// An English article for the def.
369371 pub fn article ( & self ) -> & ' static str {
370372 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 ( ) ,
374380 _ => "a" ,
375381 }
376382 }
377383
378384 pub fn map_id < R > ( self , mut map : impl FnMut ( Id ) -> R ) -> Def < R > {
379385 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) ,
392387 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) ,
401388 Def :: PrimTy ( id) => Def :: PrimTy ( id) ,
402389 Def :: Local ( id) => Def :: Local ( map ( id) ) ,
403390 Def :: Upvar ( id, index, closure) => Def :: Upvar (
@@ -407,7 +394,6 @@ impl<Id> Def<Id> {
407394 ) ,
408395 Def :: Label ( id) => Def :: Label ( id) ,
409396 Def :: SelfTy ( a, b) => Def :: SelfTy ( a, b) ,
410- Def :: Macro ( id, macro_kind) => Def :: Macro ( id, macro_kind) ,
411397 Def :: ToolMod => Def :: ToolMod ,
412398 Def :: NonMacroAttr ( attr_kind) => Def :: NonMacroAttr ( attr_kind) ,
413399 Def :: Err => Def :: Err ,
0 commit comments