diff --git a/src/librustc/hir/def.rs b/src/librustc/hir/def.rs index 4af79764a601..6366c1f93e67 100644 --- a/src/librustc/hir/def.rs +++ b/src/librustc/hir/def.rs @@ -11,12 +11,12 @@ 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,41 +45,99 @@ pub enum NonMacroAttrKind { } #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)] -pub enum Def { +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), +} + +impl DefKind { + pub fn descr(self) -> &'static str { + match self { + DefKind::Fn => "function", + DefKind::Mod => "module", + DefKind::Static => "static", + DefKind::Enum => "enum", + DefKind::Variant => "variant", + DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant", + DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant", + DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant", + DefKind::Struct => "struct", + DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct", + DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct", + DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => + bug!("impossible struct constructor"), + DefKind::Existential => "existential type", + DefKind::TyAlias => "type alias", + DefKind::TraitAlias => "trait alias", + DefKind::AssociatedTy => "associated type", + DefKind::AssociatedExistential => "associated existential type", + DefKind::Union => "union", + DefKind::Trait => "trait", + DefKind::ForeignTy => "foreign type", + DefKind::Method => "method", + DefKind::Const => "constant", + DefKind::AssociatedConst => "associated constant", + DefKind::TyParam => "type parameter", + DefKind::ConstParam => "const parameter", + DefKind::Macro(macro_kind) => macro_kind.descr(), + } + } + + /// An English article for the def. + pub fn article(&self) -> &'static str { + match *self { + DefKind::AssociatedTy + | DefKind::AssociatedConst + | DefKind::AssociatedExistential + | DefKind::Enum + | DefKind::Existential => "an", + DefKind::Macro(macro_kind) => macro_kind.article(), + _ => "a", + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)] +pub enum Res { + Def(DefKind, DefId), + + // Type namespace PrimTy(hir::PrimTy), - TyParam(DefId), SelfTy(Option /* trait */, Option /* 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 @@ -87,46 +145,45 @@ pub enum Def { Label(ast::NodeId), // Macro namespace - Macro(DefId, MacroKind), NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]` - // Both namespaces + // All namespaces Err, } /// The result of resolving a path before lowering to HIR. -/// `base_def` is definition of resolved part of the +/// `base_res` is the resolution of the resolved part of the /// path, `unresolved_segments` is the number of unresolved /// segments. /// /// ```text /// module::Type::AssocX::AssocY::MethodOrAssocType /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -/// base_def unresolved_segments = 3 +/// base_res unresolved_segments = 3 /// /// ::AssocX::AssocY::MethodOrAssocType /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~ -/// base_def unresolved_segments = 2 +/// base_res unresolved_segments = 2 /// ``` #[derive(Copy, Clone, Debug)] pub struct PathResolution { - base_def: Def, + base_res: Res, unresolved_segments: usize, } impl PathResolution { - pub fn new(def: Def) -> Self { - PathResolution { base_def: def, unresolved_segments: 0 } + pub fn new(res: Res) -> Self { + PathResolution { base_res: res, unresolved_segments: 0 } } - pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self { - if def == Def::Err { unresolved_segments = 0 } - PathResolution { base_def: def, unresolved_segments: unresolved_segments } + pub fn with_unresolved_segments(res: Res, mut unresolved_segments: usize) -> Self { + if res == Res::Err { unresolved_segments = 0 } + PathResolution { base_res: res, unresolved_segments: unresolved_segments } } #[inline] - pub fn base_def(&self) -> Def { - self.base_def + pub fn base_res(&self) -> Res { + self.base_res } #[inline] @@ -213,7 +270,7 @@ pub fn present_items(self) -> impl Iterator { } /// Definition mapping -pub type DefMap = NodeMap; +pub type ResMap = NodeMap; /// This is the replacement export map. It maps a module to all of the exports /// within. @@ -227,9 +284,9 @@ pub fn present_items(self) -> impl Iterator { pub struct Export { /// The name of the target. pub ident: ast::Ident, - /// The definition of the target. - pub def: Def, - /// The span of the target definition. + /// The resolution of the target. + pub res: Res, + /// The span of the target. pub span: Span, /// The visibility of the export. /// We include non-`pub` exports for hygienic macros that get used from extern crates. @@ -240,7 +297,7 @@ impl Export { pub fn map_id(self, map: impl FnMut(Id) -> R) -> Export { Export { ident: self.ident, - def: self.def.map_id(map), + res: self.res.map_id(map), span: self.span, vis: self.vis, } @@ -277,140 +334,85 @@ pub fn descr(self) -> &'static str { } } -impl Def { +impl Res { /// Return the `DefId` of this `Def` if it has an id, else panic. pub fn def_id(&self) -> DefId where Id: Debug, { self.opt_def_id().unwrap_or_else(|| { - bug!("attempted .def_id() on invalid def: {:?}", self) + bug!("attempted .def_id() on invalid res: {:?}", self) }) } - /// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`. + /// Return `Some(..)` with the `DefId` of this `Res` if it has a id, else `None`. pub fn opt_def_id(&self) -> Option { 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) - } + Res::Def(_, id) => Some(id), - Def::Local(..) | - Def::Upvar(..) | - Def::Label(..) | - Def::PrimTy(..) | - Def::SelfTy(..) | - Def::SelfCtor(..) | - Def::ToolMod | - Def::NonMacroAttr(..) | - Def::Err => { + Res::Local(..) | + Res::Upvar(..) | + Res::Label(..) | + Res::PrimTy(..) | + Res::SelfTy(..) | + Res::SelfCtor(..) | + Res::ToolMod | + Res::NonMacroAttr(..) | + Res::Err => { None } } } - /// Return the `DefId` of this `Def` if it represents a module. + /// Return the `DefId` of this `Res` if it represents a module. pub fn mod_def_id(&self) -> Option { match *self { - Def::Mod(id) => Some(id), + Res::Def(DefKind::Mod, id) => Some(id), _ => None, } } - /// A human readable name for the def kind ("function", "module", etc.). + /// A human readable name for the res 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) => - 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::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::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::ToolMod => "tool module", - Def::NonMacroAttr(attr_kind) => attr_kind.descr(), - Def::Err => "unresolved item", + Res::Def(kind, _) => kind.descr(), + Res::SelfCtor(..) => "self constructor", + Res::PrimTy(..) => "builtin type", + Res::Local(..) => "local variable", + Res::Upvar(..) => "closure capture", + Res::Label(..) => "label", + Res::SelfTy(..) => "self type", + Res::ToolMod => "tool module", + Res::NonMacroAttr(attr_kind) => attr_kind.descr(), + Res::Err => "unresolved item", } } - /// An English article for the def. + /// An English article for the res. 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(), + Res::Def(kind, _) => kind.article(), + Res::Err => "an", _ => "a", } } - pub fn map_id(self, mut map: impl FnMut(Id) -> R) -> Def { + pub fn map_id(self, mut map: impl FnMut(Id) -> R) -> Res { 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::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( + Res::Def(kind, id) => Res::Def(kind, id), + Res::SelfCtor(id) => Res::SelfCtor(id), + Res::PrimTy(id) => Res::PrimTy(id), + Res::Local(id) => Res::Local(map(id)), + Res::Upvar(id, index, closure) => Res::Upvar( map(id), index, closure ), - 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, + Res::Label(id) => Res::Label(id), + Res::SelfTy(a, b) => Res::SelfTy(a, b), + Res::ToolMod => Res::ToolMod, + Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind), + Res::Err => Res::Err, } } } diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index ad26e05bf80d..8361a62c07e4 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -37,7 +37,7 @@ 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::{Res, DefKind, PathResolution, PerNS}; use crate::hir::{GenericArg, ConstArg}; use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES, ELIDED_LIFETIMES_IN_PATHS}; @@ -812,29 +812,29 @@ fn next_id(&mut self) -> hir::HirId { self.lower_node_id(self.sess.next_node_id()) } - fn lower_def(&mut self, def: Def) -> Def { - def.map_id(|id| { + fn lower_res(&mut self, res: Res) -> Res { + res.map_id(|id| { self.lower_node_id_generic(id, |_| { - panic!("expected node_id to be lowered already for def {:#?}", def) + panic!("expected node_id to be lowered already for res {:#?}", res) }) }) } - fn expect_full_def(&mut self, id: NodeId) -> Def { - self.resolver.get_resolution(id).map_or(Def::Err, |pr| { + fn expect_full_res(&mut self, id: NodeId) -> Res { + self.resolver.get_resolution(id).map_or(Res::Err, |pr| { if pr.unresolved_segments() != 0 { bug!("path not fully resolved: {:?}", pr); } - pr.base_def() + pr.base_res() }) } - fn expect_full_def_from_use(&mut self, id: NodeId) -> impl Iterator> { + fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator> { self.resolver.get_import(id).present_items().map(|pr| { if pr.unresolved_segments() != 0 { bug!("path not fully resolved: {:?}", pr); } - pr.base_def() + pr.base_res() }) } @@ -1136,7 +1136,7 @@ fn make_async_expr( output, c_variadic: false }; - // Lower the arguments before the body otherwise the body will call `lower_def` expecting + // Lower the arguments before the body otherwise the body will call `lower_res` expecting // the argument to have been assigned an id already. let arguments = self.lower_args(Some(&decl)); let body_expr = body(self); @@ -1251,7 +1251,7 @@ fn lower_label(&mut self, label: Option