Skip to content

Commit 5a02480

Browse files
committed
Auto merge of #38014 - jseyfried:refactor_path_resolution, r=nrc
resolve: refactor path resolution This is a pure refactoring, modulo minor diagnostics improvements. r? @nrc
2 parents 5db4826 + c871637 commit 5a02480

File tree

7 files changed

+446
-821
lines changed

7 files changed

+446
-821
lines changed

src/librustc/hir/lowering.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,11 @@ pub struct LoweringContext<'a> {
8282

8383
pub trait Resolver {
8484
// Resolve a global hir path generated by the lowerer when expanding `for`, `if let`, etc.
85-
fn resolve_generated_global_path(&mut self, path: &mut hir::Path, is_value: bool);
85+
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
8686

8787
// Obtain the resolution for a node id
8888
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
8989

90-
// Record the resolution of a path or binding generated by the lowerer when expanding.
91-
fn record_resolution(&mut self, id: NodeId, def: Def);
92-
9390
// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
9491
// This should only return `None` during testing.
9592
fn definitions(&mut self) -> &mut Definitions;
@@ -372,12 +369,7 @@ impl<'a> LoweringContext<'a> {
372369
// Otherwise, the base path is an implicit `Self` type path,
373370
// e.g. `Vec` in `Vec::new` or `<I as Iterator>::Item` in
374371
// `<I as Iterator>::Item::default`.
375-
let ty = self.ty(p.span, hir::TyPath(hir::QPath::Resolved(qself, path)));
376-
377-
// Associate that innermost path type with the base Def.
378-
self.resolver.record_resolution(ty.id, resolution.base_def);
379-
380-
ty
372+
self.ty(p.span, hir::TyPath(hir::QPath::Resolved(qself, path)))
381373
};
382374

383375
// Anything after the base path are associated "extensions",
@@ -1979,10 +1971,8 @@ impl<'a> LoweringContext<'a> {
19791971
def: def,
19801972
segments: hir_vec![hir::PathSegment::from_name(id)],
19811973
})));
1982-
let expr = self.expr(span, expr_path, ThinVec::new());
1983-
self.resolver.record_resolution(expr.id, def);
19841974

1985-
expr
1975+
self.expr(span, expr_path, ThinVec::new())
19861976
}
19871977

19881978
fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
@@ -1995,10 +1985,7 @@ impl<'a> LoweringContext<'a> {
19951985
attrs: ThinVec<Attribute>)
19961986
-> hir::Expr {
19971987
let path = self.std_path(span, components, true);
1998-
let def = path.def;
1999-
let expr = self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs);
2000-
self.resolver.record_resolution(expr.id, def);
2001-
expr
1988+
self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs)
20021989
}
20031990

20041991
fn expr_match(&mut self,
@@ -2025,11 +2012,8 @@ impl<'a> LoweringContext<'a> {
20252012
e: Option<P<hir::Expr>>,
20262013
attrs: ThinVec<Attribute>) -> hir::Expr {
20272014
let path = self.std_path(span, components, false);
2028-
let def = path.def;
20292015
let qpath = hir::QPath::Resolved(None, P(path));
2030-
let expr = self.expr(span, hir::ExprStruct(qpath, fields, e), attrs);
2031-
self.resolver.record_resolution(expr.id, def);
2032-
expr
2016+
self.expr(span, hir::ExprStruct(qpath, fields, e), attrs)
20332017
}
20342018

20352019
fn expr(&mut self, span: Span, node: hir::Expr_, attrs: ThinVec<Attribute>) -> hir::Expr {
@@ -2098,16 +2082,13 @@ impl<'a> LoweringContext<'a> {
20982082
subpats: hir::HirVec<P<hir::Pat>>)
20992083
-> P<hir::Pat> {
21002084
let path = self.std_path(span, components, true);
2101-
let def = path.def;
21022085
let qpath = hir::QPath::Resolved(None, P(path));
21032086
let pt = if subpats.is_empty() {
21042087
hir::PatKind::Path(qpath)
21052088
} else {
21062089
hir::PatKind::TupleStruct(qpath, subpats, None)
21072090
};
2108-
let pat = self.pat(span, pt);
2109-
self.resolver.record_resolution(pat.id, def);
2110-
pat
2091+
self.pat(span, pt)
21112092
}
21122093

21132094
fn pat_ident(&mut self, span: Span, name: Name) -> P<hir::Pat> {
@@ -2124,7 +2105,6 @@ impl<'a> LoweringContext<'a> {
21242105
let def_index = defs.create_def_with_parent(parent_def, id, def_path_data);
21252106
DefId::local(def_index)
21262107
};
2127-
self.resolver.record_resolution(id, Def::Local(def_id));
21282108

21292109
P(hir::Pat {
21302110
id: id,
@@ -2168,7 +2148,7 @@ impl<'a> LoweringContext<'a> {
21682148
segments: segments.into(),
21692149
};
21702150

2171-
self.resolver.resolve_generated_global_path(&mut path, is_value);
2151+
self.resolver.resolve_hir_path(&mut path, is_value);
21722152
path
21732153
}
21742154

src/librustc_driver/driver.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use rustc::hir;
1212
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
13-
use rustc::hir::def::DefMap;
1413
use rustc::hir::lowering::lower_crate;
1514
use rustc_data_structures::blake2b::Blake2bHasher;
1615
use rustc_data_structures::fmt_wrap::FmtWrap;
@@ -63,7 +62,6 @@ use derive_registrar;
6362

6463
#[derive(Clone)]
6564
pub struct Resolutions {
66-
pub def_map: DefMap,
6765
pub freevars: FreevarMap,
6866
pub trait_map: TraitMap,
6967
pub maybe_unused_trait_imports: NodeSet,
@@ -794,7 +792,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
794792
hir_ty_to_ty: NodeMap(),
795793
},
796794
resolutions: Resolutions {
797-
def_map: resolver.def_map,
798795
freevars: resolver.freevars,
799796
trait_map: resolver.trait_map,
800797
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,

src/librustc_resolve/build_reduced_graph.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
1919
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
2020
use Namespace::{self, TypeNS, ValueNS, MacroNS};
21-
use ResolveResult::Success;
2221
use {resolve_error, resolve_struct_error, ResolutionError};
2322

2423
use rustc::middle::cstore::LoadedMacro;
@@ -583,7 +582,7 @@ impl<'b> Resolver<'b> {
583582
} else {
584583
for (name, span) in legacy_imports.imports {
585584
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
586-
if let Success(binding) = result {
585+
if let Ok(binding) = result {
587586
self.legacy_import_macro(name, binding, span, allow_shadowing);
588587
} else {
589588
span_err!(self.session, span, E0469, "imported macro not found");
@@ -595,7 +594,7 @@ impl<'b> Resolver<'b> {
595594
self.used_crates.insert(krate);
596595
self.session.cstore.export_macros(krate);
597596
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
598-
if let Success(binding) = result {
597+
if let Ok(binding) = result {
599598
self.macro_exports.push(Export { name: name, def: binding.def() });
600599
} else {
601600
span_err!(self.session, span, E0470, "reexported macro not found");

0 commit comments

Comments
 (0)