Skip to content

Commit 9d43e03

Browse files
cixtorgraydon
authored andcommitted
---
yaml --- r: 1208 b: refs/heads/master c: 2fb09eb h: refs/heads/master v: v3
1 parent b145a46 commit 9d43e03

File tree

7 files changed

+139
-38
lines changed

7 files changed

+139
-38
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: 3c61793b5aeef3ab3e8fe876fb94dd9c8c4410c6
2+
refs/heads/master: 2fb09eb58569f5a0e5544c6256173d4b6b8ffcf2

trunk/src/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ TEST_XFAILS_RUSTC := $(filter-out \
437437
int.rs \
438438
i32-sub.rs \
439439
i8-incr.rs \
440+
import2.rs \
441+
import3.rs \
440442
item-name-overload.rs \
441443
large-records.rs \
442444
lazy-init.rs \

trunk/src/comp/front/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ tag def {
3636
def_ty_arg(def_id);
3737
def_binding(def_id);
3838
def_use(def_id);
39-
def_import(def_id);
4039
}
4140

4241
type crate = spanned[crate_];

trunk/src/comp/middle/fold.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,14 @@ fn fold_mod[ENV](&ENV e, ast_fold[ENV] fld, &ast._mod m) -> ast._mod {
781781
let vec[@item] items = vec();
782782
auto index = m.index;
783783

784-
for (@item i in m.items) {
785-
append[@item](items, fold_item[ENV](e, fld, i));
786-
}
787-
788784
for (@view_item vi in m.view_items) {
789785
append[@view_item](view_items, fold_view_item[ENV](e, fld, vi));
790786
}
791787

788+
for (@item i in m.items) {
789+
append[@item](items, fold_item[ENV](e, fld, i));
790+
}
791+
792792
ret fld.fold_mod(e, rec(view_items=view_items, items=items, index=index));
793793
}
794794

trunk/src/comp/middle/resolve.rs

Lines changed: 111 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,66 +24,139 @@ tag scope {
2424
type env = rec(list[scope] scopes,
2525
session.session sess);
2626

27-
fn lookup_name(&env e, ast.ident i) -> option.t[def] {
27+
// A simple wrapper over defs that stores a bit more information about modules
28+
// and uses so that we can use the regular lookup_name when resolving imports.
29+
tag def_wrap {
30+
def_wrap_use(@ast.view_item);
31+
def_wrap_mod(@ast.item);
32+
def_wrap_other(def);
33+
}
34+
35+
fn unwrap_def(option.t[def_wrap] d_) -> option.t[def] {
36+
alt (d_) {
37+
case (none[def_wrap]) {
38+
ret none[def];
39+
}
40+
case (some[def_wrap](?d)) {
41+
alt (d) {
42+
case (def_wrap_use(?it)) {
43+
alt (it.node) {
44+
case (ast.view_item_use(_, _, ?id)) {
45+
ret some[def](ast.def_use(id));
46+
}
47+
}
48+
fail;
49+
}
50+
case (def_wrap_mod(?i)) {
51+
alt (i.node) {
52+
case (ast.item_mod(_, _, ?id)) {
53+
ret some[def](ast.def_mod(id));
54+
}
55+
}
56+
fail;
57+
}
58+
case (def_wrap_other(?d)) {
59+
ret some[def](d);
60+
}
61+
}
62+
}
63+
}
64+
fail;
65+
}
66+
67+
// Follow the path of an import and return what it ultimately points to.
68+
69+
fn find_final_def(&env e, vec[ident] idents) -> option.t[def_wrap] {
70+
auto len = _vec.len[ident](idents);
71+
auto first = idents.(0);
72+
if (len == 1u) {
73+
ret lookup_name(e, first);
74+
}
75+
auto d_ = lookup_name(e, first);
76+
alt (d_) {
77+
case (none[def_wrap]) {
78+
ret d_;
79+
}
80+
case (some[def_wrap](?d)) {
81+
alt(d) {
82+
case (def_wrap_mod(?i)) {
83+
auto new_env = update_env_for_item(e, i);
84+
auto new_idents = _vec.slice[ident](idents, 1u, len);
85+
ret find_final_def(new_env, new_idents);
86+
}
87+
}
88+
}
89+
}
90+
fail;
91+
}
92+
93+
fn lookup_name(&env e, ast.ident i) -> option.t[def_wrap] {
2894

2995
// log "resolving name " + i;
3096

31-
fn found_def_item(@ast.item i) -> option.t[def] {
97+
fn found_def_item(@ast.item i) -> option.t[def_wrap] {
3298
alt (i.node) {
3399
case (ast.item_const(_, _, _, ?id, _)) {
34-
ret some[def](ast.def_const(id));
100+
ret some[def_wrap](def_wrap_other(ast.def_const(id)));
35101
}
36102
case (ast.item_fn(_, _, _, ?id, _)) {
37-
ret some[def](ast.def_fn(id));
103+
ret some[def_wrap](def_wrap_other(ast.def_fn(id)));
38104
}
39105
case (ast.item_mod(_, _, ?id)) {
40-
ret some[def](ast.def_mod(id));
106+
ret some[def_wrap](def_wrap_mod(i));
41107
}
42108
case (ast.item_ty(_, _, _, ?id, _)) {
43-
ret some[def](ast.def_ty(id));
109+
ret some[def_wrap](def_wrap_other(ast.def_ty(id)));
44110
}
45111
case (ast.item_tag(_, _, _, ?id)) {
46-
ret some[def](ast.def_ty(id));
112+
ret some[def_wrap](def_wrap_other(ast.def_ty(id)));
47113
}
48114
case (ast.item_obj(_, _, _, ?id, _)) {
49-
ret some[def](ast.def_obj(id));
115+
ret some[def_wrap](def_wrap_other(ast.def_obj(id)));
50116
}
51117
}
52118
}
53119

54-
fn found_decl_stmt(@ast.stmt s) -> option.t[def] {
120+
fn found_decl_stmt(@ast.stmt s) -> option.t[def_wrap] {
55121
alt (s.node) {
56122
case (ast.stmt_decl(?d)) {
57123
alt (d.node) {
58124
case (ast.decl_local(?loc)) {
59-
ret some[def](ast.def_local(loc.id));
125+
auto t = ast.def_local(loc.id);
126+
ret some[def_wrap](def_wrap_other(t));
60127
}
61128
case (ast.decl_item(?it)) {
62129
ret found_def_item(it);
63130
}
64131
}
65132
}
66133
}
67-
ret none[def];
134+
ret none[def_wrap];
68135
}
69136

70-
fn found_def_view(@ast.view_item i) -> option.t[def] {
137+
fn found_def_view(&env e, @ast.view_item i) -> option.t[def_wrap] {
71138
alt (i.node) {
72139
case (ast.view_item_use(_, _, ?id)) {
73-
ret some[def](ast.def_use(id));
140+
ret some[def_wrap](def_wrap_use(i));
74141
}
75-
case (ast.view_item_import(_,?id)) {
76-
ret some[def](ast.def_import(id));
142+
case (ast.view_item_import(?idents,_)) {
143+
auto d = find_final_def(e, idents);
144+
alt (d) {
145+
case (some[def_wrap](_)) {
146+
ret d;
147+
}
148+
}
77149
}
78150
}
151+
fail;
79152
}
80153

81-
fn check_mod(ast.ident i, ast._mod m) -> option.t[def] {
154+
fn check_mod(&env e, ast.ident i, ast._mod m) -> option.t[def_wrap] {
82155
alt (m.index.find(i)) {
83156
case (some[ast.mod_index_entry](?ent)) {
84157
alt (ent) {
85158
case (ast.mie_view_item(?ix)) {
86-
ret found_def_view(m.view_items.(ix));
159+
ret found_def_view(e, m.view_items.(ix));
87160
}
88161
case (ast.mie_item(?ix)) {
89162
ret found_def_item(m.items.(ix));
@@ -92,7 +165,8 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
92165
alt (m.items.(item_idx).node) {
93166
case (ast.item_tag(_, ?variants, _, ?tid)) {
94167
auto vid = variants.(variant_idx).id;
95-
ret some[def](ast.def_variant(tid, vid));
168+
auto t = ast.def_variant(tid, vid);
169+
ret some[def_wrap](def_wrap_other(t));
96170
}
97171
case (_) {
98172
log "tag item not actually a tag";
@@ -104,45 +178,49 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
104178
}
105179
case (none[ast.mod_index_entry]) { /* fall through */ }
106180
}
107-
ret none[def];
181+
ret none[def_wrap];
108182
}
109183

110184

111-
fn in_scope(ast.ident i, &scope s) -> option.t[def] {
185+
fn in_scope(ast.ident i, env e, &scope s) -> option.t[def_wrap] {
112186
alt (s) {
113187

114188
case (scope_crate(?c)) {
115-
ret check_mod(i, c.node.module);
189+
ret check_mod(e, i, c.node.module);
116190
}
117191

118192
case (scope_item(?it)) {
119193
alt (it.node) {
120194
case (ast.item_fn(_, ?f, ?ty_params, _, _)) {
121195
for (ast.arg a in f.inputs) {
122196
if (_str.eq(a.ident, i)) {
123-
ret some[def](ast.def_arg(a.id));
197+
auto t = ast.def_arg(a.id);
198+
ret some[def_wrap](def_wrap_other(t));
124199
}
125200
}
126201
for (ast.ty_param tp in ty_params) {
127202
if (_str.eq(tp.ident, i)) {
128-
ret some[def](ast.def_ty_arg(tp.id));
203+
auto t = ast.def_ty_arg(tp.id);
204+
ret some[def_wrap](def_wrap_other(t));
129205
}
130206
}
131207
}
132208
case (ast.item_obj(_, ?ob, ?ty_params, _, _)) {
133209
for (ast.obj_field f in ob.fields) {
134210
if (_str.eq(f.ident, i)) {
135-
ret some[def](ast.def_obj_field(f.id));
211+
auto t = ast.def_obj_field(f.id);
212+
ret some[def_wrap](def_wrap_other(t));
136213
}
137214
}
138215
for (ast.ty_param tp in ty_params) {
139216
if (_str.eq(tp.ident, i)) {
140-
ret some[def](ast.def_ty_arg(tp.id));
217+
auto t = ast.def_ty_arg(tp.id);
218+
ret some[def_wrap](def_wrap_other(t));
141219
}
142220
}
143221
}
144222
case (ast.item_mod(_, ?m, _)) {
145-
ret check_mod(i, m);
223+
ret check_mod(e, i, m);
146224
}
147225
case (_) { /* fall through */ }
148226
}
@@ -160,22 +238,23 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
160238
case (scope_arm(?a)) {
161239
alt (a.index.find(i)) {
162240
case (some[ast.def_id](?did)) {
163-
ret some[def](ast.def_binding(did));
241+
auto t = ast.def_binding(did);
242+
ret some[def_wrap](def_wrap_other(t));
164243
}
165244
case (_) { /* fall through */ }
166245
}
167246
}
168247
}
169-
ret none[def];
248+
ret none[def_wrap];
170249
}
171250

172-
ret std.list.find[scope,def](e.scopes, bind in_scope(i, _));
251+
ret std.list.find[scope,def_wrap](e.scopes, bind in_scope(i, e, _));
173252
}
174253

175254
fn fold_pat_tag(&env e, &span sp, ident i, vec[@ast.pat] args,
176255
option.t[ast.variant_def] old_def, ann a) -> @ast.pat {
177256
auto new_def;
178-
alt (lookup_name(e, i)) {
257+
alt (unwrap_def(lookup_name(e, i))) {
179258
case (some[def](?d)) {
180259
alt (d) {
181260
case (ast.def_variant(?did, ?vid)) {
@@ -203,7 +282,7 @@ fn fold_expr_name(&env e, &span sp, &ast.name n,
203282
e.sess.unimpl("resolving name expr with ty params");
204283
}
205284

206-
auto d_ = lookup_name(e, n.node.ident);
285+
auto d_ = unwrap_def(lookup_name(e, n.node.ident));
207286

208287
alt (d_) {
209288
case (some[def](_)) {
@@ -232,7 +311,7 @@ fn fold_ty_path(&env e, &span sp, ast.path p,
232311
e.sess.unimpl("resolving path ty with ty params");
233312
}
234313

235-
auto d_ = lookup_name(e, n.node.ident);
314+
auto d_ = unwrap_def(lookup_name(e, n.node.ident));
236315

237316
alt (d_) {
238317
case (some[def](_)) {

trunk/src/test/run-pass/import2.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import zed.bar;
2+
mod zed {
3+
fn bar() {
4+
log "bar";
5+
}
6+
}
7+
fn main(vec[str] args) {
8+
bar();
9+
}

trunk/src/test/run-pass/import3.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import zed.bar;
2+
import baz.zed;
3+
mod baz {
4+
mod zed {
5+
fn bar() {
6+
log "bar2";
7+
}
8+
}
9+
}
10+
fn main(vec[str] args) {
11+
bar();
12+
}

0 commit comments

Comments
 (0)