Skip to content

Commit c7bfef4

Browse files
committed
Ignore current scope when resolving self-shadowing imports
That is, for example, import x::y::x, which defines a local x, and thus wouldn't be able to find x::y anymore. Closes issue #624
1 parent eeda0f4 commit c7bfef4

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/comp/middle/resolve.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,10 @@ type scopes = list[scope];
5858

5959
tag import_state {
6060
todo(@ast::view_item, scopes); // only used for explicit imports
61-
6261
resolving(span);
63-
resolved(option::t[def],
64-
65-
/* value */
66-
option::t[def],
67-
68-
/* type */
69-
option::t[def]);
70-
/* module */
71-
62+
resolved(option::t[def], /* value */
63+
option::t[def], /* type */
64+
option::t[def]); /* module */
7265
}
7366

7467
type ext_hash = hashmap[tup(def_id, str, namespace), def];
@@ -94,8 +87,7 @@ tag mod_index_entry {
9487
mie_view_item(@ast::view_item);
9588
mie_item(@ast::item);
9689
mie_native_item(@ast::native_item);
97-
mie_tag_variant(@ast::item, /* tag item */uint);
98-
/* variant index */
90+
mie_tag_variant(@ast::item /* tag item */, uint /* variant index */);
9991

10092
}
10193

@@ -445,24 +437,29 @@ fn add_constr(&@env e, node_id id, &ty::constr_def c) {
445437

446438

447439
// Import resolution
448-
fn resolve_import(&env e, &@ast::view_item it, &scopes sc) {
440+
fn resolve_import(&env e, &@ast::view_item it, scopes sc) {
449441
auto defid;
450442
auto ids;
443+
auto name;
451444
alt (it.node) {
452-
case (ast::view_item_import(_, ?_ids, ?_id)) {
445+
case (ast::view_item_import(?_name, ?_ids, ?_id)) {
453446
defid = local_def(_id);
454447
ids = _ids;
448+
name = _name;
455449
}
456450
}
457451
e.imports.insert(defid._1, resolving(it.span));
458452
auto n_idents = ivec::len(ids);
459453
auto end_id = ids.(n_idents - 1u);
454+
// Ignore the current scope if this import would shadow itself.
455+
if (str::eq(name, ids.(0))) {
456+
sc = std::list::cdr(sc);
457+
}
460458
if (n_idents == 1u) {
461-
auto next_sc = std::list::cdr(sc);
462459
register(e, defid, it.span, end_id,
463-
lookup_in_scope(e, next_sc, it.span, end_id, ns_value),
464-
lookup_in_scope(e, next_sc, it.span, end_id, ns_type),
465-
lookup_in_scope(e, next_sc, it.span, end_id, ns_module));
460+
lookup_in_scope(e, sc, it.span, end_id, ns_value),
461+
lookup_in_scope(e, sc, it.span, end_id, ns_type),
462+
lookup_in_scope(e, sc, it.span, end_id, ns_module));
466463
remove_if_unresolved(e.imports, defid._1);
467464
} else {
468465
auto dcur = alt(lookup_in_scope(e, sc, it.span, ids.(0), ns_module)) {
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod a {
2+
mod b {
3+
mod a {
4+
fn foo() -> int { ret 1; }
5+
}
6+
}
7+
}
8+
9+
mod c {
10+
import a::b::a;
11+
fn bar() { assert a::foo() == 1; }
12+
}
13+
14+
fn main() {
15+
c::bar();
16+
}

0 commit comments

Comments
 (0)