Skip to content

Commit 6aff4e3

Browse files
committed
---
yaml --- r: 6451 b: refs/heads/master c: e98286b h: refs/heads/master i: 6449: 40d50bd 6447: 4a80710 v: v3
1 parent c1112a9 commit 6aff4e3

File tree

10 files changed

+114
-83
lines changed

10 files changed

+114
-83
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 03f6060e802e1acd8efe85b07bc98a97bf5caa7d
2+
refs/heads/master: e98286b5944c27cde4a5a268ff4ca926b40b8e76

trunk/src/comp/front/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
7070
ast::blk_ {
7171
let filter = bind filter_stmt(cfg, _);
7272
let filtered_stmts = vec::filter_map(filter, b.stmts);
73-
ret {stmts: vec::map(fld.fold_stmt, filtered_stmts),
73+
ret {view_items: b.view_items,
74+
stmts: vec::map(fld.fold_stmt, filtered_stmts),
7475
expr: option::map(fld.fold_expr, b.expr),
7576
id: b.id,
7677
rules: b.rules};

trunk/src/comp/front/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ fn mk_test_wrapper(cx: test_ctxt,
348348
};
349349

350350
let wrapper_body: ast::blk = nospan({
351+
view_items: [],
351352
stmts: [@call_stmt],
352353
expr: option::none,
353354
id: cx.next_node_id(),

trunk/src/comp/middle/resolve.rs

Lines changed: 88 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ tag scope {
3737
scope_fn(ast::fn_decl, ast::proto, [ast::ty_param]);
3838
scope_native_item(@ast::native_item);
3939
scope_loop(@ast::local); // there's only 1 decl per loop.
40-
4140
scope_block(ast::blk, @mutable uint, @mutable uint);
4241
scope_arm(ast::arm);
4342
}
@@ -105,6 +104,7 @@ type env =
105104
ast_map: ast_map::map,
106105
imports: hashmap<ast::node_id, import_state>,
107106
mod_map: hashmap<ast::node_id, @indexed_mod>,
107+
block_map: hashmap<ast::node_id, [glob_imp_def]>,
108108
ext_map: hashmap<def_id, [ident]>,
109109
ext_cache: ext_hash,
110110
used_imports: {mutable track: bool,
@@ -124,11 +124,12 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
124124
{def_map: def_map, ext_map: ext_map} {
125125
let e =
126126
@{cstore: sess.get_cstore(),
127-
def_map: new_int_hash::<def>(),
127+
def_map: new_int_hash(),
128128
ast_map: amap,
129-
imports: new_int_hash::<import_state>(),
130-
mod_map: new_int_hash::<@indexed_mod>(),
131-
ext_map: new_def_hash::<[ident]>(),
129+
imports: new_int_hash(),
130+
mod_map: new_int_hash(),
131+
block_map: new_int_hash(),
132+
ext_map: new_def_hash(),
132133
ext_cache: new_ext_hash(),
133134
used_imports: {mutable track: false, mutable data: []},
134135
mutable reported: [],
@@ -149,14 +150,14 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
149150
// resolve through them.
150151
fn map_crate(e: @env, c: @ast::crate) {
151152
// First, find all the modules, and index the names that they contain
152-
153153
let v_map_mod =
154154
@{visit_view_item: bind index_vi(e, _, _, _),
155-
visit_item: bind index_i(e, _, _, _)
156-
with *visit::default_visitor::<scopes>()};
155+
visit_item: bind index_i(e, _, _, _),
156+
visit_block: visit_block_with_scope
157+
with *visit::default_visitor::<scopes>()};
157158
visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v_map_mod));
158-
// Register the top-level mod
159159

160+
// Register the top-level mod
160161
e.mod_map.insert(-1,
161162
@{m: some(c.node.module),
162163
index: index_mod(c.node.module),
@@ -200,38 +201,36 @@ fn map_crate(e: @env, c: @ast::crate) {
200201
_ { }
201202
}
202203
}
203-
// Next, assemble the links for globbed imports.
204204

205+
// Next, assemble the links for globbed imports.
205206
let v_link_glob =
206207
@{visit_view_item: bind link_glob(e, _, _, _),
208+
visit_block: visit_block_with_scope,
207209
visit_item: visit_item_with_scope
208-
with *visit::default_visitor::<scopes>()};
210+
with *visit::default_visitor::<scopes>()};
209211
visit::visit_crate(*c, cons(scope_crate, @nil),
210212
visit::mk_vt(v_link_glob));
211213
fn link_glob(e: @env, vi: @ast::view_item, sc: scopes, _v: vt<scopes>) {
212-
fn find_mod(e: @env, sc: scopes) -> @indexed_mod {
213-
alt sc {
214-
cons(scope_item(i), tl) {
215-
alt i.node {
216-
ast::item_mod(_) | ast::item_native_mod(_) {
217-
ret e.mod_map.get(i.id);
218-
}
219-
_ { be find_mod(e, *tl); }
220-
}
221-
}
222-
_ {
223-
ret e.mod_map.get(-1); //top-level
224-
225-
}
226-
}
227-
}
228214
alt vi.node {
229215
//if it really is a glob import, that is
230216
ast::view_item_import_glob(path, _) {
231217
let imp = follow_import(*e, sc, path, vi.span);
232218
if option::is_some(imp) {
233-
find_mod(e, sc).glob_imports +=
234-
[{def: option::get(imp), item: vi}];
219+
let glob = {def: option::get(imp), item: vi};;
220+
alt sc {
221+
cons(scope_item(i), _) {
222+
e.mod_map.get(i.id).glob_imports += [glob];
223+
}
224+
cons(scope_block(b, _, _), _) {
225+
let globs = alt e.block_map.find(b.node.id) {
226+
some(globs) { globs + [glob] } none. { [glob] }
227+
};
228+
e.block_map.insert(b.node.id, globs);
229+
}
230+
nil. {
231+
e.mod_map.get(-1).glob_imports += [glob];
232+
}
233+
}
235234
}
236235
}
237236
_ { }
@@ -370,6 +369,7 @@ fn visit_fn_with_scope(e: @env, f: ast::_fn, tp: [ast::ty_param], sp: span,
370369
fn visit_block_with_scope(b: ast::blk, sc: scopes, v: vt<scopes>) {
371370
let pos = @mutable 0u, loc = @mutable 0u;
372371
let block_sc = cons(scope_block(b, pos, loc), @sc);
372+
for vi in b.node.view_items { v.visit_view_item(vi, block_sc, v); }
373373
for stmt in b.node.stmts {
374374
v.visit_stmt(stmt, block_sc, v);;
375375
*pos += 1u;;
@@ -426,9 +426,8 @@ fn follow_import(e: env, sc: scopes, path: [ident], sp: span) ->
426426
alt option::get(dcur) {
427427
ast::def_mod(_) | ast::def_native_mod(_) { ret dcur; }
428428
_ {
429-
e.sess.span_err(sp,
430-
str::connect(path, "::") +
431-
" does not name a module.");
429+
e.sess.span_err(sp, str::connect(path, "::") +
430+
" does not name a module.");
432431
ret none;
433432
}
434433
}
@@ -678,7 +677,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
678677
}
679678
}
680679
scope_block(b, pos, loc) {
681-
ret lookup_in_block(name, b.node, *pos, *loc, ns);
680+
ret lookup_in_block(e, name, sp, b.node, *pos, *loc, ns);
682681
}
683682
scope_arm(a) {
684683
if ns == ns_value {
@@ -793,8 +792,8 @@ fn lookup_in_obj(name: ident, ob: ast::_obj, ty_params: [ast::ty_param],
793792
}
794793
}
795794

796-
fn lookup_in_block(name: ident, b: ast::blk_, pos: uint, loc_pos: uint,
797-
ns: namespace) -> option::t<def> {
795+
fn lookup_in_block(e: env, name: ident, sp: span, b: ast::blk_, pos: uint,
796+
loc_pos: uint, ns: namespace) -> option::t<def> {
798797
let i = vec::len(b.stmts);
799798
while i > 0u {
800799
i -= 1u;
@@ -849,7 +848,30 @@ fn lookup_in_block(name: ident, b: ast::blk_, pos: uint, loc_pos: uint,
849848
_ { }
850849
}
851850
}
852-
ret none::<def>;
851+
for vi in b.view_items {
852+
alt vi.node {
853+
ast::view_item_import(ident, _, id) {
854+
if name == ident { ret lookup_import(e, local_def(id), ns); }
855+
}
856+
ast::view_item_import_from(mod_path, idents, id) {
857+
for ident in idents {
858+
if name == ident.node.name {
859+
ret lookup_import(e, local_def(ident.node.id), ns);
860+
}
861+
}
862+
}
863+
ast::view_item_import_glob(_, _) {
864+
alt e.block_map.find(b.id) {
865+
some(globs) {
866+
let found = lookup_in_globs(e, globs, sp, name, ns, inside);
867+
if found != none { ret found; }
868+
}
869+
_ {}
870+
}
871+
}
872+
}
873+
}
874+
ret none;
853875
}
854876

855877
fn found_def_item(i: @ast::item, ns: namespace) -> option::t<def> {
@@ -991,57 +1013,51 @@ fn lookup_in_local_mod(e: env, node_id: node_id, sp: span, id: ident,
9911013
}
9921014
}
9931015
}
994-
9951016
// not local or explicitly imported; try globs:
9961017
ret lookup_glob_in_mod(e, info, sp, id, ns, outside);
9971018
}
9981019

999-
fn lookup_glob_in_mod(e: env, info: @indexed_mod, sp: span, id: ident,
1000-
wanted_ns: namespace, dr: dir) -> option::t<def> {
1001-
fn per_ns(e: env, info: @indexed_mod, sp: span, id: ident, ns: namespace,
1002-
dr: dir) -> option::t<def> {
1003-
1004-
fn lookup_in_mod_(e: env, def: glob_imp_def, sp: span, name: ident,
1005-
ns: namespace, dr: dir) -> option::t<glob_imp_def> {
1006-
alt lookup_in_mod(e, def.def, sp, name, ns, dr) {
1007-
option::some(d) { option::some({def: d, item: def.item}) }
1008-
option::none. { option::none }
1009-
}
1020+
fn lookup_in_globs(e: env, globs: [glob_imp_def], sp: span, id: ident,
1021+
ns: namespace, dr: dir) -> option::t<def> {
1022+
fn lookup_in_mod_(e: env, def: glob_imp_def, sp: span, name: ident,
1023+
ns: namespace, dr: dir) -> option::t<glob_imp_def> {
1024+
alt lookup_in_mod(e, def.def, sp, name, ns, dr) {
1025+
option::some(d) { option::some({def: d, item: def.item}) }
1026+
option::none. { option::none }
10101027
}
1011-
1012-
let matches =
1013-
vec::filter_map(bind lookup_in_mod_(e, _, sp, id, ns, dr),
1014-
{ info.glob_imports });
1015-
if vec::len(matches) == 0u {
1016-
ret none;
1017-
} else if vec::len(matches) == 1u {
1018-
ret some(matches[0].def);
1019-
} else {
1020-
for match: glob_imp_def in matches {
1021-
let sp = match.item.span;
1022-
e.sess.span_note(sp, #fmt["'%s' is imported here", id]);
1023-
}
1024-
e.sess.span_fatal(sp,
1025-
"'" + id + "' is glob-imported from" +
1026-
" multiple different modules.");
1028+
}
1029+
let matches =
1030+
vec::filter_map(bind lookup_in_mod_(e, _, sp, id, ns, dr),
1031+
{ globs });
1032+
if vec::len(matches) == 0u {
1033+
ret none;
1034+
} else if vec::len(matches) == 1u {
1035+
ret some(matches[0].def);
1036+
} else {
1037+
for match: glob_imp_def in matches {
1038+
let sp = match.item.span;
1039+
e.sess.span_note(sp, #fmt["'%s' is imported here", id]);
10271040
}
1041+
e.sess.span_fatal(sp, "'" + id + "' is glob-imported from" +
1042+
" multiple different modules.");
10281043
}
1044+
}
1045+
1046+
fn lookup_glob_in_mod(e: env, info: @indexed_mod, sp: span, id: ident,
1047+
wanted_ns: namespace, dr: dir) -> option::t<def> {
10291048
// since we don't know what names we have in advance,
10301049
// absence takes the place of todo()
1031-
10321050
if !info.glob_imported_names.contains_key(id) {
10331051
info.glob_imported_names.insert(id, resolving(sp));
1034-
let val = per_ns(e, info, sp, id, ns_value, dr);
1035-
let typ = per_ns(e, info, sp, id, ns_type, dr);
1036-
let md = per_ns(e, info, sp, id, ns_module, dr);
1052+
let val = lookup_in_globs(e, info.glob_imports, sp, id, ns_value, dr);
1053+
let typ = lookup_in_globs(e, info.glob_imports, sp, id, ns_type, dr);
1054+
let md = lookup_in_globs(e, info.glob_imports, sp, id, ns_module, dr);
10371055
info.glob_imported_names.insert(id, resolved(val, typ, md, id, sp));
10381056
}
10391057
alt info.glob_imported_names.get(id) {
10401058
todo(_, _, _, _, _) { e.sess.bug("Shouldn't've put a todo in."); }
1041-
resolving(sp) {
1042-
ret none::<def>; //circularity is okay in import globs
1043-
1044-
}
1059+
//circularity is okay in import globs
1060+
resolving(sp) { ret none::<def>; }
10451061
resolved(val, typ, md, _, _) {
10461062
ret alt wanted_ns {
10471063
ns_value. { val }

trunk/src/comp/syntax/ast.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ tag meta_item_ {
7676

7777
type blk = spanned<blk_>;
7878

79-
type blk_ =
80-
{stmts: [@stmt], expr: option::t<@expr>, id: node_id,
81-
rules: blk_check_mode};
79+
type blk_ = {view_items: [@view_item], stmts: [@stmt], expr: option::t<@expr>,
80+
id: node_id, rules: blk_check_mode};
8281

8382
type pat = {id: node_id, node: pat_, span: span};
8483

trunk/src/comp/syntax/ast_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn block_from_expr(e: @expr) -> blk {
199199

200200
fn default_block(stmts1: [@stmt], expr1: option::t<@expr>, id1: node_id) ->
201201
blk_ {
202-
ret {stmts: stmts1, expr: expr1, id: id1, rules: default_blk};
202+
{view_items: [], stmts: stmts1, expr: expr1, id: id1, rules: default_blk}
203203
}
204204

205205
fn obj_field_from_anon_obj_field(f: anon_obj_field) -> obj_field {

trunk/src/comp/syntax/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ fn noop_fold_method(m: method_, fld: ast_fold) -> method_ {
247247

248248

249249
fn noop_fold_block(b: blk_, fld: ast_fold) -> blk_ {
250-
ret {stmts: vec::map(fld.fold_stmt, b.stmts),
250+
ret {view_items: vec::map(fld.fold_view_item, b.view_items),
251+
stmts: vec::map(fld.fold_stmt, b.stmts),
251252
expr: option::map(fld.fold_expr, b.expr),
252253
id: b.id,
253254
rules: b.rules};

trunk/src/comp/syntax/parse/parser.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,8 +1697,8 @@ fn parse_block_no_value(p: parser) -> ast::blk {
16971697
// necessary, and this should take a qualifier.
16981698
// some blocks start with "#{"...
16991699
fn parse_block_tail(p: parser, lo: uint, s: ast::blk_check_mode) -> ast::blk {
1700-
let stmts: [@ast::stmt] = [];
1701-
let expr: option::t<@ast::expr> = none;
1700+
let view_items = [], stmts = [], expr = none;
1701+
while is_word(p, "import") { view_items += [parse_view_item(p)]; }
17021702
while p.peek() != token::RBRACE {
17031703
alt p.peek() {
17041704
token::SEMI. {
@@ -1736,7 +1736,8 @@ fn parse_block_tail(p: parser, lo: uint, s: ast::blk_check_mode) -> ast::blk {
17361736
}
17371737
let hi = p.get_hi_pos();
17381738
p.bump();
1739-
let bloc = {stmts: stmts, expr: expr, id: p.get_id(), rules: s};
1739+
let bloc = {view_items: view_items, stmts: stmts, expr: expr,
1740+
id: p.get_id(), rules: s};
17401741
ret spanned(lo, hi, bloc);
17411742
}
17421743

trunk/src/comp/syntax/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ fn visit_fn<E>(f: _fn, _tp: [ty_param], _sp: span, _i: fn_ident, _id: node_id,
200200
}
201201

202202
fn visit_block<E>(b: ast::blk, e: E, v: vt<E>) {
203-
for s: @stmt in b.node.stmts { v.visit_stmt(s, e, v); }
203+
for vi in b.node.view_items { v.visit_view_item(vi, e, v); }
204+
for s in b.node.stmts { v.visit_stmt(s, e, v); }
204205
visit_expr_opt(b.node.expr, e, v);
205206
}
206207

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std;
2+
3+
fn main() {
4+
import std::vec;
5+
import vec::to_mut;
6+
log vec::len(to_mut([1, 2]));
7+
{
8+
import vec::*;
9+
log len([2]);
10+
}
11+
}

0 commit comments

Comments
 (0)