diff --git a/src/librustc/hir/def.rs b/src/librustc/hir/def.rs index 6366c1f93e67..0719eb701a98 100644 --- a/src/librustc/hir/def.rs +++ b/src/librustc/hir/def.rs @@ -1,5 +1,5 @@ use crate::hir::def_id::DefId; -use crate::util::nodemap::{NodeMap, DefIdMap}; +use crate::util::nodemap::DefIdMap; use syntax::ast; use syntax::ext::base::MacroKind; use syntax::ast::NodeId; @@ -142,7 +142,6 @@ pub enum Res { 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 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]` @@ -151,7 +150,9 @@ pub enum Res { Err, } -/// The result of resolving a path before lowering to HIR. +/// The result of resolving a path before lowering to HIR, +/// with "module" segments resolved and associated item +/// segments deferred to type checking. /// `base_res` is the resolution of the resolved part of the /// path, `unresolved_segments` is the number of unresolved /// segments. @@ -166,19 +167,21 @@ pub enum Res { /// base_res unresolved_segments = 2 /// ``` #[derive(Copy, Clone, Debug)] -pub struct PathResolution { +pub struct PartialRes { base_res: Res, unresolved_segments: usize, } -impl PathResolution { - pub fn new(res: Res) -> Self { - PathResolution { base_res: res, unresolved_segments: 0 } +impl PartialRes { + #[inline] + pub fn new(base_res: Res) -> Self { + PartialRes { base_res, unresolved_segments: 0 } } - 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 with_unresolved_segments(base_res: Res, mut unresolved_segments: usize) -> Self { + if base_res == Res::Err { unresolved_segments = 0 } + PartialRes { base_res, unresolved_segments } } #[inline] @@ -269,17 +272,10 @@ pub fn present_items(self) -> impl Iterator { } } -/// Definition mapping -pub type ResMap = NodeMap; - /// This is the replacement export map. It maps a module to all of the exports /// within. pub type ExportMap = DefIdMap>>; -/// Map used to track the `use` statements within a scope, matching it with all the items in every -/// namespace. -pub type ImportMap = NodeMap>>; - #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)] pub struct Export { /// The name of the target. @@ -352,7 +348,6 @@ pub fn opt_def_id(&self) -> Option { Res::Local(..) | Res::Upvar(..) | - Res::Label(..) | Res::PrimTy(..) | Res::SelfTy(..) | Res::SelfCtor(..) | @@ -373,14 +368,13 @@ pub fn mod_def_id(&self) -> Option { } /// A human readable name for the res kind ("function", "module", etc.). - pub fn kind_name(&self) -> &'static str { + pub fn descr(&self) -> &'static str { match *self { 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(), @@ -408,7 +402,6 @@ pub fn map_id(self, mut map: impl FnMut(Id) -> R) -> Res { index, closure ), - 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), diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 0ac9429de139..36e4195c989c 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::{Res, DefKind, PathResolution, PerNS}; +use crate::hir::def::{Res, DefKind, PartialRes, PerNS}; use crate::hir::{GenericArg, ConstArg}; use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES, ELIDED_LIFETIMES_IN_PATHS}; @@ -145,11 +145,14 @@ fn resolve_hir_path( is_value: bool, ) -> hir::Path; - /// Obtain the resolution for a `NodeId`. - fn get_resolution(&mut self, id: NodeId) -> Option; + /// Obtain resolution for a `NodeId` with a single resolution. + fn get_partial_res(&mut self, id: NodeId) -> Option; - /// Obtain the possible resolutions for the given `use` statement. - fn get_import(&mut self, id: NodeId) -> PerNS>; + /// Obtain per-namespace resolutions for `use` statement with the given `NoedId`. + fn get_import_res(&mut self, id: NodeId) -> PerNS>>; + + /// Obtain resolution for a label with the given `NodeId`. + fn get_label_res(&mut self, id: NodeId) -> Option; /// We must keep the set of definitions up to date as we add nodes that weren't in the AST. /// This should only return `None` during testing. @@ -821,7 +824,7 @@ fn lower_res(&mut self, res: Res) -> Res { } fn expect_full_res(&mut self, id: NodeId) -> Res { - self.resolver.get_resolution(id).map_or(Res::Err, |pr| { + self.resolver.get_partial_res(id).map_or(Res::Err, |pr| { if pr.unresolved_segments() != 0 { bug!("path not fully resolved: {:?}", pr); } @@ -830,12 +833,7 @@ fn expect_full_res(&mut self, id: NodeId) -> Res { } 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_res() - }) + self.resolver.get_import_res(id).present_items() } fn diagnostic(&self) -> &errors::Handler { @@ -1251,7 +1249,7 @@ fn lower_label(&mut self, label: Option