Skip to content

Commit 34643ee

Browse files
cixtorgraydon
authored andcommitted
Fix another import case we got wrong: The local environment should not
interfere with the import statements.
1 parent c5a766f commit 34643ee

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ TEST_XFAILS_RUSTC := $(filter-out \
443443
i8-incr.rs \
444444
import2.rs \
445445
import3.rs \
446+
import4.rs \
446447
item-name-overload.rs \
447448
large-records.rs \
448449
lazy-init.rs \

src/comp/middle/resolve.rs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import front.ast.ident;
33
import front.ast.def;
44
import front.ast.ann;
55
import driver.session;
6+
import util.common.new_def_hash;
67
import util.common.span;
78
import std.map.hashmap;
89
import std.list.list;
@@ -24,6 +25,8 @@ tag scope {
2425
type env = rec(list[scope] scopes,
2526
session.session sess);
2627

28+
type import_map = std.map.hashmap[ast.def_id,def_wrap];
29+
2730
// A simple wrapper over defs that stores a bit more information about modules
2831
// and uses so that we can use the regular lookup_name when resolving imports.
2932
tag def_wrap {
@@ -69,7 +72,7 @@ fn unwrap_def(option.t[def_wrap] d_) -> option.t[def] {
6972
fn find_final_def(&env e, &span sp, vec[ident] idents) -> option.t[def_wrap] {
7073
auto len = _vec.len[ident](idents);
7174
auto first = idents.(0);
72-
auto d_ = lookup_name(e, first);
75+
auto d_ = lookup_name(e, none[import_map], first);
7376
if (len == 1u) {
7477
ret d_;
7578
}
@@ -93,7 +96,8 @@ fn find_final_def(&env e, &span sp, vec[ident] idents) -> option.t[def_wrap] {
9396
fail;
9497
}
9598

96-
fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
99+
fn lookup_name(&env e, option.t[import_map] index,
100+
ast.ident i) -> option.t[def_wrap] {
97101

98102
// log "resolving name " + i;
99103

@@ -137,24 +141,33 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
137141
ret none[def_wrap];
138142
}
139143

140-
fn found_def_view(&env e, @ast.view_item i) -> option.t[def_wrap] {
144+
fn found_def_view(&env e, option.t[import_map] index,
145+
@ast.view_item i) -> option.t[def_wrap] {
141146
alt (i.node) {
142147
case (ast.view_item_use(_, _, ?id)) {
143148
ret some[def_wrap](def_wrap_use(i));
144149
}
145-
case (ast.view_item_import(?idents,_)) {
146-
ret find_final_def(e, i.span, idents);
150+
case (ast.view_item_import(?idents,?d)) {
151+
alt (index) {
152+
case (some[import_map](?idx)) {
153+
ret idx.find(d);
154+
}
155+
case (none[import_map]) {
156+
ret find_final_def(e, i.span, idents);
157+
}
158+
}
147159
}
148160
}
149161
fail;
150162
}
151163

152-
fn check_mod(&env e, ast.ident i, ast._mod m) -> option.t[def_wrap] {
164+
fn check_mod(&env e, option.t[import_map] index, ast.ident i,
165+
ast._mod m) -> option.t[def_wrap] {
153166
alt (m.index.find(i)) {
154167
case (some[ast.mod_index_entry](?ent)) {
155168
alt (ent) {
156169
case (ast.mie_view_item(?ix)) {
157-
ret found_def_view(e, m.view_items.(ix));
170+
ret found_def_view(e, index, m.view_items.(ix));
158171
}
159172
case (ast.mie_item(?ix)) {
160173
ret found_def_item(m.items.(ix));
@@ -180,11 +193,12 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
180193
}
181194

182195

183-
fn in_scope(ast.ident i, env e, &scope s) -> option.t[def_wrap] {
196+
fn in_scope(ast.ident i, env e, option.t[import_map] index,
197+
&scope s) -> option.t[def_wrap] {
184198
alt (s) {
185199

186200
case (scope_crate(?c)) {
187-
ret check_mod(e, i, c.node.module);
201+
ret check_mod(e, index, i, c.node.module);
188202
}
189203

190204
case (scope_item(?it)) {
@@ -218,7 +232,7 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
218232
}
219233
}
220234
case (ast.item_mod(_, ?m, _)) {
221-
ret check_mod(e, i, m);
235+
ret check_mod(e, index, i, m);
222236
}
223237
case (_) { /* fall through */ }
224238
}
@@ -246,13 +260,15 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
246260
ret none[def_wrap];
247261
}
248262

249-
ret std.list.find[scope,def_wrap](e.scopes, bind in_scope(i, e, _));
263+
ret std.list.find[scope,def_wrap](e.scopes,
264+
bind in_scope(i, e, index, _));
250265
}
251266

252-
fn fold_pat_tag(&env e, &span sp, ident i, vec[@ast.pat] args,
253-
option.t[ast.variant_def] old_def, ann a) -> @ast.pat {
267+
fn fold_pat_tag(&env e, &span sp, import_map index, ident i,
268+
vec[@ast.pat] args, option.t[ast.variant_def] old_def,
269+
ann a) -> @ast.pat {
254270
auto new_def;
255-
alt (unwrap_def(lookup_name(e, i))) {
271+
alt (unwrap_def(lookup_name(e, some(index), i))) {
256272
case (some[def](?d)) {
257273
alt (d) {
258274
case (ast.def_variant(?did, ?vid)) {
@@ -273,14 +289,14 @@ fn fold_pat_tag(&env e, &span sp, ident i, vec[@ast.pat] args,
273289
ret @fold.respan[ast.pat_](sp, ast.pat_tag(i, args, new_def, a));
274290
}
275291

276-
fn fold_expr_name(&env e, &span sp, &ast.name n,
277-
&option.t[def] d, ann a) -> @ast.expr {
292+
fn fold_expr_name(&env e, &span sp, import_map index,
293+
&ast.name n, &option.t[def] d, ann a) -> @ast.expr {
278294

279295
if (_vec.len[@ast.ty](n.node.types) > 0u) {
280296
e.sess.unimpl("resolving name expr with ty params");
281297
}
282298

283-
auto d_ = unwrap_def(lookup_name(e, n.node.ident));
299+
auto d_ = unwrap_def(lookup_name(e, some(index), n.node.ident));
284300

285301
alt (d_) {
286302
case (some[def](_)) {
@@ -294,25 +310,27 @@ fn fold_expr_name(&env e, &span sp, &ast.name n,
294310
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, a));
295311
}
296312

297-
fn fold_view_item_import(&env e, &span sp, vec[ident] is,
298-
ast.def_id id) -> @ast.view_item {
313+
fn fold_view_item_import(&env e, &span sp,
314+
import_map index,
315+
vec[ident] is, ast.def_id id) -> @ast.view_item {
299316
// Produce errors for invalid imports
300317
auto len = _vec.len[ast.ident](is);
301318
auto last_id = is.(len - 1u);
302-
auto d = lookup_name(e, last_id);
319+
auto d = lookup_name(e, none[import_map], last_id);
303320
alt (d) {
304321
case (none[def_wrap]) {
305322
e.sess.span_err(sp, "unresolved name: " + last_id);
306323
}
307-
case (some[def_wrap](_)) {
324+
case (some[def_wrap](?d2)) {
325+
index.insert(id, d2);
308326
}
309327
}
310328

311329
ret @fold.respan[ast.view_item_](sp, ast.view_item_import(is, id));
312330
}
313331

314332

315-
fn fold_ty_path(&env e, &span sp, ast.path p,
333+
fn fold_ty_path(&env e, &span sp, import_map index, ast.path p,
316334
&option.t[def] d) -> @ast.ty {
317335

318336
let uint len = _vec.len[ast.name](p);
@@ -327,7 +345,7 @@ fn fold_ty_path(&env e, &span sp, ast.path p,
327345
e.sess.unimpl("resolving path ty with ty params");
328346
}
329347

330-
auto d_ = unwrap_def(lookup_name(e, n.node.ident));
348+
auto d_ = unwrap_def(lookup_name(e, some(index), n.node.ident));
331349

332350
alt (d_) {
333351
case (some[def](_)) {
@@ -361,10 +379,12 @@ fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
361379

362380
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
363381

364-
fld = @rec( fold_pat_tag = bind fold_pat_tag(_,_,_,_,_,_),
365-
fold_expr_name = bind fold_expr_name(_,_,_,_,_),
366-
fold_view_item_import = bind fold_view_item_import(_,_,_,_),
367-
fold_ty_path = bind fold_ty_path(_,_,_,_),
382+
auto import_index = new_def_hash[def_wrap]();
383+
fld = @rec( fold_pat_tag = bind fold_pat_tag(_,_,import_index,_,_,_,_),
384+
fold_expr_name = bind fold_expr_name(_,_,import_index,_,_,_),
385+
fold_view_item_import
386+
= bind fold_view_item_import(_,_,import_index,_,_),
387+
fold_ty_path = bind fold_ty_path(_,_,import_index,_,_),
368388
update_env_for_crate = bind update_env_for_crate(_,_),
369389
update_env_for_item = bind update_env_for_item(_,_),
370390
update_env_for_block = bind update_env_for_block(_,_),

src/test/run-pass/import4.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import zed.bar;
2+
mod zed {
3+
fn bar() {
4+
log "bar";
5+
}
6+
}
7+
fn main(vec[str] args) {
8+
auto zed = 42;
9+
bar();
10+
}

0 commit comments

Comments
 (0)