Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: refactor path resolution #38014

Merged
merged 6 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ pub struct LoweringContext<'a> {

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

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

// Record the resolution of a path or binding generated by the lowerer when expanding.
fn record_resolution(&mut self, id: NodeId, def: Def);

// 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.
fn definitions(&mut self) -> &mut Definitions;
Expand Down Expand Up @@ -351,12 +348,7 @@ impl<'a> LoweringContext<'a> {
// Otherwise, the base path is an implicit `Self` type path,
// e.g. `Vec` in `Vec::new` or `<I as Iterator>::Item` in
// `<I as Iterator>::Item::default`.
let ty = self.ty(p.span, hir::TyPath(hir::QPath::Resolved(qself, path)));

// Associate that innermost path type with the base Def.
self.resolver.record_resolution(ty.id, resolution.base_def);

ty
self.ty(p.span, hir::TyPath(hir::QPath::Resolved(qself, path)))
};

// Anything after the base path are associated "extensions",
Expand Down Expand Up @@ -1902,10 +1894,8 @@ impl<'a> LoweringContext<'a> {
def: def,
segments: hir_vec![hir::PathSegment::from_name(id)],
})));
let expr = self.expr(span, expr_path, ThinVec::new());
self.resolver.record_resolution(expr.id, def);

expr
self.expr(span, expr_path, ThinVec::new())
}

fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
Expand All @@ -1918,10 +1908,7 @@ impl<'a> LoweringContext<'a> {
attrs: ThinVec<Attribute>)
-> hir::Expr {
let path = self.std_path(span, components, true);
let def = path.def;
let expr = self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs);
self.resolver.record_resolution(expr.id, def);
expr
self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs)
}

fn expr_match(&mut self,
Expand All @@ -1948,11 +1935,8 @@ impl<'a> LoweringContext<'a> {
e: Option<P<hir::Expr>>,
attrs: ThinVec<Attribute>) -> hir::Expr {
let path = self.std_path(span, components, false);
let def = path.def;
let qpath = hir::QPath::Resolved(None, P(path));
let expr = self.expr(span, hir::ExprStruct(qpath, fields, e), attrs);
self.resolver.record_resolution(expr.id, def);
expr
self.expr(span, hir::ExprStruct(qpath, fields, e), attrs)
}

fn expr(&mut self, span: Span, node: hir::Expr_, attrs: ThinVec<Attribute>) -> hir::Expr {
Expand Down Expand Up @@ -2021,16 +2005,13 @@ impl<'a> LoweringContext<'a> {
subpats: hir::HirVec<P<hir::Pat>>)
-> P<hir::Pat> {
let path = self.std_path(span, components, true);
let def = path.def;
let qpath = hir::QPath::Resolved(None, P(path));
let pt = if subpats.is_empty() {
hir::PatKind::Path(qpath)
} else {
hir::PatKind::TupleStruct(qpath, subpats, None)
};
let pat = self.pat(span, pt);
self.resolver.record_resolution(pat.id, def);
pat
self.pat(span, pt)
}

fn pat_ident(&mut self, span: Span, name: Name) -> P<hir::Pat> {
Expand All @@ -2047,7 +2028,6 @@ impl<'a> LoweringContext<'a> {
let def_index = defs.create_def_with_parent(parent_def, id, def_path_data);
DefId::local(def_index)
};
self.resolver.record_resolution(id, Def::Local(def_id));

P(hir::Pat {
id: id,
Expand Down Expand Up @@ -2091,7 +2071,7 @@ impl<'a> LoweringContext<'a> {
segments: segments.into(),
};

self.resolver.resolve_generated_global_path(&mut path, is_value);
self.resolver.resolve_hir_path(&mut path, is_value);
path
}

Expand Down
3 changes: 0 additions & 3 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use rustc::hir;
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
use rustc::hir::def::DefMap;
use rustc::hir::lowering::lower_crate;
use rustc_data_structures::blake2b::Blake2bHasher;
use rustc_data_structures::fmt_wrap::FmtWrap;
Expand Down Expand Up @@ -63,7 +62,6 @@ use derive_registrar;

#[derive(Clone)]
pub struct Resolutions {
pub def_map: DefMap,
pub freevars: FreevarMap,
pub trait_map: TraitMap,
pub maybe_unused_trait_imports: NodeSet,
Expand Down Expand Up @@ -794,7 +792,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
hir_ty_to_ty: NodeMap(),
},
resolutions: Resolutions {
def_map: resolver.def_map,
freevars: resolver.freevars,
trait_map: resolver.trait_map,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
use Namespace::{self, TypeNS, ValueNS, MacroNS};
use ResolveResult::Success;
use {resolve_error, resolve_struct_error, ResolutionError};

use rustc::middle::cstore::LoadedMacro;
Expand Down Expand Up @@ -583,7 +582,7 @@ impl<'b> Resolver<'b> {
} else {
for (name, span) in legacy_imports.imports {
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
if let Success(binding) = result {
if let Ok(binding) = result {
self.legacy_import_macro(name, binding, span, allow_shadowing);
} else {
span_err!(self.session, span, E0469, "imported macro not found");
Expand All @@ -595,7 +594,7 @@ impl<'b> Resolver<'b> {
self.used_crates.insert(krate);
self.session.cstore.export_macros(krate);
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
if let Success(binding) = result {
if let Ok(binding) = result {
self.macro_exports.push(Export { name: name, def: binding.def() });
} else {
span_err!(self.session, span, E0470, "reexported macro not found");
Expand Down
Loading