Skip to content

Commit 5d8fd98

Browse files
committed
Auto merge of #60544 - petrochenkov:parder, r=eddyb
Rename `PathResolution` to `PartialRes` Don't use `PartialRes` when `Res` is enough. Rename `Res::kind_name` to `Res::descr` for consistency. Remove `Res::Label`, paths can never resolve to labels. Some further cleanup after #60462 r? @eddyb
2 parents d65e721 + 7da9250 commit 5d8fd98

File tree

13 files changed

+143
-169
lines changed

13 files changed

+143
-169
lines changed

src/librustc/hir/def.rs

+14-21
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;
@@ -142,7 +142,6 @@ pub enum Res<Id = hir::HirId> {
142142
Upvar(Id, // `HirId` of closed over local
143143
usize, // index in the `freevars` list of the closure
144144
ast::NodeId), // expr node that creates the closure
145-
Label(ast::NodeId),
146145

147146
// Macro namespace
148147
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
@@ -151,7 +150,9 @@ pub enum Res<Id = hir::HirId> {
151150
Err,
152151
}
153152

154-
/// The result of resolving a path before lowering to HIR.
153+
/// The result of resolving a path before lowering to HIR,
154+
/// with "module" segments resolved and associated item
155+
/// segments deferred to type checking.
155156
/// `base_res` is the resolution of the resolved part of the
156157
/// path, `unresolved_segments` is the number of unresolved
157158
/// segments.
@@ -166,19 +167,21 @@ pub enum Res<Id = hir::HirId> {
166167
/// base_res unresolved_segments = 2
167168
/// ```
168169
#[derive(Copy, Clone, Debug)]
169-
pub struct PathResolution {
170+
pub struct PartialRes {
170171
base_res: Res<NodeId>,
171172
unresolved_segments: usize,
172173
}
173174

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

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 }
181+
#[inline]
182+
pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
183+
if base_res == Res::Err { unresolved_segments = 0 }
184+
PartialRes { base_res, unresolved_segments }
182185
}
183186

184187
#[inline]
@@ -269,17 +272,10 @@ impl<T> PerNS<Option<T>> {
269272
}
270273
}
271274

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

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-
283279
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
284280
pub struct Export<Id> {
285281
/// The name of the target.
@@ -352,7 +348,6 @@ impl<Id> Res<Id> {
352348

353349
Res::Local(..) |
354350
Res::Upvar(..) |
355-
Res::Label(..) |
356351
Res::PrimTy(..) |
357352
Res::SelfTy(..) |
358353
Res::SelfCtor(..) |
@@ -373,14 +368,13 @@ impl<Id> Res<Id> {
373368
}
374369

375370
/// A human readable name for the res kind ("function", "module", etc.).
376-
pub fn kind_name(&self) -> &'static str {
371+
pub fn descr(&self) -> &'static str {
377372
match *self {
378373
Res::Def(kind, _) => kind.descr(),
379374
Res::SelfCtor(..) => "self constructor",
380375
Res::PrimTy(..) => "builtin type",
381376
Res::Local(..) => "local variable",
382377
Res::Upvar(..) => "closure capture",
383-
Res::Label(..) => "label",
384378
Res::SelfTy(..) => "self type",
385379
Res::ToolMod => "tool module",
386380
Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
@@ -408,7 +402,6 @@ impl<Id> Res<Id> {
408402
index,
409403
closure
410404
),
411-
Res::Label(id) => Res::Label(id),
412405
Res::SelfTy(a, b) => Res::SelfTy(a, b),
413406
Res::ToolMod => Res::ToolMod,
414407
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),

src/librustc/hir/lowering.rs

+21-23
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,14 @@ 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>>>;
153+
154+
/// Obtain resolution for a label with the given `NodeId`.
155+
fn get_label_res(&mut self, id: NodeId) -> Option<NodeId>;
153156

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

823826
fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
824-
self.resolver.get_resolution(id).map_or(Res::Err, |pr| {
827+
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
825828
if pr.unresolved_segments() != 0 {
826829
bug!("path not fully resolved: {:?}", pr);
827830
}
@@ -830,12 +833,7 @@ impl<'a> LoweringContext<'a> {
830833
}
831834

832835
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-
})
836+
self.resolver.get_import_res(id).present_items()
839837
}
840838

841839
fn diagnostic(&self) -> &errors::Handler {
@@ -1251,7 +1249,7 @@ impl<'a> LoweringContext<'a> {
12511249
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
12521250
let target_id = match destination {
12531251
Some((id, _)) => {
1254-
if let Res::Label(loop_id) = self.expect_full_res(id) {
1252+
if let Some(loop_id) = self.resolver.get_label_res(id) {
12551253
Ok(self.lower_node_id(loop_id))
12561254
} else {
12571255
Err(hir::LoopIdError::UnresolvedLabel)
@@ -1842,13 +1840,13 @@ impl<'a> LoweringContext<'a> {
18421840
let qself_position = qself.as_ref().map(|q| q.position);
18431841
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx.reborrow()));
18441842

1845-
let resolution = self.resolver
1846-
.get_resolution(id)
1847-
.unwrap_or_else(|| PathResolution::new(Res::Err));
1843+
let partial_res = self.resolver
1844+
.get_partial_res(id)
1845+
.unwrap_or_else(|| PartialRes::new(Res::Err));
18481846

1849-
let proj_start = p.segments.len() - resolution.unresolved_segments();
1847+
let proj_start = p.segments.len() - partial_res.unresolved_segments();
18501848
let path = P(hir::Path {
1851-
res: self.lower_res(resolution.base_res()),
1849+
res: self.lower_res(partial_res.base_res()),
18521850
segments: p.segments[..proj_start]
18531851
.iter()
18541852
.enumerate()
@@ -1869,7 +1867,7 @@ impl<'a> LoweringContext<'a> {
18691867
krate: def_id.krate,
18701868
index: this.def_key(def_id).parent.expect("missing parent"),
18711869
};
1872-
let type_def_id = match resolution.base_res() {
1870+
let type_def_id = match partial_res.base_res() {
18731871
Res::Def(DefKind::AssociatedTy, def_id) if i + 2 == proj_start => {
18741872
Some(parent_def_id(self, def_id))
18751873
}
@@ -1886,7 +1884,7 @@ impl<'a> LoweringContext<'a> {
18861884
}
18871885
_ => None,
18881886
};
1889-
let parenthesized_generic_args = match resolution.base_res() {
1887+
let parenthesized_generic_args = match partial_res.base_res() {
18901888
// `a::b::Trait(Args)`
18911889
Res::Def(DefKind::Trait, _)
18921890
if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
@@ -1940,7 +1938,7 @@ impl<'a> LoweringContext<'a> {
19401938

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

@@ -2792,7 +2790,7 @@ impl<'a> LoweringContext<'a> {
27922790
&& bound_pred.bound_generic_params.is_empty() =>
27932791
{
27942792
if let Some(Res::Def(DefKind::TyParam, def_id)) = self.resolver
2795-
.get_resolution(bound_pred.bounded_ty.id)
2793+
.get_partial_res(bound_pred.bounded_ty.id)
27962794
.map(|d| d.base_res())
27972795
{
27982796
if let Some(node_id) =
@@ -3946,7 +3944,7 @@ impl<'a> LoweringContext<'a> {
39463944
let node = match p.node {
39473945
PatKind::Wild => hir::PatKind::Wild,
39483946
PatKind::Ident(ref binding_mode, ident, ref sub) => {
3949-
match self.resolver.get_resolution(p.id).map(|d| d.base_res()) {
3947+
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
39503948
// `None` can occur in body-less function signatures
39513949
res @ None | res @ Some(Res::Local(_)) => {
39523950
let canonical_id = match res {

src/librustc/hir/mod.rs

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

src/librustc_mir/hair/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
286286
PatKind::Path(hir::QPath::Resolved(None, ref path))
287287
if path.segments.len() == 1 && path.segments[0].args.is_none() => {
288288
format!("interpreted as {} {} pattern, not new variable",
289-
path.res.article(), path.res.kind_name())
289+
path.res.article(), path.res.descr())
290290
}
291291
_ => format!("pattern `{}` not covered", pattern_string),
292292
};

src/librustc_resolve/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a> Resolver<'a> {
4141
let item_str = path.last().unwrap().ident;
4242
let code = source.error_code(res.is_some());
4343
let (base_msg, fallback_label, base_span) = if let Some(res) = res {
44-
(format!("expected {}, found {} `{}`", expected, res.kind_name(), path_str),
44+
(format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
4545
format!("not a {}", expected),
4646
span)
4747
} else {

0 commit comments

Comments
 (0)