Skip to content

Commit

Permalink
Rename PathResolution to PartialRes
Browse files Browse the repository at this point in the history
Don't use `PartialRes` when `Res` is enough
  • Loading branch information
petrochenkov committed May 4, 2019
1 parent d0a6ddf commit 917a0fb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 130 deletions.
29 changes: 13 additions & 16 deletions 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;
Expand Down Expand Up @@ -151,7 +151,9 @@ pub enum Res<Id = hir::HirId> {
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.
Expand All @@ -166,19 +168,21 @@ pub enum Res<Id = hir::HirId> {
/// base_res unresolved_segments = 2
/// ```
#[derive(Copy, Clone, Debug)]
pub struct PathResolution {
pub struct PartialRes {
base_res: Res<NodeId>,
unresolved_segments: usize,
}

impl PathResolution {
pub fn new(res: Res<NodeId>) -> Self {
PathResolution { base_res: res, unresolved_segments: 0 }
impl PartialRes {
#[inline]
pub fn new(base_res: Res<NodeId>) -> Self {
PartialRes { base_res, unresolved_segments: 0 }
}

pub fn with_unresolved_segments(res: Res<NodeId>, 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<NodeId>, mut unresolved_segments: usize) -> Self {
if base_res == Res::Err { unresolved_segments = 0 }
PartialRes { base_res, unresolved_segments }
}

#[inline]
Expand Down Expand Up @@ -269,17 +273,10 @@ impl<T> PerNS<Option<T>> {
}
}

/// Definition mapping
pub type ResMap = NodeMap<PathResolution>;

/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;

/// Map used to track the `use` statements within a scope, matching it with all the items in every
/// namespace.
pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;

#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct Export<Id> {
/// The name of the target.
Expand Down
39 changes: 17 additions & 22 deletions src/librustc/hir/lowering.rs
Expand Up @@ -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::{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};
Expand Down Expand Up @@ -145,11 +145,11 @@ pub trait Resolver {
is_value: bool,
) -> hir::Path;

/// Obtain the resolution for a `NodeId`.
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
/// Obtain resolution for a `NodeId` with a single resolution.
fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes>;

/// Obtain the possible resolutions for the given `use` statement.
fn get_import(&mut self, id: NodeId) -> PerNS<Option<PathResolution>>;
/// Obtain per-namespace resolutions for `use` statement with the given `NoedId`.
fn get_import_res(&mut self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;

/// 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.
Expand Down Expand Up @@ -821,7 +821,7 @@ impl<'a> LoweringContext<'a> {
}

fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
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);
}
Expand All @@ -830,12 +830,7 @@ impl<'a> LoweringContext<'a> {
}

fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
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 {
Expand Down Expand Up @@ -1842,13 +1837,13 @@ impl<'a> LoweringContext<'a> {
let qself_position = qself.as_ref().map(|q| q.position);
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx.reborrow()));

let resolution = self.resolver
.get_resolution(id)
.unwrap_or_else(|| PathResolution::new(Res::Err));
let partial_res = self.resolver
.get_partial_res(id)
.unwrap_or_else(|| PartialRes::new(Res::Err));

let proj_start = p.segments.len() - resolution.unresolved_segments();
let proj_start = p.segments.len() - partial_res.unresolved_segments();
let path = P(hir::Path {
res: self.lower_res(resolution.base_res()),
res: self.lower_res(partial_res.base_res()),
segments: p.segments[..proj_start]
.iter()
.enumerate()
Expand All @@ -1869,7 +1864,7 @@ impl<'a> LoweringContext<'a> {
krate: def_id.krate,
index: this.def_key(def_id).parent.expect("missing parent"),
};
let type_def_id = match resolution.base_res() {
let type_def_id = match partial_res.base_res() {
Res::Def(DefKind::AssociatedTy, def_id) if i + 2 == proj_start => {
Some(parent_def_id(self, def_id))
}
Expand All @@ -1886,7 +1881,7 @@ impl<'a> LoweringContext<'a> {
}
_ => None,
};
let parenthesized_generic_args = match resolution.base_res() {
let parenthesized_generic_args = match partial_res.base_res() {
// `a::b::Trait(Args)`
Res::Def(DefKind::Trait, _)
if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
Expand Down Expand Up @@ -1940,7 +1935,7 @@ impl<'a> LoweringContext<'a> {

// Simple case, either no projections, or only fully-qualified.
// E.g., `std::mem::size_of` or `<I as Iterator>::Item`.
if resolution.unresolved_segments() == 0 {
if partial_res.unresolved_segments() == 0 {
return hir::QPath::Resolved(qself, path);
}

Expand Down Expand Up @@ -2792,7 +2787,7 @@ impl<'a> LoweringContext<'a> {
&& bound_pred.bound_generic_params.is_empty() =>
{
if let Some(Res::Def(DefKind::TyParam, def_id)) = self.resolver
.get_resolution(bound_pred.bounded_ty.id)
.get_partial_res(bound_pred.bounded_ty.id)
.map(|d| d.base_res())
{
if let Some(node_id) =
Expand Down Expand Up @@ -3946,7 +3941,7 @@ impl<'a> LoweringContext<'a> {
let node = match p.node {
PatKind::Wild => hir::PatKind::Wild,
PatKind::Ident(ref binding_mode, ident, ref sub) => {
match self.resolver.get_resolution(p.id).map(|d| d.base_res()) {
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
// `None` can occur in body-less function signatures
res @ None | res @ Some(Res::Local(_)) => {
let canonical_id = match res {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/mod.rs
Expand Up @@ -2142,7 +2142,7 @@ pub enum UseKind {
/// resolve maps each TraitRef's ref_id to its defining trait; that's all
/// that the ref_id is for. Note that ref_id's value is not the NodeId of the
/// trait being referred to but just a unique NodeId that serves as a key
/// within the ResMap.
/// within the resolution map.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct TraitRef {
pub path: Path,
Expand Down

0 comments on commit 917a0fb

Please sign in to comment.