Refactor path resoloution.
This commit is contained in:
parent
cb9f14e987
commit
af2d89c7f6
4 changed files with 288 additions and 547 deletions
|
@ -77,7 +77,7 @@ pub struct LoweringContext<'a> {
|
||||||
|
|
||||||
pub trait Resolver {
|
pub trait Resolver {
|
||||||
// Resolve a global hir path generated by the lowerer when expanding `for`, `if let`, etc.
|
// 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
|
// Obtain the resolution for a node id
|
||||||
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
|
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
|
||||||
|
@ -2091,7 +2091,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
segments: segments.into(),
|
segments: segments.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.resolver.resolve_generated_global_path(&mut path, is_value);
|
self.resolver.resolve_hir_path(&mut path, is_value);
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -12,11 +12,10 @@ use self::ImportDirectiveSubclass::*;
|
||||||
|
|
||||||
use {Module, PerNS};
|
use {Module, PerNS};
|
||||||
use Namespace::{self, TypeNS, MacroNS};
|
use Namespace::{self, TypeNS, MacroNS};
|
||||||
use {NameBinding, NameBindingKind, PrivacyError, ToNameBinding};
|
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError, ToNameBinding};
|
||||||
use ResolveResult;
|
use ResolveResult;
|
||||||
use ResolveResult::*;
|
use ResolveResult::*;
|
||||||
use Resolver;
|
use Resolver;
|
||||||
use UseLexicalScopeFlag::DontUseLexicalScope;
|
|
||||||
use {names_to_string, module_to_string};
|
use {names_to_string, module_to_string};
|
||||||
use {resolve_error, ResolutionError};
|
use {resolve_error, ResolutionError};
|
||||||
|
|
||||||
|
@ -27,6 +26,7 @@ use rustc::hir::def::*;
|
||||||
use syntax::ast::{Ident, NodeId, Name};
|
use syntax::ast::{Ident, NodeId, Name};
|
||||||
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
|
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
|
||||||
use syntax::ext::hygiene::Mark;
|
use syntax::ext::hygiene::Mark;
|
||||||
|
use syntax::symbol::keywords;
|
||||||
use syntax::util::lev_distance::find_best_match_for_name;
|
use syntax::util::lev_distance::find_best_match_for_name;
|
||||||
use syntax_pos::Span;
|
use syntax_pos::Span;
|
||||||
|
|
||||||
|
@ -482,14 +482,13 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
|
||||||
// For better failure detection, pretend that the import will not define any names
|
// For better failure detection, pretend that the import will not define any names
|
||||||
// while resolving its module path.
|
// while resolving its module path.
|
||||||
directive.vis.set(ty::Visibility::PrivateExternal);
|
directive.vis.set(ty::Visibility::PrivateExternal);
|
||||||
let result =
|
let result = self.resolve_path(&directive.module_path, PathScope::Import, None, None);
|
||||||
self.resolve_module_path(&directive.module_path, DontUseLexicalScope, None);
|
|
||||||
directive.vis.set(vis);
|
directive.vis.set(vis);
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Success(module) => module,
|
PathResult::Module(module) => module,
|
||||||
Indeterminate => return Indeterminate,
|
PathResult::Indeterminate => return Indeterminate,
|
||||||
Failed(err) => return Failed(err),
|
_ => return Failed(None),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -551,21 +550,20 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
|
||||||
self.current_module = directive.parent;
|
self.current_module = directive.parent;
|
||||||
|
|
||||||
let ImportDirective { ref module_path, span, .. } = *directive;
|
let ImportDirective { ref module_path, span, .. } = *directive;
|
||||||
let module_result = self.resolve_module_path(&module_path, DontUseLexicalScope, Some(span));
|
let module_result = self.resolve_path(&module_path, PathScope::Import, None, Some(span));
|
||||||
let module = match module_result {
|
let module = match module_result {
|
||||||
Success(module) => module,
|
PathResult::Module(module) => module,
|
||||||
Indeterminate => return Indeterminate,
|
PathResult::NonModule(..) => return Success(()),
|
||||||
Failed(err) => {
|
PathResult::Indeterminate => return Indeterminate,
|
||||||
let self_module = self.module_map[&self.current_module.normal_ancestor_id.unwrap()];
|
PathResult::Failed(msg, _) => {
|
||||||
|
let mut path = vec![keywords::SelfValue.ident()];
|
||||||
let resolve_from_self_result = self.resolve_module_path_from_root(
|
path.extend(module_path);
|
||||||
&self_module, &module_path, 0, Some(span));
|
let result = self.resolve_path(&path, PathScope::Import, None, None);
|
||||||
|
return if let PathResult::Module(..) = result {
|
||||||
return if let Success(_) = resolve_from_self_result {
|
|
||||||
let msg = format!("Did you mean `self::{}`?", &names_to_string(module_path));
|
let msg = format!("Did you mean `self::{}`?", &names_to_string(module_path));
|
||||||
Failed(Some((span, msg)))
|
Failed(Some((span, msg)))
|
||||||
} else {
|
} else {
|
||||||
Failed(err)
|
Failed(Some((span, msg)))
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,6 +10,6 @@
|
||||||
|
|
||||||
// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc.
|
// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc.
|
||||||
|
|
||||||
use foo::{}; //~ ERROR failed to resolve. foo
|
use foo::{}; //~ ERROR failed to resolve. Maybe a missing `extern crate foo;`?
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue