Skip to content

Commit 43ff7c3

Browse files
committed
Add and use resolve_name_in_lexical_scope and
exclude the prelude from `resolve_name(.., allow_private_imports = true)`.
1 parent 7d9f389 commit 43ff7c3

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/librustc_resolve/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
351351
if let Some(sp) = resolver.ast_map.span_if_local(did) {
352352
err.span_note(sp, "constant defined here");
353353
}
354-
if let Success(binding) = resolver.current_module.resolve_name(name, ValueNS, true) {
354+
if let Some(binding) = resolver.current_module
355+
.resolve_name_in_lexical_scope(name, ValueNS) {
355356
if binding.is_import() {
356357
err.span_note(binding.span.unwrap(), "constant imported here");
357358
}
@@ -1575,13 +1576,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15751576
module: Module<'a>,
15761577
name: Name,
15771578
namespace: Namespace,
1578-
allow_private_imports: bool,
1579+
use_lexical_scope: bool,
15791580
record_used: bool)
15801581
-> ResolveResult<&'a NameBinding<'a>> {
15811582
debug!("(resolving name in module) resolving `{}` in `{}`", name, module_to_string(module));
15821583

15831584
build_reduced_graph::populate_module_if_necessary(self, module);
1584-
module.resolve_name(name, namespace, allow_private_imports).and_then(|binding| {
1585+
match use_lexical_scope {
1586+
true => module.resolve_name_in_lexical_scope(name, namespace)
1587+
.map(Success).unwrap_or(Failed(None)),
1588+
false => module.resolve_name(name, namespace, false),
1589+
}.and_then(|binding| {
15851590
if record_used {
15861591
self.record_use(name, namespace, binding);
15871592
}
@@ -3028,7 +3033,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30283033
if name_path.len() == 1 {
30293034
match this.primitive_type_table.primitive_types.get(last_name) {
30303035
Some(_) => None,
3031-
None => this.current_module.resolve_name(*last_name, TypeNS, true).success()
3036+
None => this.current_module.resolve_name_in_lexical_scope(*last_name, TypeNS)
30323037
.and_then(NameBinding::module)
30333038
}
30343039
} else {
@@ -3085,7 +3090,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30853090

30863091
// Look for a method in the current self type's impl module.
30873092
if let Some(module) = get_module(self, path.span, &name_path) {
3088-
if let Success(binding) = module.resolve_name(name, ValueNS, true) {
3093+
if let Some(binding) = module.resolve_name_in_lexical_scope(name, ValueNS) {
30893094
if let Some(Def::Method(did)) = binding.def() {
30903095
if is_static_method(self, did) {
30913096
return StaticMethod(path_names_to_string(&path, 0));

src/librustc_resolve/resolve_imports.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,16 @@ impl<'a> ::ModuleS<'a> {
220220
}
221221
}
222222

223-
self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false))
224-
.unwrap_or(Failed(None))
223+
Failed(None)
224+
}
225+
226+
// Invariant: this may not be called until import resolution is complete.
227+
pub fn resolve_name_in_lexical_scope(&self, name: Name, ns: Namespace)
228+
-> Option<&'a NameBinding<'a>> {
229+
self.resolutions.borrow().get(&(name, ns)).and_then(|resolution| resolution.binding)
230+
.or_else(|| self.prelude.borrow().and_then(|prelude| {
231+
prelude.resolve_name(name, ns, false).success()
232+
}))
225233
}
226234

227235
// Define the name or return the existing binding if there is a collision.

0 commit comments

Comments
 (0)