Skip to content

Commit c5a766f

Browse files
cixtorgraydon
authored andcommitted
Fix two invalid import cases we were not detecting:
* If an import was unused we would never print any errors for it. * We would keep the existing environment in scope when descending 'foo.bar' and would find 'bar' in the global environment if there was no 'bar' in 'foo'.
1 parent a8eeec1 commit c5a766f

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ TEST_XFAILS_BOOT := $(TASK_XFAILS) \
402402
test/run-pass/vec-slice.rs \
403403
test/run-pass/while-and-do-while.rs \
404404
test/run-fail/task-comm-14.rs \
405+
test/compile-fail/import.rs \
406+
test/compile-fail/import2.rs \
405407
test/compile-fail/bad-recv.rs \
406408
test/compile-fail/bad-send.rs \
407409
test/compile-fail/infinite-vec-type-recursion.rs \
@@ -477,6 +479,8 @@ TEST_XFAILS_RUSTC := $(filter-out \
477479
$(addprefix test/compile-fail/, \
478480
arg-count-mismatch.rs \
479481
arg-type-mismatch.rs \
482+
import.rs \
483+
import2.rs \
480484
while-type-error.rs \
481485
), \
482486
$(wildcard test/*/*.rs test/*/*.rc))

src/comp/middle/resolve.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,26 @@ fn unwrap_def(option.t[def_wrap] d_) -> option.t[def] {
6666

6767
// Follow the path of an import and return what it ultimately points to.
6868

69-
fn find_final_def(&env e, vec[ident] idents) -> option.t[def_wrap] {
69+
fn find_final_def(&env e, &span sp, vec[ident] idents) -> option.t[def_wrap] {
7070
auto len = _vec.len[ident](idents);
7171
auto first = idents.(0);
72+
auto d_ = lookup_name(e, first);
7273
if (len == 1u) {
73-
ret lookup_name(e, first);
74+
ret d_;
7475
}
75-
auto d_ = lookup_name(e, first);
7676
alt (d_) {
7777
case (none[def_wrap]) {
78+
e.sess.span_err(sp, "unresolved name: " + first);
7879
ret d_;
7980
}
8081
case (some[def_wrap](?d)) {
81-
alt(d) {
82+
alt (d) {
8283
case (def_wrap_mod(?i)) {
83-
auto new_env = update_env_for_item(e, i);
8484
auto new_idents = _vec.slice[ident](idents, 1u, len);
85-
ret find_final_def(new_env, new_idents);
85+
auto tmp_e = rec(scopes = nil[scope],
86+
sess = e.sess);
87+
auto new_e = update_env_for_item(tmp_e, i);
88+
ret find_final_def(new_e, sp, new_idents);
8689
}
8790
}
8891
}
@@ -140,12 +143,7 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
140143
ret some[def_wrap](def_wrap_use(i));
141144
}
142145
case (ast.view_item_import(?idents,_)) {
143-
auto d = find_final_def(e, idents);
144-
alt (d) {
145-
case (some[def_wrap](_)) {
146-
ret d;
147-
}
148-
}
146+
ret find_final_def(e, i.span, idents);
149147
}
150148
}
151149
fail;
@@ -296,6 +294,24 @@ fn fold_expr_name(&env e, &span sp, &ast.name n,
296294
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, a));
297295
}
298296

297+
fn fold_view_item_import(&env e, &span sp, vec[ident] is,
298+
ast.def_id id) -> @ast.view_item {
299+
// Produce errors for invalid imports
300+
auto len = _vec.len[ast.ident](is);
301+
auto last_id = is.(len - 1u);
302+
auto d = lookup_name(e, last_id);
303+
alt (d) {
304+
case (none[def_wrap]) {
305+
e.sess.span_err(sp, "unresolved name: " + last_id);
306+
}
307+
case (some[def_wrap](_)) {
308+
}
309+
}
310+
311+
ret @fold.respan[ast.view_item_](sp, ast.view_item_import(is, id));
312+
}
313+
314+
299315
fn fold_ty_path(&env e, &span sp, ast.path p,
300316
&option.t[def] d) -> @ast.ty {
301317

@@ -347,6 +363,7 @@ fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
347363

348364
fld = @rec( fold_pat_tag = bind fold_pat_tag(_,_,_,_,_,_),
349365
fold_expr_name = bind fold_expr_name(_,_,_,_,_),
366+
fold_view_item_import = bind fold_view_item_import(_,_,_,_),
350367
fold_ty_path = bind fold_ty_path(_,_,_,_),
351368
update_env_for_crate = bind update_env_for_crate(_,_),
352369
update_env_for_item = bind update_env_for_item(_,_),

src/test/compile-fail/import.rs

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

src/test/compile-fail/import2.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: unresolved name: zed
2+
import baz.zed.bar;
3+
mod baz {
4+
}
5+
mod zed {
6+
fn bar() {
7+
log "bar3";
8+
}
9+
}
10+
fn main(vec[str] args) {
11+
bar();
12+
}

src/test/run-pass/use.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use libc();
33
use zed(name = "std");
44
use bar(name = "std", ver = "0.0.1");
55

6-
import std._str;
7-
import x = std._str;
8-
6+
// FIXME: commented out since resolve doesn't know how to handle crates yet.
7+
// import std._str;
8+
// import x = std._str;
99

1010
mod baz {
1111
use std;
1212
use libc();
1313
use zed(name = "std");
1414
use bar(name = "std", ver = "0.0.1");
1515

16-
import std._str;
17-
import x = std._str;
16+
// import std._str;
17+
// import x = std._str;
1818
}
1919

2020
fn main() {

0 commit comments

Comments
 (0)