Skip to content

Commit 7f3491c

Browse files
committed
Fix name resolution in lexical scopes
1 parent 8b7c3f2 commit 7f3491c

File tree

2 files changed

+26
-67
lines changed

2 files changed

+26
-67
lines changed

src/librustc_resolve/lib.rs

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13571357
/// On success, returns the resolved module, and the closest *private*
13581358
/// module found to the destination when resolving this path.
13591359
fn resolve_module_path(&mut self,
1360-
module_: Module<'a>,
13611360
module_path: &[Name],
13621361
use_lexical_scope: UseLexicalScopeFlag,
13631362
span: Span)
@@ -1368,10 +1367,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13681367

13691368
debug!("(resolving module path for import) processing `{}` rooted at `{}`",
13701369
names_to_string(module_path),
1371-
module_to_string(&module_));
1370+
module_to_string(self.current_module));
13721371

13731372
// Resolve the module prefix, if any.
1374-
let module_prefix_result = self.resolve_module_prefix(module_, module_path);
1373+
let module_prefix_result = self.resolve_module_prefix(module_path);
13751374

13761375
let search_module;
13771376
let start_index;
@@ -1413,8 +1412,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14131412
// This is not a crate-relative path. We resolve the
14141413
// first component of the path in the current lexical
14151414
// scope and then proceed to resolve below that.
1416-
match self.resolve_item_in_lexical_scope(module_,
1417-
module_path[0],
1415+
match self.resolve_item_in_lexical_scope(module_path[0],
14181416
TypeNS,
14191417
true) {
14201418
Failed(err) => return Failed(err),
@@ -1448,61 +1446,30 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14481446
/// Invariant: This must only be called during main resolution, not during
14491447
/// import resolution.
14501448
fn resolve_item_in_lexical_scope(&mut self,
1451-
module_: Module<'a>,
14521449
name: Name,
14531450
namespace: Namespace,
14541451
record_used: bool)
14551452
-> ResolveResult<&'a NameBinding<'a>> {
1456-
debug!("(resolving item in lexical scope) resolving `{}` in namespace {:?} in `{}`",
1457-
name,
1458-
namespace,
1459-
module_to_string(&module_));
1460-
1461-
// Proceed up the scope chain looking for parent modules.
1462-
let mut search_module = module_;
1463-
loop {
1464-
// Resolve the name in the parent module.
1465-
match self.resolve_name_in_module(search_module, name, namespace, true, record_used) {
1466-
Failed(Some((span, msg))) => {
1467-
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
1468-
}
1469-
Failed(None) => (), // Continue up the search chain.
1470-
Indeterminate => {
1471-
// We couldn't see through the higher scope because of an
1472-
// unresolved import higher up. Bail.
1473-
1474-
debug!("(resolving item in lexical scope) indeterminate higher scope; bailing");
1475-
return Indeterminate;
1476-
}
1477-
Success(binding) => {
1478-
// We found the module.
1479-
debug!("(resolving item in lexical scope) found name in module, done");
1480-
return Success(binding);
1481-
}
1453+
// Check the local set of ribs.
1454+
for i in (0 .. self.get_ribs(namespace).len()).rev() {
1455+
if let Some(_) = self.get_ribs(namespace)[i].bindings.get(&name).cloned() {
1456+
return Failed(None);
14821457
}
14831458

1484-
// Go to the next parent.
1485-
match search_module.parent_link {
1486-
NoParentLink => {
1487-
// No more parents. This module was unresolved.
1488-
debug!("(resolving item in lexical scope) unresolved module: no parent module");
1489-
return Failed(None);
1490-
}
1491-
ModuleParentLink(parent_module_node, _) => {
1492-
if search_module.is_normal() {
1493-
// We stop the search here.
1494-
debug!("(resolving item in lexical scope) unresolved module: not \
1495-
searching through module parents");
1496-
return Failed(None);
1497-
} else {
1498-
search_module = parent_module_node;
1499-
}
1500-
}
1501-
BlockParentLink(parent_module_node, _) => {
1502-
search_module = parent_module_node;
1459+
if let ModuleRibKind(module) = self.get_ribs(namespace)[i].kind {
1460+
if let Success(binding) = self.resolve_name_in_module(module,
1461+
name,
1462+
namespace,
1463+
true,
1464+
record_used) {
1465+
return Success(binding);
15031466
}
1467+
// We can only see through anonymous modules
1468+
if module.def.is_some() { return Failed(None); }
15041469
}
15051470
}
1471+
1472+
Failed(None)
15061473
}
15071474

15081475
/// Returns the nearest normal module parent of the given module.
@@ -1538,9 +1505,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15381505
/// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
15391506
/// (b) some chain of `super::`.
15401507
/// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
1541-
fn resolve_module_prefix(&mut self,
1542-
module_: Module<'a>,
1543-
module_path: &[Name])
1508+
fn resolve_module_prefix(&mut self, module_path: &[Name])
15441509
-> ResolveResult<ModulePrefixResult<'a>> {
15451510
// Start at the current module if we see `self` or `super`, or at the
15461511
// top of the crate otherwise.
@@ -1549,6 +1514,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15491514
"super" => 0,
15501515
_ => return Success(NoPrefixFound),
15511516
};
1517+
let module_ = self.current_module;
15521518
let mut containing_module = self.get_nearest_normal_module_parent_or_self(module_);
15531519

15541520
// Now loop through all the `super`s we find.
@@ -2594,8 +2560,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25942560
name: Name,
25952561
span: Span)
25962562
-> BareIdentifierPatternResolution {
2597-
let module = self.current_module;
2598-
match self.resolve_item_in_lexical_scope(module, name, ValueNS, true) {
2563+
match self.resolve_item_in_lexical_scope(name, ValueNS, true) {
25992564
Success(binding) => {
26002565
debug!("(resolve bare identifier pattern) succeeded in finding {} at {:?}",
26012566
name,
@@ -2754,9 +2719,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27542719
}
27552720

27562721
// Check the items.
2757-
let module = self.current_module;
27582722
let name = identifier.unhygienic_name;
2759-
match self.resolve_item_in_lexical_scope(module, name, namespace, record_used) {
2723+
match self.resolve_item_in_lexical_scope(name, namespace, record_used) {
27602724
Success(binding) => binding.def().map(LocalDef::from_def),
27612725
Failed(Some((span, msg))) => {
27622726
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
@@ -2869,8 +2833,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28692833
.collect::<Vec<_>>();
28702834

28712835
let containing_module;
2872-
let current_module = self.current_module;
2873-
match self.resolve_module_path(current_module, &module_path, UseLexicalScope, span) {
2836+
match self.resolve_module_path(&module_path, UseLexicalScope, span) {
28742837
Failed(err) => {
28752838
let (span, msg) = match err {
28762839
Some((span, msg)) => (span, msg),
@@ -3024,7 +2987,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30242987
span: Span,
30252988
name_path: &[ast::Name])
30262989
-> Option<Module<'a>> {
3027-
let root = this.current_module;
30282990
let last_name = name_path.last().unwrap();
30292991

30302992
if name_path.len() == 1 {
@@ -3034,7 +2996,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30342996
.and_then(NameBinding::module)
30352997
}
30362998
} else {
3037-
this.resolve_module_path(root, &name_path, UseLexicalScope, span).success()
2999+
this.resolve_module_path(&name_path, UseLexicalScope, span).success()
30383000
}
30393001
}
30403002

@@ -3274,10 +3236,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32743236
let name_path = path.segments.iter()
32753237
.map(|seg| seg.identifier.name)
32763238
.collect::<Vec<_>>();
3277-
let current_module = self.current_module;
32783239

3279-
match self.resolve_module_path(current_module,
3280-
&name_path[..],
3240+
match self.resolve_module_path(&name_path[..],
32813241
UseLexicalScope,
32823242
expr.span) {
32833243
Success(_) => {

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
427427
module_to_string(&module_));
428428

429429
self.resolver
430-
.resolve_module_path(module_,
431-
&import_directive.module_path,
430+
.resolve_module_path(&import_directive.module_path,
432431
UseLexicalScopeFlag::DontUseLexicalScope,
433432
import_directive.span)
434433
.and_then(|containing_module| {

0 commit comments

Comments
 (0)