From 3cac20dae3272728282466467cb0193d7dbbf00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?= Date: Fri, 28 Jan 2011 11:54:59 -0500 Subject: [PATCH] Correctly handle "import foo = bar.zed;". --- src/Makefile | 1 + src/comp/front/ast.rs | 8 +++----- src/comp/front/parser.rs | 28 +++++++++++++++++++++------- src/comp/middle/fold.rs | 20 ++++++++++---------- src/comp/middle/resolve.rs | 12 ++++++------ src/test/run-pass/import8.rs | 13 +++++++++++++ 6 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 src/test/run-pass/import8.rs diff --git a/src/Makefile b/src/Makefile index 487cbe606bc..adaddddc84d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 \ diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 2ae4c90734f..70da87ddfe3 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -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)); } } } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 5444e5fde96..5c13ef86d85 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -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]); } } } diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 8a6f82f2ddb..711db111b64 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -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](_,_,_,_,_,_), diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 380ec150cf6..1af3b20596c 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -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(_,_), diff --git a/src/test/run-pass/import8.rs b/src/test/run-pass/import8.rs new file mode 100644 index 00000000000..5031e98475b --- /dev/null +++ b/src/test/run-pass/import8.rs @@ -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); +}