Skip to content

Commit a3fcab3

Browse files
committed
rustc: factor most DefId-containing variants out of Def and into DefKind.
1 parent 3af1bdc commit a3fcab3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+737
-611
lines changed

src/librustc/hir/def.rs

+76-90
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use std::fmt::Debug;
1111

1212
use 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)]
1616
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.
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,

src/librustc/hir/lowering.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::hir::{self, ParamName};
3737
use crate::hir::HirVec;
3838
use crate::hir::map::{DefKey, DefPathData, Definitions};
3939
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
40-
use crate::hir::def::{Def, PathResolution, PerNS};
40+
use crate::hir::def::{Def, DefKind, PathResolution, PerNS};
4141
use crate::hir::{GenericArg, ConstArg};
4242
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
4343
ELIDED_LIFETIMES_IN_PATHS};
@@ -1500,7 +1500,7 @@ impl<'a> LoweringContext<'a> {
15001500
None,
15011501
P(hir::Path {
15021502
span,
1503-
def: Def::TyParam(DefId::local(def_index)),
1503+
def: Def::Def(DefKind::TyParam, DefId::local(def_index)),
15041504
segments: hir_vec![hir::PathSegment::from_ident(ident)],
15051505
}),
15061506
))
@@ -1870,39 +1870,42 @@ impl<'a> LoweringContext<'a> {
18701870
index: this.def_key(def_id).parent.expect("missing parent"),
18711871
};
18721872
let type_def_id = match resolution.base_def() {
1873-
Def::AssociatedTy(def_id) if i + 2 == proj_start => {
1873+
Def::Def(DefKind::AssociatedTy, def_id) if i + 2 == proj_start => {
18741874
Some(parent_def_id(self, def_id))
18751875
}
1876-
Def::Variant(def_id) if i + 1 == proj_start => {
1876+
Def::Def(DefKind::Variant, def_id) if i + 1 == proj_start => {
18771877
Some(parent_def_id(self, def_id))
18781878
}
1879-
Def::Struct(def_id)
1880-
| Def::Union(def_id)
1881-
| Def::Enum(def_id)
1882-
| Def::TyAlias(def_id)
1883-
| Def::Trait(def_id) if i + 1 == proj_start =>
1879+
Def::Def(DefKind::Struct, def_id)
1880+
| Def::Def(DefKind::Union, def_id)
1881+
| Def::Def(DefKind::Enum, def_id)
1882+
| Def::Def(DefKind::TyAlias, def_id)
1883+
| Def::Def(DefKind::Trait, def_id) if i + 1 == proj_start =>
18841884
{
18851885
Some(def_id)
18861886
}
18871887
_ => None,
18881888
};
18891889
let parenthesized_generic_args = match resolution.base_def() {
18901890
// `a::b::Trait(Args)`
1891-
Def::Trait(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
1891+
Def::Def(DefKind::Trait, _)
1892+
if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
18921893
// `a::b::Trait(Args)::TraitItem`
1893-
Def::Method(..) | Def::AssociatedConst(..) | Def::AssociatedTy(..)
1894+
Def::Def(DefKind::Method, _)
1895+
| Def::Def(DefKind::AssociatedConst, _)
1896+
| Def::Def(DefKind::AssociatedTy, _)
18941897
if i + 2 == proj_start =>
18951898
{
18961899
ParenthesizedGenericArgs::Ok
18971900
}
18981901
// Avoid duplicated errors.
18991902
Def::Err => ParenthesizedGenericArgs::Ok,
19001903
// An error
1901-
Def::Struct(..)
1902-
| Def::Enum(..)
1903-
| Def::Union(..)
1904-
| Def::TyAlias(..)
1905-
| Def::Variant(..) if i + 1 == proj_start =>
1904+
Def::Def(DefKind::Struct, _)
1905+
| Def::Def(DefKind::Enum, _)
1906+
| Def::Def(DefKind::Union, _)
1907+
| Def::Def(DefKind::TyAlias, _)
1908+
| Def::Def(DefKind::Variant, _) if i + 1 == proj_start =>
19061909
{
19071910
ParenthesizedGenericArgs::Err
19081911
}
@@ -2788,7 +2791,7 @@ impl<'a> LoweringContext<'a> {
27882791
if path.segments.len() == 1
27892792
&& bound_pred.bound_generic_params.is_empty() =>
27902793
{
2791-
if let Some(Def::TyParam(def_id)) = self.resolver
2794+
if let Some(Def::Def(DefKind::TyParam, def_id)) = self.resolver
27922795
.get_resolution(bound_pred.bounded_ty.id)
27932796
.map(|d| d.base_def())
27942797
{
@@ -3242,7 +3245,7 @@ impl<'a> LoweringContext<'a> {
32423245
});
32433246

32443247
if let Some(ref trait_ref) = trait_ref {
3245-
if let Def::Trait(def_id) = trait_ref.path.def {
3248+
if let Def::Def(DefKind::Trait, def_id) = trait_ref.path.def {
32463249
this.trait_impls.entry(def_id).or_default().push(
32473250
lowered_trait_impl_id);
32483251
}
@@ -5277,7 +5280,7 @@ impl<'a> LoweringContext<'a> {
52775280
hir::QPath::Resolved(None, path) => {
52785281
// Turn trait object paths into `TyKind::TraitObject` instead.
52795282
match path.def {
5280-
Def::Trait(_) | Def::TraitAlias(_) => {
5283+
Def::Def(DefKind::Trait, _) | Def::Def(DefKind::TraitAlias, _) => {
52815284
let principal = hir::PolyTraitRef {
52825285
bound_generic_params: hir::HirVec::new(),
52835286
trait_ref: hir::TraitRef {

0 commit comments

Comments
 (0)