Skip to content

Commit 7a2e5d8

Browse files
committed
---
yaml --- r: 2299 b: refs/heads/master c: 459b0ec h: refs/heads/master i: 2297: 6b8e5c2 2295: 24c88f8 v: v3
1 parent 76bf4eb commit 7a2e5d8

File tree

7 files changed

+144
-15
lines changed

7 files changed

+144
-15
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: 40624e35d74e5d200ae689c02753f0d60924e668
2+
refs/heads/master: 459b0ec8339da2b7ba2f76c3fd110f5b3e971a73

trunk/src/comp/middle/resolve.rs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ tag scope {
2727
scope_arm(ast.arm);
2828
}
2929

30+
// This indicates whether we're searching up the scope chain
31+
// or whether we've found a path component and started following
32+
// it back down, which has an effect on export visibility
33+
tag search_direction {
34+
up;
35+
down;
36+
}
37+
3038
type env = rec(list[scope] scopes,
31-
session.session sess);
39+
session.session sess,
40+
search_direction direction);
3241

3342
tag namespace {
3443
ns_value;
@@ -148,7 +157,8 @@ fn find_final_def(&env e, import_map index,
148157
auto len = _vec.len[ident](idents);
149158
auto rest_idents = _vec.slice[ident](idents, 1u, len);
150159
auto empty_e = rec(scopes = nil[scope],
151-
sess = e.sess);
160+
sess = e.sess,
161+
direction = down);
152162
auto tmp_e = update_env_for_item(empty_e, i);
153163
auto next_i = rest_idents.(0);
154164
auto next_ = lookup_name_wrapped(tmp_e, next_i, ns);
@@ -172,7 +182,9 @@ fn find_final_def(&env e, import_map index,
172182
-> def_wrap {
173183
auto len = _vec.len[ident](idents);
174184
auto rest_idents = _vec.slice[ident](idents, 1u, len);
175-
auto empty_e = rec(scopes = nil[scope], sess = e.sess);
185+
auto empty_e = rec(scopes = nil[scope],
186+
sess = e.sess,
187+
direction = down);
176188
auto tmp_e = update_env_for_external_mod(empty_e, mod_id, idents);
177189
auto next_i = rest_idents.(0);
178190
auto next_ = lookup_name_wrapped(tmp_e, next_i, ns);
@@ -347,16 +359,50 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
347359
fail;
348360
}
349361

350-
fn check_mod(ast.ident i, ast._mod m, namespace ns)
362+
fn check_mod(&env e, ast.ident i, ast._mod m, namespace ns)
351363
-> option.t[def_wrap] {
364+
365+
fn visible(&env e, ast.ident i, ast._mod m) -> bool {
366+
367+
alt (e.direction) {
368+
case (up) {
369+
ret true;
370+
}
371+
case (down) {
372+
// fall through
373+
}
374+
}
375+
376+
auto count = 0;
377+
for (@ast.view_item vi in m.view_items) {
378+
alt (vi.node) {
379+
case (ast.view_item_export(?id)) {
380+
if (_str.eq(i, id)) {
381+
ret true;
382+
}
383+
count += 1;
384+
}
385+
case (_) { /* fall through */ }
386+
}
387+
}
388+
// If there are no declared exports then everything is exported
389+
if (count == 0) {
390+
ret true;
391+
} else {
392+
ret false;
393+
}
394+
}
395+
352396
alt (m.index.find(i)) {
353397
case (some[ast.mod_index_entry](?ent)) {
354398
alt (ent) {
355399
case (ast.mie_view_item(?view_item)) {
356400
ret some(found_def_view(view_item));
357401
}
358402
case (ast.mie_item(?item)) {
359-
ret some(found_def_item(item, ns));
403+
if (visible(e, i, m)) {
404+
ret some(found_def_item(item, ns));
405+
}
360406
}
361407
case (ast.mie_tag_variant(?item, ?variant_idx)) {
362408
alt (item.node) {
@@ -453,12 +499,12 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
453499
}
454500
}
455501

456-
fn in_scope(&session.session sess, ast.ident identifier, &scope s,
502+
fn in_scope(&env e, ast.ident identifier, &scope s,
457503
namespace ns) -> option.t[def_wrap] {
458504
alt (s) {
459505

460506
case (scope_crate(?c)) {
461-
ret check_mod(identifier, c.node.module, ns);
507+
ret check_mod(e, identifier, c.node.module, ns);
462508
}
463509

464510
case (scope_item(?it)) {
@@ -494,7 +540,7 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
494540
}
495541
}
496542
case (ast.item_mod(_, ?m, _)) {
497-
ret check_mod(identifier, m, ns);
543+
ret check_mod(e, identifier, m, ns);
498544
}
499545
case (ast.item_native_mod(_, ?m, _)) {
500546
ret check_native_mod(identifier, m);
@@ -522,7 +568,7 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
522568
}
523569

524570
case (scope_external_mod(?mod_id, ?path)) {
525-
ret lookup_external_def(sess, mod_id._0, path);
571+
ret lookup_external_def(e.sess, mod_id._0, path);
526572
}
527573

528574
case (scope_loop(?d)) {
@@ -558,7 +604,7 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
558604
ret none[tup(@env, def_wrap)];
559605
}
560606
case (cons[scope](?hd, ?tl)) {
561-
auto x = in_scope(e.sess, i, hd, ns);
607+
auto x = in_scope(e, i, hd, ns);
562608
alt (x) {
563609
case (some[def_wrap](?x)) {
564610
ret some(tup(@e, x));
@@ -737,7 +783,8 @@ fn resolve_imports(session.session sess, @ast.crate crate) -> @ast.crate {
737783
with *fld );
738784

739785
auto e = rec(scopes = nil[scope],
740-
sess = sess);
786+
sess = sess,
787+
direction = up);
741788

742789
ret fold.fold_crate[env](e, fld, crate);
743790
}
@@ -761,7 +808,8 @@ fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
761808
with *fld );
762809

763810
auto e = rec(scopes = nil[scope],
764-
sess = sess);
811+
sess = sess,
812+
direction = up);
765813

766814
ret fold.fold_crate[env](e, fld, new_crate);
767815
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// xfail-stage0
2+
// error-pattern: unknown module item
3+
4+
// rustboot has a different error message than rustc
5+
// this test can die with rustboot, or rustc's error can change
6+
7+
mod foo {
8+
export x;
9+
fn x(int y) {
10+
log y;
11+
}
12+
fn z(int y) {
13+
log y;
14+
}
15+
}
16+
17+
fn main() {
18+
foo.z(10);
19+
}

trunk/src/test/compile-fail/export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// xfail-stage0
2-
// error-pattern: unknown module item
1+
// xfail-boot
2+
// error-pattern: unresolved name
33
mod foo {
44
export x;
55
fn x(int y) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// xfail-boot
2+
// error-pattern: unresolved name
3+
4+
mod foo {
5+
export x;
6+
7+
fn x() {
8+
bar.x();
9+
}
10+
}
11+
12+
mod bar {
13+
export y;
14+
15+
fn x() {
16+
log "x";
17+
}
18+
19+
fn y() {
20+
}
21+
}
22+
23+
fn main() {
24+
foo.x();
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mod foo {
2+
3+
export bar;
4+
5+
mod bar {
6+
fn y() {
7+
x();
8+
}
9+
}
10+
11+
fn x() {
12+
log "x";
13+
}
14+
}
15+
16+
fn main() {
17+
foo.bar.y();
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
mod foo {
2+
export x;
3+
4+
fn x() {
5+
bar.x();
6+
}
7+
}
8+
9+
mod bar {
10+
export x;
11+
12+
fn x() {
13+
log "x";
14+
}
15+
}
16+
17+
fn main() {
18+
foo.x();
19+
}

0 commit comments

Comments
 (0)