Skip to content

Fix two invalid import cases #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ TEST_XFAILS_BOOT := $(TASK_XFAILS) \
test/run-pass/vec-slice.rs \
test/run-pass/while-and-do-while.rs \
test/run-fail/task-comm-14.rs \
test/compile-fail/import.rs \
test/compile-fail/import2.rs \
test/compile-fail/bad-recv.rs \
test/compile-fail/bad-send.rs \
test/compile-fail/infinite-vec-type-recursion.rs \
Expand Down Expand Up @@ -477,6 +479,8 @@ TEST_XFAILS_RUSTC := $(filter-out \
$(addprefix test/compile-fail/, \
arg-count-mismatch.rs \
arg-type-mismatch.rs \
import.rs \
import2.rs \
while-type-error.rs \
), \
$(wildcard test/*/*.rs test/*/*.rc))
Expand Down
41 changes: 29 additions & 12 deletions src/comp/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,26 @@ fn unwrap_def(option.t[def_wrap] d_) -> option.t[def] {

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

fn find_final_def(&env e, vec[ident] idents) -> option.t[def_wrap] {
fn find_final_def(&env e, &span sp, vec[ident] idents) -> option.t[def_wrap] {
auto len = _vec.len[ident](idents);
auto first = idents.(0);
auto d_ = lookup_name(e, first);
if (len == 1u) {
ret lookup_name(e, first);
ret d_;
}
auto d_ = lookup_name(e, first);
alt (d_) {
case (none[def_wrap]) {
e.sess.span_err(sp, "unresolved name: " + first);
ret d_;
}
case (some[def_wrap](?d)) {
alt(d) {
alt (d) {
case (def_wrap_mod(?i)) {
auto new_env = update_env_for_item(e, i);
auto new_idents = _vec.slice[ident](idents, 1u, len);
ret find_final_def(new_env, new_idents);
auto tmp_e = rec(scopes = nil[scope],
sess = e.sess);
auto new_e = update_env_for_item(tmp_e, i);
ret find_final_def(new_e, sp, new_idents);
}
}
}
Expand Down Expand Up @@ -140,12 +143,7 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
ret some[def_wrap](def_wrap_use(i));
}
case (ast.view_item_import(?idents,_)) {
auto d = find_final_def(e, idents);
alt (d) {
case (some[def_wrap](_)) {
ret d;
}
}
ret find_final_def(e, i.span, idents);
}
}
fail;
Expand Down Expand Up @@ -296,6 +294,24 @@ fn fold_expr_name(&env e, &span sp, &ast.name n,
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, a));
}

fn fold_view_item_import(&env e, &span sp, vec[ident] is,
ast.def_id id) -> @ast.view_item {
// Produce errors for invalid imports
auto len = _vec.len[ast.ident](is);
auto last_id = is.(len - 1u);
auto d = lookup_name(e, last_id);
alt (d) {
case (none[def_wrap]) {
e.sess.span_err(sp, "unresolved name: " + last_id);
}
case (some[def_wrap](_)) {
}
}

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


fn fold_ty_path(&env e, &span sp, ast.path p,
&option.t[def] d) -> @ast.ty {

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

fld = @rec( fold_pat_tag = bind fold_pat_tag(_,_,_,_,_,_),
fold_expr_name = bind fold_expr_name(_,_,_,_,_),
fold_view_item_import = bind fold_view_item_import(_,_,_,_),
fold_ty_path = bind fold_ty_path(_,_,_,_),
update_env_for_crate = bind update_env_for_crate(_,_),
update_env_for_item = bind update_env_for_item(_,_),
Expand Down
11 changes: 11 additions & 0 deletions src/test/compile-fail/import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// error-pattern: unresolved name: baz
import zed.bar;
import zed.baz;
mod zed {
fn bar() {
log "bar";
}
}
fn main(vec[str] args) {
bar();
}
12 changes: 12 additions & 0 deletions src/test/compile-fail/import2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// error-pattern: unresolved name: zed
import baz.zed.bar;
mod baz {
}
mod zed {
fn bar() {
log "bar3";
}
}
fn main(vec[str] args) {
bar();
}
10 changes: 5 additions & 5 deletions src/test/run-pass/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use libc();
use zed(name = "std");
use bar(name = "std", ver = "0.0.1");

import std._str;
import x = std._str;

// FIXME: commented out since resolve doesn't know how to handle crates yet.
// import std._str;
// import x = std._str;

mod baz {
use std;
use libc();
use zed(name = "std");
use bar(name = "std", ver = "0.0.1");

import std._str;
import x = std._str;
// import std._str;
// import x = std._str;
}

fn main() {
Expand Down