Correctly handle "import foo = bar.zed;".

This commit is contained in:
Rafael Ávila de Espíndola 2011-01-28 11:54:59 -05:00
parent 13b7a356f8
commit 3cac20dae3
6 changed files with 54 additions and 28 deletions

View File

@ -466,6 +466,7 @@ TEST_XFAILS_RUSTC := $(filter-out \
import5.rs \
import6.rs \
import7.rs \
import8.rs \
item-name-overload.rs \
large-records.rs \
lazy-init.rs \

View File

@ -231,7 +231,7 @@ type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type view_item = spanned[view_item_];
tag view_item_ {
view_item_use(ident, vec[@meta_item], def_id);
view_item_import(vec[ident], def_id, option.t[def]);
view_item_import(ident, vec[ident], def_id, option.t[def]);
}
type item = spanned[item_];
@ -249,10 +249,8 @@ fn index_view_item(mod_index index, @view_item it) {
case(ast.view_item_use(?id, _, _)) {
index.insert(id, ast.mie_view_item(it));
}
case(ast.view_item_import(?ids,_,_)) {
auto len = _vec.len[ast.ident](ids);
auto last_id = ids.(len - 1u);
index.insert(last_id, ast.mie_view_item(it));
case(ast.view_item_import(?def_ident,_,_,_)) {
index.insert(def_ident, ast.mie_view_item(it));
}
}
}

View File

@ -1756,27 +1756,41 @@ impure fn parse_use(parser p) -> @ast.view_item {
ret @spanned(lo, hi, use_decl);
}
impure fn parse_rest_import_name(parser p, ast.ident id) -> @ast.view_item {
impure fn parse_rest_import_name(parser p, ast.ident first,
option.t[ast.ident] def_ident)
-> @ast.view_item {
auto lo = p.get_span();
auto hi = lo;
let vec[ast.ident] identifiers = vec();
identifiers += id;
identifiers += first;
while (p.peek() != token.SEMI) {
expect(p, token.DOT);
auto i = parse_ident(p);
identifiers += i;
}
p.bump();
auto import_decl = ast.view_item_import(identifiers, p.next_def_id(),
auto defined_id;
alt (def_ident) {
case(some[ast.ident](?i)) {
defined_id = i;
}
case (_) {
auto len = _vec.len[ast.ident](identifiers);
defined_id = identifiers.(len - 1u);
}
}
auto import_decl = ast.view_item_import(defined_id, identifiers,
p.next_def_id(),
none[ast.def]);
ret @spanned(lo, hi, import_decl);
}
impure fn parse_full_import_name(parser p) -> @ast.view_item {
impure fn parse_full_import_name(parser p, ast.ident def_ident)
-> @ast.view_item {
alt (p.peek()) {
case (token.IDENT(?ident)) {
p.bump();
ret parse_rest_import_name(p, ident);
ret parse_rest_import_name(p, ident, some(def_ident));
}
case (_) {
p.err("expecting an identifier");
@ -1793,10 +1807,10 @@ impure fn parse_import(parser p) -> @ast.view_item {
alt (p.peek()) {
case (token.EQ) {
p.bump();
ret parse_full_import_name(p);
ret parse_full_import_name(p, ident);
}
case (_) {
ret parse_rest_import_name(p, ident);
ret parse_rest_import_name(p, ident, none[ast.ident]);
}
}
}

View File

@ -213,7 +213,7 @@ type ast_fold[ENV] =
vec[@meta_item] meta_items,
def_id id) -> @view_item) fold_view_item_use,
(fn(&ENV e, &span sp, vec[ident] idents,
(fn(&ENV e, &span sp, ident i, vec[ident] idents,
def_id id, option.t[def]) -> @view_item) fold_view_item_import,
// Additional nodes.
@ -725,14 +725,13 @@ fn fold_view_item[ENV](&ENV env, ast_fold[ENV] fld, @view_item vi)
alt (vi.node) {
case (ast.view_item_use(?ident, ?meta_items, ?def_id)) {
// FIXME: what other folding should be done in here?
ret fld.fold_view_item_use(env_, vi.span, ident, meta_items,
def_id);
}
case (ast.view_item_import(?idents, ?def_id, ?target_def)) {
// FIXME: what other folding should be done in here?
ret fld.fold_view_item_import(env_, vi.span, idents, def_id,
target_def);
case (ast.view_item_import(?def_ident, ?idents, ?def_id,
?target_def)) {
ret fld.fold_view_item_import(env_, vi.span, def_ident, idents,
def_id, target_def);
}
}
@ -1116,10 +1115,11 @@ fn identity_fold_view_item_use[ENV](&ENV e, &span sp, ident i,
ret @respan(sp, ast.view_item_use(i, meta_items, id));
}
fn identity_fold_view_item_import[ENV](&ENV e, &span sp, vec[ident] is,
def_id id, option.t[def] target_def)
fn identity_fold_view_item_import[ENV](&ENV e, &span sp, ident i,
vec[ident] is, def_id id,
option.t[def] target_def)
-> @view_item {
ret @respan(sp, ast.view_item_import(is, id, target_def));
ret @respan(sp, ast.view_item_import(i, is, id, target_def));
}
// Additional identities.
@ -1271,7 +1271,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_view_item_use =
bind identity_fold_view_item_use[ENV](_,_,_,_,_),
fold_view_item_import =
bind identity_fold_view_item_import[ENV](_,_,_,_,_),
bind identity_fold_view_item_import[ENV](_,_,_,_,_,_),
fold_block = bind identity_fold_block[ENV](_,_,_),
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_,_),

View File

@ -50,7 +50,7 @@ fn unwrap_def(def_wrap d) -> def {
}
case (def_wrap_import(?it)) {
alt (it.node) {
case (ast.view_item_import(_, ?id, ?target_def)) {
case (ast.view_item_import(_, _, ?id, ?target_def)) {
alt (target_def) {
case (some[def](?d)) {
ret d;
@ -106,7 +106,7 @@ fn find_final_def(&env e, import_map index,
alt (d) {
case (def_wrap_import(?imp)) {
alt (imp.node) {
case (ast.view_item_import(?new_idents, ?d, _)) {
case (ast.view_item_import(_, ?new_idents, ?d, _)) {
auto x = find_final_def(e, index, sp, new_idents,
some(d));
ret found_something(e, index, sp, idents, x);
@ -235,7 +235,7 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] {
case (ast.view_item_use(_, _, ?id)) {
ret def_wrap_use(i);
}
case (ast.view_item_import(?idents,?d, _)) {
case (ast.view_item_import(_, ?idents,?d, _)) {
ret def_wrap_import(i);
}
}
@ -456,7 +456,7 @@ fn fold_expr_path(&env e, &span sp, &ast.path p, &option.t[def] d,
}
fn fold_view_item_import(&env e, &span sp,
import_map index,
import_map index, ident i,
vec[ident] is, ast.def_id id,
option.t[def] target_id) -> @ast.view_item {
// Produce errors for invalid imports
@ -472,7 +472,7 @@ fn fold_view_item_import(&env e, &span sp,
}
}
let option.t[def] target_def = some(unwrap_def(d));
ret @fold.respan[ast.view_item_](sp, ast.view_item_import(is, id,
ret @fold.respan[ast.view_item_](sp, ast.view_item_import(i, is, id,
target_def));
}
@ -514,7 +514,7 @@ fn resolve_imports(session.session sess, @ast.crate crate) -> @ast.crate {
auto import_index = new_def_hash[def_wrap]();
fld = @rec( fold_view_item_import
= bind fold_view_item_import(_,_,import_index,_,_,_),
= bind fold_view_item_import(_,_,import_index,_,_,_,_),
update_env_for_crate = bind update_env_for_crate(_,_),
update_env_for_item = bind update_env_for_item(_,_),
update_env_for_block = bind update_env_for_block(_,_),

View File

@ -0,0 +1,13 @@
import foo.x;
import z = foo.x;
mod foo {
fn x(int y) {
log y;
}
}
fn main() {
x(10);
z(10);
}