Add and use `resolve_name_in_lexical_scope` and

exclude the prelude from `resolve_name(.., allow_private_imports = true)`.
This commit is contained in:
Jeffrey Seyfried 2016-03-09 04:21:54 +00:00
parent de970b1dff
commit 54cd4d1472
2 changed files with 20 additions and 7 deletions

View File

@ -349,7 +349,8 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
if let Some(sp) = resolver.ast_map.span_if_local(did) { if let Some(sp) = resolver.ast_map.span_if_local(did) {
err.span_note(sp, "constant defined here"); err.span_note(sp, "constant defined here");
} }
if let Success(binding) = resolver.current_module.resolve_name(name, ValueNS, true) { if let Some(binding) = resolver.current_module
.resolve_name_in_lexical_scope(name, ValueNS) {
if binding.is_import() { if binding.is_import() {
err.span_note(binding.span.unwrap(), "constant imported here"); err.span_note(binding.span.unwrap(), "constant imported here");
} }
@ -1536,13 +1537,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
module: Module<'a>, module: Module<'a>,
name: Name, name: Name,
namespace: Namespace, namespace: Namespace,
allow_private_imports: bool, use_lexical_scope: bool,
record_used: bool) record_used: bool)
-> ResolveResult<&'a NameBinding<'a>> { -> ResolveResult<&'a NameBinding<'a>> {
debug!("(resolving name in module) resolving `{}` in `{}`", name, module_to_string(module)); debug!("(resolving name in module) resolving `{}` in `{}`", name, module_to_string(module));
build_reduced_graph::populate_module_if_necessary(self, module); build_reduced_graph::populate_module_if_necessary(self, module);
module.resolve_name(name, namespace, allow_private_imports).and_then(|binding| { match use_lexical_scope {
true => module.resolve_name_in_lexical_scope(name, namespace)
.map(Success).unwrap_or(Failed(None)),
false => module.resolve_name(name, namespace, false),
}.and_then(|binding| {
if record_used { if record_used {
self.record_use(name, namespace, binding); self.record_use(name, namespace, binding);
} }
@ -2961,7 +2966,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if name_path.len() == 1 { if name_path.len() == 1 {
match this.primitive_type_table.primitive_types.get(last_name) { match this.primitive_type_table.primitive_types.get(last_name) {
Some(_) => None, Some(_) => None,
None => this.current_module.resolve_name(*last_name, TypeNS, true).success() None => this.current_module.resolve_name_in_lexical_scope(*last_name, TypeNS)
.and_then(NameBinding::module) .and_then(NameBinding::module)
} }
} else { } else {
@ -3018,7 +3023,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// Look for a method in the current self type's impl module. // Look for a method in the current self type's impl module.
if let Some(module) = get_module(self, path.span, &name_path) { if let Some(module) = get_module(self, path.span, &name_path) {
if let Success(binding) = module.resolve_name(name, ValueNS, true) { if let Some(binding) = module.resolve_name_in_lexical_scope(name, ValueNS) {
if let Some(Def::Method(did)) = binding.def() { if let Some(Def::Method(did)) = binding.def() {
if is_static_method(self, did) { if is_static_method(self, did) {
return StaticMethod(path_names_to_string(&path, 0)); return StaticMethod(path_names_to_string(&path, 0));

View File

@ -239,8 +239,16 @@ impl<'a> ::ModuleS<'a> {
} }
} }
self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false)) Failed(None)
.unwrap_or(Failed(None)) }
// Invariant: this may not be called until import resolution is complete.
pub fn resolve_name_in_lexical_scope(&self, name: Name, ns: Namespace)
-> Option<&'a NameBinding<'a>> {
self.resolutions.borrow().get(&(name, ns)).and_then(|resolution| resolution.binding)
.or_else(|| self.prelude.borrow().and_then(|prelude| {
prelude.resolve_name(name, ns, false).success()
}))
} }
// Define the name or return the existing binding if there is a collision. // Define the name or return the existing binding if there is a collision.