Skip to content
Permalink
Browse files

rustc: factor most DefId-containing variants out of Def and into DefK…

…ind.
  • Loading branch information...
eddyb committed Apr 20, 2019
1 parent 3af1bdc commit a3fcab36d25317ff2bad24d67f7e932c37f35d19
Showing with 737 additions and 611 deletions.
  1. +76 −90 src/librustc/hir/def.rs
  2. +22 −19 src/librustc/hir/lowering.rs
  3. +42 −51 src/librustc/hir/map/mod.rs
  4. +7 −4 src/librustc/hir/mod.rs
  5. +4 −4 src/librustc/hir/pat_util.rs
  6. +6 −4 src/librustc/middle/dead.rs
  7. +9 −5 src/librustc/middle/expr_use_visitor.rs
  8. +2 −2 src/librustc/middle/intrinsicck.rs
  9. +14 −8 src/librustc/middle/mem_categorization.rs
  10. +3 −3 src/librustc/middle/reachable.rs
  11. +15 −10 src/librustc/middle/resolve_lifetime.rs
  12. +5 −5 src/librustc/middle/stability.rs
  13. +2 −2 src/librustc/ty/context.rs
  14. +17 −15 src/librustc/ty/mod.rs
  15. +3 −3 src/librustc_lint/builtin.rs
  16. +2 −2 src/librustc_lint/nonstandard_style.rs
  17. +2 −2 src/librustc_lint/unused.rs
  18. +35 −29 src/librustc_metadata/decoder.rs
  19. +2 −1 src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
  20. +2 −2 src/librustc_mir/const_eval.rs
  21. +22 −18 src/librustc_mir/hair/cx/expr.rs
  22. +13 −8 src/librustc_mir/hair/pattern/mod.rs
  23. +4 −2 src/librustc_mir/interpret/eval_context.rs
  24. +2 −2 src/librustc_mir/transform/const_prop.rs
  25. +8 −5 src/librustc_mir/util/pretty.rs
  26. +11 −8 src/librustc_passes/rvalue_promotion.rs
  27. +7 −5 src/librustc_privacy/lib.rs
  28. +50 −37 src/librustc_resolve/build_reduced_graph.rs
  29. +22 −16 src/librustc_resolve/diagnostics.rs
  30. +100 −61 src/librustc_resolve/lib.rs
  31. +8 −8 src/librustc_resolve/macros.rs
  32. +2 −2 src/librustc_resolve/resolve_imports.rs
  33. +8 −8 src/librustc_save_analysis/dump_visitor.rs
  34. +24 −24 src/librustc_save_analysis/lib.rs
  35. +4 −2 src/librustc_save_analysis/sig.rs
  36. +28 −24 src/librustc_typeck/astconv.rs
  37. +9 −9 src/librustc_typeck/check/_match.rs
  38. +2 −1 src/librustc_typeck/check/compare_method.rs
  39. +2 −2 src/librustc_typeck/check/demand.rs
  40. +5 −2 src/librustc_typeck/check/method/mod.rs
  41. +2 −2 src/librustc_typeck/check/method/probe.rs
  42. +5 −5 src/librustc_typeck/check/method/suggest.rs
  43. +16 −10 src/librustc_typeck/check/mod.rs
  44. +7 −7 src/librustc_typeck/collect.rs
  45. +15 −15 src/librustdoc/clean/inline.rs
  46. +57 −37 src/librustdoc/clean/mod.rs
  47. +21 −18 src/librustdoc/passes/collect_intra_doc_links.rs
  48. +11 −10 src/librustdoc/visit_ast.rs
  49. +2 −2 src/librustdoc/visit_lib.rs
@@ -11,12 +11,12 @@ use std::fmt::Debug;

use self::Namespace::*;

/// Encodes if a `Def::Ctor` is the constructor of an enum variant or a struct.
/// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
pub enum CtorOf {
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit struct.
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
Struct,
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit variant.
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
Variant,
}

@@ -45,52 +45,62 @@ pub enum NonMacroAttrKind {
}

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
pub enum Def<Id = hir::HirId> {
pub enum DefKind {
// Type namespace
Mod(DefId),
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
Struct(DefId),
Union(DefId),
Enum(DefId),
/// `DefId` refers to the variant itself, `Def::Ctor` refers to its constructor if it exists.
Variant(DefId),
Trait(DefId),
Mod,
/// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
Struct,
Union,
Enum,
/// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
Variant,
Trait,
/// `existential type Foo: Bar;`
Existential(DefId),
Existential,
/// `type Foo = Bar;`
TyAlias(DefId),
ForeignTy(DefId),
TraitAlias(DefId),
AssociatedTy(DefId),
TyAlias,
ForeignTy,
TraitAlias,
AssociatedTy,
/// `existential type Foo: Bar;`
AssociatedExistential(DefId),
AssociatedExistential,
TyParam,

// Value namespace
Fn,
Const,
ConstParam,
Static,
/// Refers to the struct or enum variant's constructor.
Ctor(CtorOf, CtorKind),
Method,
AssociatedConst,

// Macro namespace
Macro(MacroKind),
}

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
pub enum Def<Id = hir::HirId> {
Def(DefKind, DefId),

// Type namespace
PrimTy(hir::PrimTy),
TyParam(DefId),
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`

// Value namespace
Fn(DefId),
Const(DefId),
ConstParam(DefId),
Static(DefId),
/// `DefId` refers to the struct or enum variant's constructor.
Ctor(DefId, CtorOf, CtorKind),
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
Method(DefId),
AssociatedConst(DefId),

Local(Id),
Upvar(Id, // `HirId` of closed over local
usize, // index in the `freevars` list of the closure
ast::NodeId), // expr node that creates the closure
Label(ast::NodeId),

// Macro namespace
Macro(DefId, MacroKind),
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`

// Both namespaces
// All namespaces
Err,
}

@@ -291,15 +301,7 @@ impl<Id> Def<Id> {
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
pub fn opt_def_id(&self) -> Option<DefId> {
match *self {
Def::Fn(id) | Def::Mod(id) | Def::Static(id) |
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
Def::TyAlias(id) | Def::TraitAlias(id) |
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
Def::AssociatedConst(id) | Def::Macro(id, ..) |
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
Some(id)
}
Def::Def(_, id) => Some(id),

Def::Local(..) |
Def::Upvar(..) |
@@ -318,47 +320,47 @@ impl<Id> Def<Id> {
/// Return the `DefId` of this `Def` if it represents a module.
pub fn mod_def_id(&self) -> Option<DefId> {
match *self {
Def::Mod(id) => Some(id),
Def::Def(DefKind::Mod, id) => Some(id),
_ => None,
}
}

/// A human readable name for the def kind ("function", "module", etc.).
pub fn kind_name(&self) -> &'static str {
match *self {
Def::Fn(..) => "function",
Def::Mod(..) => "module",
Def::Static(..) => "static",
Def::Enum(..) => "enum",
Def::Variant(..) => "variant",
Def::Ctor(_, CtorOf::Variant, CtorKind::Fn) => "tuple variant",
Def::Ctor(_, CtorOf::Variant, CtorKind::Const) => "unit variant",
Def::Ctor(_, CtorOf::Variant, CtorKind::Fictive) => "struct variant",
Def::Struct(..) => "struct",
Def::Ctor(_, CtorOf::Struct, CtorKind::Fn) => "tuple struct",
Def::Ctor(_, CtorOf::Struct, CtorKind::Const) => "unit struct",
Def::Ctor(_, CtorOf::Struct, CtorKind::Fictive) =>
Def::Def(DefKind::Fn, _) => "function",
Def::Def(DefKind::Mod, _) => "module",
Def::Def(DefKind::Static, _) => "static",
Def::Def(DefKind::Enum, _) => "enum",
Def::Def(DefKind::Variant, _) => "variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), _) => "tuple variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), _) => "unit variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive), _) => "struct variant",
Def::Def(DefKind::Struct, _) => "struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _) => "tuple struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Const), _) => "unit struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive), _) =>
bug!("impossible struct constructor"),
Def::Existential(..) => "existential type",
Def::TyAlias(..) => "type alias",
Def::TraitAlias(..) => "trait alias",
Def::AssociatedTy(..) => "associated type",
Def::AssociatedExistential(..) => "associated existential type",
Def::Def(DefKind::Existential, _) => "existential type",
Def::Def(DefKind::TyAlias, _) => "type alias",
Def::Def(DefKind::TraitAlias, _) => "trait alias",
Def::Def(DefKind::AssociatedTy, _) => "associated type",
Def::Def(DefKind::AssociatedExistential, _) => "associated existential type",
Def::SelfCtor(..) => "self constructor",
Def::Union(..) => "union",
Def::Trait(..) => "trait",
Def::ForeignTy(..) => "foreign type",
Def::Method(..) => "method",
Def::Const(..) => "constant",
Def::AssociatedConst(..) => "associated constant",
Def::TyParam(..) => "type parameter",
Def::ConstParam(..) => "const parameter",
Def::Def(DefKind::Union, _) => "union",
Def::Def(DefKind::Trait, _) => "trait",
Def::Def(DefKind::ForeignTy, _) => "foreign type",
Def::Def(DefKind::Method, _) => "method",
Def::Def(DefKind::Const, _) => "constant",
Def::Def(DefKind::AssociatedConst, _) => "associated constant",
Def::Def(DefKind::TyParam, _) => "type parameter",
Def::Def(DefKind::ConstParam, _) => "const parameter",
Def::PrimTy(..) => "builtin type",
Def::Local(..) => "local variable",
Def::Upvar(..) => "closure capture",
Def::Label(..) => "label",
Def::SelfTy(..) => "self type",
Def::Macro(.., macro_kind) => macro_kind.descr(),
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.descr(),
Def::ToolMod => "tool module",
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
Def::Err => "unresolved item",
@@ -368,36 +370,21 @@ impl<Id> Def<Id> {
/// An English article for the def.
pub fn article(&self) -> &'static str {
match *self {
Def::AssociatedTy(..) | Def::AssociatedConst(..) | Def::AssociatedExistential(..) |
Def::Enum(..) | Def::Existential(..) | Def::Err => "an",
Def::Macro(.., macro_kind) => macro_kind.article(),
Def::Def(DefKind::AssociatedTy, _)
| Def::Def(DefKind::AssociatedConst, _)
| Def::Def(DefKind::AssociatedExistential, _)
| Def::Def(DefKind::Enum, _)
| Def::Def(DefKind::Existential, _)
| Def::Err => "an",
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.article(),
_ => "a",
}
}

pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Def<R> {
match self {
Def::Fn(id) => Def::Fn(id),
Def::Mod(id) => Def::Mod(id),
Def::Static(id) => Def::Static(id),
Def::Enum(id) => Def::Enum(id),
Def::Variant(id) => Def::Variant(id),
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
Def::Struct(id) => Def::Struct(id),
Def::Existential(id) => Def::Existential(id),
Def::TyAlias(id) => Def::TyAlias(id),
Def::TraitAlias(id) => Def::TraitAlias(id),
Def::AssociatedTy(id) => Def::AssociatedTy(id),
Def::AssociatedExistential(id) => Def::AssociatedExistential(id),
Def::Def(kind, id) => Def::Def(kind, id),
Def::SelfCtor(id) => Def::SelfCtor(id),
Def::Union(id) => Def::Union(id),
Def::Trait(id) => Def::Trait(id),
Def::ForeignTy(id) => Def::ForeignTy(id),
Def::Method(id) => Def::Method(id),
Def::Const(id) => Def::Const(id),
Def::AssociatedConst(id) => Def::AssociatedConst(id),
Def::TyParam(id) => Def::TyParam(id),
Def::ConstParam(id) => Def::ConstParam(id),
Def::PrimTy(id) => Def::PrimTy(id),
Def::Local(id) => Def::Local(map(id)),
Def::Upvar(id, index, closure) => Def::Upvar(
@@ -407,7 +394,6 @@ impl<Id> Def<Id> {
),
Def::Label(id) => Def::Label(id),
Def::SelfTy(a, b) => Def::SelfTy(a, b),
Def::Macro(id, macro_kind) => Def::Macro(id, macro_kind),
Def::ToolMod => Def::ToolMod,
Def::NonMacroAttr(attr_kind) => Def::NonMacroAttr(attr_kind),
Def::Err => Def::Err,
@@ -37,7 +37,7 @@ use crate::hir::{self, ParamName};
use crate::hir::HirVec;
use crate::hir::map::{DefKey, DefPathData, Definitions};
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
use crate::hir::def::{Def, PathResolution, PerNS};
use crate::hir::def::{Def, DefKind, PathResolution, PerNS};
use crate::hir::{GenericArg, ConstArg};
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
ELIDED_LIFETIMES_IN_PATHS};
@@ -1500,7 +1500,7 @@ impl<'a> LoweringContext<'a> {
None,
P(hir::Path {
span,
def: Def::TyParam(DefId::local(def_index)),
def: Def::Def(DefKind::TyParam, DefId::local(def_index)),
segments: hir_vec![hir::PathSegment::from_ident(ident)],
}),
))
@@ -1870,39 +1870,42 @@ impl<'a> LoweringContext<'a> {
index: this.def_key(def_id).parent.expect("missing parent"),
};
let type_def_id = match resolution.base_def() {
Def::AssociatedTy(def_id) if i + 2 == proj_start => {
Def::Def(DefKind::AssociatedTy, def_id) if i + 2 == proj_start => {
Some(parent_def_id(self, def_id))
}
Def::Variant(def_id) if i + 1 == proj_start => {
Def::Def(DefKind::Variant, def_id) if i + 1 == proj_start => {
Some(parent_def_id(self, def_id))
}
Def::Struct(def_id)
| Def::Union(def_id)
| Def::Enum(def_id)
| Def::TyAlias(def_id)
| Def::Trait(def_id) if i + 1 == proj_start =>
Def::Def(DefKind::Struct, def_id)
| Def::Def(DefKind::Union, def_id)
| Def::Def(DefKind::Enum, def_id)
| Def::Def(DefKind::TyAlias, def_id)
| Def::Def(DefKind::Trait, def_id) if i + 1 == proj_start =>
{
Some(def_id)
}
_ => None,
};
let parenthesized_generic_args = match resolution.base_def() {
// `a::b::Trait(Args)`
Def::Trait(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
Def::Def(DefKind::Trait, _)
if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
// `a::b::Trait(Args)::TraitItem`
Def::Method(..) | Def::AssociatedConst(..) | Def::AssociatedTy(..)
Def::Def(DefKind::Method, _)
| Def::Def(DefKind::AssociatedConst, _)
| Def::Def(DefKind::AssociatedTy, _)
if i + 2 == proj_start =>
{
ParenthesizedGenericArgs::Ok
}
// Avoid duplicated errors.
Def::Err => ParenthesizedGenericArgs::Ok,
// An error
Def::Struct(..)
| Def::Enum(..)
| Def::Union(..)
| Def::TyAlias(..)
| Def::Variant(..) if i + 1 == proj_start =>
Def::Def(DefKind::Struct, _)
| Def::Def(DefKind::Enum, _)
| Def::Def(DefKind::Union, _)
| Def::Def(DefKind::TyAlias, _)
| Def::Def(DefKind::Variant, _) if i + 1 == proj_start =>
{
ParenthesizedGenericArgs::Err
}
@@ -2788,7 +2791,7 @@ impl<'a> LoweringContext<'a> {
if path.segments.len() == 1
&& bound_pred.bound_generic_params.is_empty() =>
{
if let Some(Def::TyParam(def_id)) = self.resolver
if let Some(Def::Def(DefKind::TyParam, def_id)) = self.resolver
.get_resolution(bound_pred.bounded_ty.id)
.map(|d| d.base_def())
{
@@ -3242,7 +3245,7 @@ impl<'a> LoweringContext<'a> {
});

if let Some(ref trait_ref) = trait_ref {
if let Def::Trait(def_id) = trait_ref.path.def {
if let Def::Def(DefKind::Trait, def_id) = trait_ref.path.def {
this.trait_impls.entry(def_id).or_default().push(
lowered_trait_impl_id);
}
@@ -5277,7 +5280,7 @@ impl<'a> LoweringContext<'a> {
hir::QPath::Resolved(None, path) => {
// Turn trait object paths into `TyKind::TraitObject` instead.
match path.def {
Def::Trait(_) | Def::TraitAlias(_) => {
Def::Def(DefKind::Trait, _) | Def::Def(DefKind::TraitAlias, _) => {
let principal = hir::PolyTraitRef {
bound_generic_params: hir::HirVec::new(),
trait_ref: hir::TraitRef {
Oops, something went wrong.

0 comments on commit a3fcab3

Please sign in to comment.
You can’t perform that action at this time.