Skip to content

Commit 917a0fb

Browse files
committed
Rename PathResolution to PartialRes
Don't use `PartialRes` when `Res` is enough
1 parent d0a6ddf commit 917a0fb

File tree

5 files changed

+103
-130
lines changed

5 files changed

+103
-130
lines changed

src/librustc/hir/def.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hir::def_id::DefId;
2-
use crate::util::nodemap::{NodeMap, DefIdMap};
2+
use crate::util::nodemap::DefIdMap;
33
use syntax::ast;
44
use syntax::ext::base::MacroKind;
55
use syntax::ast::NodeId;
@@ -151,7 +151,9 @@ pub enum Res<Id = hir::HirId> {
151151
Err,
152152
}
153153

154-
/// The result of resolving a path before lowering to HIR.
154+
/// The result of resolving a path before lowering to HIR,
155+
/// with "module" segments resolved and associated item
156+
/// segments deferred to type checking.
155157
/// `base_res` is the resolution of the resolved part of the
156158
/// path, `unresolved_segments` is the number of unresolved
157159
/// segments.
@@ -166,19 +168,21 @@ pub enum Res<Id = hir::HirId> {
166168
/// base_res unresolved_segments = 2
167169
/// ```
168170
#[derive(Copy, Clone, Debug)]
169-
pub struct PathResolution {
171+
pub struct PartialRes {
170172
base_res: Res<NodeId>,
171173
unresolved_segments: usize,
172174
}
173175

174-
impl PathResolution {
175-
pub fn new(res: Res<NodeId>) -> Self {
176-
PathResolution { base_res: res, unresolved_segments: 0 }
176+
impl PartialRes {
177+
#[inline]
178+
pub fn new(base_res: Res<NodeId>) -> Self {
179+
PartialRes { base_res, unresolved_segments: 0 }
177180
}
178181

179-
pub fn with_unresolved_segments(res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
180-
if res == Res::Err { unresolved_segments = 0 }
181-
PathResolution { base_res: res, unresolved_segments: unresolved_segments }
182+
#[inline]
183+
pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
184+
if base_res == Res::Err { unresolved_segments = 0 }
185+
PartialRes { base_res, unresolved_segments }
182186
}
183187

184188
#[inline]
@@ -269,17 +273,10 @@ impl<T> PerNS<Option<T>> {
269273
}
270274
}
271275

272-
/// Definition mapping
273-
pub type ResMap = NodeMap<PathResolution>;
274-
275276
/// This is the replacement export map. It maps a module to all of the exports
276277
/// within.
277278
pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
278279

279-
/// Map used to track the `use` statements within a scope, matching it with all the items in every
280-
/// namespace.
281-
pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
282-
283280
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
284281
pub struct Export<Id> {
285282
/// The name of the target.

src/librustc/hir/lowering.rs

Lines changed: 17 additions & 22 deletions
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::{Res, DefKind, PathResolution, PerNS};
40+
use crate::hir::def::{Res, DefKind, PartialRes, PerNS};
4141
use crate::hir::{GenericArg, ConstArg};
4242
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
4343
ELIDED_LIFETIMES_IN_PATHS};
@@ -145,11 +145,11 @@ pub trait Resolver {
145145
is_value: bool,
146146
) -> hir::Path;
147147

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

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

154154
/// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
155155
/// This should only return `None` during testing.
@@ -821,7 +821,7 @@ impl<'a> LoweringContext<'a> {
821821
}
822822

823823
fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
824-
self.resolver.get_resolution(id).map_or(Res::Err, |pr| {
824+
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
825825
if pr.unresolved_segments() != 0 {
826826
bug!("path not fully resolved: {:?}", pr);
827827
}
@@ -830,12 +830,7 @@ impl<'a> LoweringContext<'a> {
830830
}
831831

832832
fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
833-
self.resolver.get_import(id).present_items().map(|pr| {
834-
if pr.unresolved_segments() != 0 {
835-
bug!("path not fully resolved: {:?}", pr);
836-
}
837-
pr.base_res()
838-
})
833+
self.resolver.get_import_res(id).present_items()
839834
}
840835

841836
fn diagnostic(&self) -> &errors::Handler {
@@ -1842,13 +1837,13 @@ impl<'a> LoweringContext<'a> {
18421837
let qself_position = qself.as_ref().map(|q| q.position);
18431838
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx.reborrow()));
18441839

1845-
let resolution = self.resolver
1846-
.get_resolution(id)
1847-
.unwrap_or_else(|| PathResolution::new(Res::Err));
1840+
let partial_res = self.resolver
1841+
.get_partial_res(id)
1842+
.unwrap_or_else(|| PartialRes::new(Res::Err));
18481843

1849-
let proj_start = p.segments.len() - resolution.unresolved_segments();
1844+
let proj_start = p.segments.len() - partial_res.unresolved_segments();
18501845
let path = P(hir::Path {
1851-
res: self.lower_res(resolution.base_res()),
1846+
res: self.lower_res(partial_res.base_res()),
18521847
segments: p.segments[..proj_start]
18531848
.iter()
18541849
.enumerate()
@@ -1869,7 +1864,7 @@ impl<'a> LoweringContext<'a> {
18691864
krate: def_id.krate,
18701865
index: this.def_key(def_id).parent.expect("missing parent"),
18711866
};
1872-
let type_def_id = match resolution.base_res() {
1867+
let type_def_id = match partial_res.base_res() {
18731868
Res::Def(DefKind::AssociatedTy, def_id) if i + 2 == proj_start => {
18741869
Some(parent_def_id(self, def_id))
18751870
}
@@ -1886,7 +1881,7 @@ impl<'a> LoweringContext<'a> {
18861881
}
18871882
_ => None,
18881883
};
1889-
let parenthesized_generic_args = match resolution.base_res() {
1884+
let parenthesized_generic_args = match partial_res.base_res() {
18901885
// `a::b::Trait(Args)`
18911886
Res::Def(DefKind::Trait, _)
18921887
if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
@@ -1940,7 +1935,7 @@ impl<'a> LoweringContext<'a> {
19401935

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

@@ -2792,7 +2787,7 @@ impl<'a> LoweringContext<'a> {
27922787
&& bound_pred.bound_generic_params.is_empty() =>
27932788
{
27942789
if let Some(Res::Def(DefKind::TyParam, def_id)) = self.resolver
2795-
.get_resolution(bound_pred.bounded_ty.id)
2790+
.get_partial_res(bound_pred.bounded_ty.id)
27962791
.map(|d| d.base_res())
27972792
{
27982793
if let Some(node_id) =
@@ -3946,7 +3941,7 @@ impl<'a> LoweringContext<'a> {
39463941
let node = match p.node {
39473942
PatKind::Wild => hir::PatKind::Wild,
39483943
PatKind::Ident(ref binding_mode, ident, ref sub) => {
3949-
match self.resolver.get_resolution(p.id).map(|d| d.base_res()) {
3944+
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
39503945
// `None` can occur in body-less function signatures
39513946
res @ None | res @ Some(Res::Local(_)) => {
39523947
let canonical_id = match res {

src/librustc/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ pub enum UseKind {
21422142
/// resolve maps each TraitRef's ref_id to its defining trait; that's all
21432143
/// that the ref_id is for. Note that ref_id's value is not the NodeId of the
21442144
/// trait being referred to but just a unique NodeId that serves as a key
2145-
/// within the ResMap.
2145+
/// within the resolution map.
21462146
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
21472147
pub struct TraitRef {
21482148
pub path: Path,

0 commit comments

Comments
 (0)