@@ -35,6 +35,7 @@ tag def_wrap {
35
35
def_wrap_import ( @ast. view_item ) ;
36
36
def_wrap_mod ( @ast. item ) ;
37
37
def_wrap_other ( def) ;
38
+ def_wrap_expr_field ( ident) ;
38
39
def_wrap_resolving;
39
40
}
40
41
@@ -88,9 +89,11 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
88
89
89
90
// Follow the path of an import and return what it ultimately points to.
90
91
92
+ // If used after imports are resolved, import_id is none.
93
+
91
94
fn find_final_def ( & env e, import_map index,
92
95
& span sp, vec[ ident] idents ,
93
- ast. def_id import_id ) -> def_wrap {
96
+ option . t [ ast. def_id] import_id ) -> def_wrap {
94
97
95
98
// We are given a series of identifiers (a.b.c.d) and we know that
96
99
// in the environment 'e' the identifier 'a' was resolved to 'd'. We
@@ -101,7 +104,8 @@ fn find_final_def(&env e, import_map index,
101
104
case ( def_wrap_import ( ?imp) ) {
102
105
alt ( imp. node ) {
103
106
case ( ast. view_item_import ( ?new_idents, ?d, _) ) {
104
- auto x = inner ( e, index, sp, new_idents, d) ;
107
+ auto x = find_final_def ( e, index, sp, new_idents,
108
+ some ( d) ) ;
105
109
ret found_something ( e, index, sp, idents, x) ;
106
110
}
107
111
}
@@ -138,15 +142,14 @@ fn find_final_def(&env e, import_map index,
138
142
}
139
143
case ( _) {
140
144
auto first = idents. ( 0 ) ;
141
- e . sess . span_err ( sp , first + " is not a module or crate" ) ;
145
+ ret def_wrap_expr_field ( first) ;
142
146
}
143
147
}
144
148
fail;
145
149
}
146
150
147
- fn inner ( & env e, import_map index, & span sp, vec[ ident] idents ,
148
- ast. def_id import_id ) -> def_wrap {
149
- alt ( index. find ( import_id) ) {
151
+ if ( import_id != none[ ast. def_id ] ) {
152
+ alt ( index. find ( option. get [ ast. def_id ] ( import_id) ) ) {
150
153
case ( some[ def_wrap] ( ?x) ) {
151
154
alt ( x) {
152
155
case ( def_wrap_resolving) {
@@ -161,22 +164,23 @@ fn find_final_def(&env e, import_map index,
161
164
case ( none[ def_wrap] ) {
162
165
}
163
166
}
164
- auto first = idents. ( 0 ) ;
165
- index. insert ( import_id, def_wrap_resolving) ;
166
- auto d_ = lookup_name_wrapped ( e, first) ;
167
- alt ( d_) {
168
- case ( none[ tup ( @env, def_wrap) ] ) {
169
- e. sess . span_err ( sp, "unresolved name: " + first) ;
170
- fail;
171
- }
172
- case ( some[ tup ( @env, def_wrap) ] ( ?d) ) {
173
- auto x = found_something ( * d. _0 , index, sp, idents, d. _1 ) ;
174
- index. insert ( import_id, x) ;
175
- ret x;
167
+ index. insert ( option. get [ ast. def_id ] ( import_id) , def_wrap_resolving) ;
168
+ }
169
+ auto first = idents. ( 0 ) ;
170
+ auto d_ = lookup_name_wrapped ( e, first) ;
171
+ alt ( d_) {
172
+ case ( none[ tup ( @env, def_wrap) ] ) {
173
+ e. sess . span_err ( sp, "unresolved name: " + first) ;
174
+ fail;
175
+ }
176
+ case ( some[ tup ( @env, def_wrap) ] ( ?d) ) {
177
+ auto x = found_something ( * d. _0 , index, sp, idents, d. _1 ) ;
178
+ if ( import_id != none[ ast. def_id ] ) {
179
+ index. insert ( option. get [ ast. def_id ] ( import_id) , x) ;
176
180
}
181
+ ret x;
177
182
}
178
183
}
179
- ret inner( e, index, sp, idents, import_id) ;
180
184
}
181
185
182
186
fn lookup_name_wrapped ( & env e, ast . ident i) -> option. t[ tup ( @env , def_wrap ) ] {
@@ -434,10 +438,13 @@ fn fold_expr_path(&env e, &span sp, &ast.path p, &option.t[def] d,
434
438
}
435
439
}
436
440
437
- // FIXME: once espindola's modifications to lookup land, actually step
438
- // through the path doing speculative lookup, and extend the maximal
439
- // static prefix. For now we are always using the minimal prefix: first
440
- // ident is static anchor, rest turn into fields.
441
+ // FIXME: All this call to find_final_def does right now is find
442
+ // unresolved names. It should be extended to return a wrapper
443
+ // over ast.expr. It is in a perfect position to construct
444
+ // the expr_field(expr_field(...(expr_path(...)))) we should return.
445
+
446
+ auto index = new_def_hash[ def_wrap] ( ) ;
447
+ find_final_def ( e, index, sp, p. node . idents , none[ ast. def_id ] ) ;
441
448
442
449
auto p_ = rec ( node=rec ( idents = vec ( id0) with p. node ) with p) ;
443
450
auto ex = @fold. respan [ ast. expr_ ] ( sp, ast. expr_path ( p_, d_, a) ) ;
@@ -457,7 +464,14 @@ fn fold_view_item_import(&env e, &span sp,
457
464
// Produce errors for invalid imports
458
465
auto len = _vec. len [ ast. ident ] ( is) ;
459
466
auto last_id = is. ( len - 1 u) ;
460
- auto d = find_final_def ( e, index, sp, is, id) ;
467
+ auto d = find_final_def ( e, index, sp, is, some ( id) ) ;
468
+ alt ( d) {
469
+ case ( def_wrap_expr_field ( ?ident) ) {
470
+ e. sess . span_err ( sp, ident + " is not a module or crate" ) ;
471
+ }
472
+ case ( _) {
473
+ }
474
+ }
461
475
let option. t[ def] target_def = some ( unwrap_def ( d) ) ;
462
476
ret @fold. respan [ ast. view_item_ ] ( sp, ast. view_item_import ( is, id,
463
477
target_def) ) ;
0 commit comments