Skip to content

Commit 43b0446

Browse files
committed
---
yaml --- r: 2507 b: refs/heads/master c: ae26b77 h: refs/heads/master i: 2505: c7e9b19 2503: c05bf7e v: v3
1 parent 8de8721 commit 43b0446

File tree

5 files changed

+79
-74
lines changed

5 files changed

+79
-74
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: eb419fd8c78f907f1a5cd20f5e71009ba37ef7e9
2+
refs/heads/master: ae26b775b4ae4a1405effefb015ec44f0311c29f

trunk/src/comp/middle/resolve.rs

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ tag scope {
4444
tag import_state {
4545
todo(@ast::view_item, list[scope]);
4646
resolving(span);
47-
resolved(option::t[def] /* value */, option::t[def] /* type */);
47+
resolved(option::t[def] /* value */,
48+
option::t[def] /* type */,
49+
option::t[def] /* module */);
4850
}
4951

5052
type ext_hash = hashmap[tup(def_id,str,namespace),def];
@@ -97,6 +99,7 @@ tag dir { inside; outside; }
9799
tag namespace {
98100
ns_value;
99101
ns_type;
102+
ns_module;
100103
}
101104

102105
fn resolve_crate(session sess, @ast::crate crate) -> def_map {
@@ -159,7 +162,7 @@ fn resolve_imports(&env e) {
159162
case (todo(?item, ?sc)) {
160163
resolve_import(e, item, sc);
161164
}
162-
case (resolved(_, _)) {}
165+
case (resolved(_, _, _)) {}
163166
}
164167
}
165168
}
@@ -324,51 +327,45 @@ fn resolve_import(&env e, &@ast::view_item it, &list[scope] sc) {
324327
if (n_idents == 1u) {
325328
register(e, defid, it.span, end_id,
326329
lookup_in_scope(e, sc, it.span, end_id, ns_value),
327-
lookup_in_scope(e, sc, it.span, end_id, ns_type));
330+
lookup_in_scope(e, sc, it.span, end_id, ns_type),
331+
lookup_in_scope(e, sc, it.span, end_id, ns_module));
328332
} else {
329-
auto dcur = lookup_in_scope_strict(e, sc, it.span, ids.(0), ns_value);
333+
auto dcur = lookup_in_scope_strict(e, sc, it.span, ids.(0),
334+
ns_module);
330335
auto i = 1u;
331336
while (true) {
332-
if (!is_module(dcur)) {
333-
e.sess.span_err(it.span, ids.(i-1u) +
334-
" is not a module or crate");
335-
}
336337
if (i == n_idents - 1u) {
337338
register(e, defid, it.span, end_id,
338339
lookup_in_mod(e, dcur, end_id, ns_value, outside),
339-
lookup_in_mod(e, dcur, end_id, ns_type, outside));
340+
lookup_in_mod(e, dcur, end_id, ns_type, outside),
341+
lookup_in_mod(e, dcur, end_id, ns_module, outside));
340342
break;
341343
} else {
342344
dcur = lookup_in_mod_strict(e, dcur, it.span, ids.(i),
343-
ns_value, outside);
345+
ns_module, outside);
344346
i += 1u;
345347
}
346348
}
347349
}
348350

349351
fn register(&env e, def_id defid, &span sp, &ident id,
350-
&option::t[def] val, &option::t[def] typ) {
351-
if (option::is_none(val) && option::is_none(typ)) {
352+
&option::t[def] val, &option::t[def] typ,
353+
&option::t[def] md) {
354+
if (option::is_none(val) && option::is_none(typ) &&
355+
option::is_none(md)) {
352356
unresolved(e, sp, id, "import");
353357
}
354-
e.imports.insert(defid._1, resolved(val, typ));
358+
e.imports.insert(defid._1, resolved(val, typ, md));
355359
}
356360
}
357361

358362
// Utilities
359363

360-
fn is_module(def d) -> bool {
361-
alt (d) {
362-
case (ast::def_mod(_)) { ret true; }
363-
case (ast::def_native_mod(_)) { ret true; }
364-
case (_) { ret false; }
365-
}
366-
}
367-
368364
fn ns_name(namespace ns) -> str {
369365
alt (ns) {
370366
case (ns_type) { ret "typename"; }
371367
case (ns_value) { ret "name"; }
368+
case (ns_module) { ret "modulename"; }
372369
}
373370
}
374371

@@ -381,20 +378,14 @@ fn unresolved(&env e, &span sp, &ident id, &str kind) {
381378
fn lookup_path_strict(&env e, &list[scope] sc, &span sp, vec[ident] idents,
382379
namespace ns) -> def {
383380
auto n_idents = _vec::len(idents);
384-
auto dcur = lookup_in_scope_strict(e, sc, sp, idents.(0), ns);
381+
auto headns = if (n_idents == 1u) { ns } else { ns_module };
382+
auto dcur = lookup_in_scope_strict(e, sc, sp, idents.(0), headns);
385383
auto i = 1u;
386384
while (i < n_idents) {
387-
if (!is_module(dcur)) {
388-
e.sess.span_err(sp, idents.(i-1u) +
389-
" can't be dereferenced as a module");
390-
}
391-
dcur = lookup_in_mod_strict(e, dcur, sp, idents.(i), ns, outside);
385+
auto curns = if (n_idents == i + 1u) { ns } else { ns_module };
386+
dcur = lookup_in_mod_strict(e, dcur, sp, idents.(i), curns, outside);
392387
i += 1u;
393388
}
394-
if (is_module(dcur)) {
395-
e.sess.span_err(sp, _str::connect(idents, "::") +
396-
" is a module, not a " + ns_name(ns));
397-
}
398389
ret dcur;
399390
}
400391

@@ -566,29 +557,37 @@ fn lookup_in_pat(&ident id, &ast::pat pat) -> option::t[def] {
566557

567558
fn lookup_in_fn(&ident id, &ast::fn_decl decl, &vec[ast::ty_param] ty_params,
568559
namespace ns) -> option::t[def] {
569-
if (ns == ns_value) {
570-
for (ast::arg a in decl.inputs) {
571-
if (_str::eq(a.ident, id)) {
572-
ret some(ast::def_arg(a.id));
560+
alt (ns) {
561+
case (ns_value) {
562+
for (ast::arg a in decl.inputs) {
563+
if (_str::eq(a.ident, id)) {
564+
ret some(ast::def_arg(a.id));
565+
}
573566
}
567+
ret none[def];
574568
}
575-
ret none[def];
576-
} else {
577-
ret lookup_in_ty_params(id, ty_params);
569+
case (ns_type) {
570+
ret lookup_in_ty_params(id, ty_params);
571+
}
572+
case (_) { ret none[def]; }
578573
}
579574
}
580575

581576
fn lookup_in_obj(&ident id, &ast::_obj ob, &vec[ast::ty_param] ty_params,
582577
namespace ns) -> option::t[def] {
583-
if (ns == ns_value) {
584-
for (ast::obj_field f in ob.fields) {
585-
if (_str::eq(f.ident, id)) {
586-
ret some(ast::def_obj_field(f.id));
578+
alt (ns) {
579+
case (ns_value) {
580+
for (ast::obj_field f in ob.fields) {
581+
if (_str::eq(f.ident, id)) {
582+
ret some(ast::def_obj_field(f.id));
583+
}
587584
}
585+
ret none[def];
588586
}
589-
ret none[def];
590-
} else {
591-
ret lookup_in_ty_params(id, ty_params);
587+
case (ns_type) {
588+
ret lookup_in_ty_params(id, ty_params);
589+
}
590+
case (_) { ret none[def]; }
592591
}
593592
}
594593

@@ -611,7 +610,7 @@ fn lookup_in_block(&ident id, &ast::block_ b, namespace ns)
611610
if (_str::eq(name, id)) {
612611
ret some(ast::def_ty(defid));
613612
}
614-
} else {
613+
} else if (ns == ns_value) {
615614
for (ast::variant v in variants) {
616615
if (_str::eq(v.node.name, id)) {
617616
ret some(ast::def_variant(
@@ -623,7 +622,7 @@ fn lookup_in_block(&ident id, &ast::block_ b, namespace ns)
623622
case (_) {
624623
if (_str::eq(ast::item_ident(it), id)) {
625624
auto found = found_def_item(it, ns);
626-
if (!option::is_none(found)) { ret found; }
625+
if (!option::is_none(found)) {ret found;}
627626
}
628627
}
629628
}
@@ -645,10 +644,10 @@ fn found_def_item(@ast::item i, namespace ns) -> option::t[def] {
645644
if (ns == ns_value) { ret some(ast::def_fn(defid)); }
646645
}
647646
case (ast::item_mod(_, _, ?defid)) {
648-
ret some(ast::def_mod(defid));
647+
if (ns == ns_module) { ret some(ast::def_mod(defid)); }
649648
}
650649
case (ast::item_native_mod(_, _, ?defid)) {
651-
ret some(ast::def_native_mod(defid));
650+
if (ns == ns_module) { ret some(ast::def_native_mod(defid)); }
652651
}
653652
case (ast::item_ty(_, _, _, ?defid, _)) {
654653
if (ns == ns_type) { ret some(ast::def_ty(defid)); }
@@ -657,8 +656,11 @@ fn found_def_item(@ast::item i, namespace ns) -> option::t[def] {
657656
if (ns == ns_type) { ret some(ast::def_ty(defid)); }
658657
}
659658
case (ast::item_obj(_, _, _, ?odid, _)) {
660-
if (ns == ns_value) { ret some(ast::def_obj(odid.ctor)); }
661-
else { ret some(ast::def_obj(odid.ty)); }
659+
alt (ns) {
660+
case (ns_value) { ret some(ast::def_obj(odid.ctor)); }
661+
case (ns_type) { ret some(ast::def_obj(odid.ty)); }
662+
case (_) { }
663+
}
662664
}
663665
case (_) { }
664666
}
@@ -725,9 +727,10 @@ fn lookup_import(&env e, def_id defid, namespace ns) -> option::t[def] {
725727
case (resolving(?sp)) {
726728
e.sess.span_err(sp, "cyclic import");
727729
}
728-
case (resolved(?val, ?typ)) {
730+
case (resolved(?val, ?typ, ?md)) {
729731
ret alt (ns) { case (ns_value) { val }
730-
case (ns_type) { typ } };
732+
case (ns_type) { typ }
733+
case (ns_module) { md } };
731734
}
732735
}
733736
}
@@ -917,31 +920,30 @@ fn index_nmod(&ast::native_mod md) -> nmod_index {
917920

918921
// External lookups
919922

920-
// FIXME creader should handle multiple namespaces
921-
fn check_def_by_ns(def d, namespace ns) -> bool {
923+
fn ns_for_def(def d) -> namespace {
922924
ret alt (d) {
923-
case (ast::def_fn(?id)) { ns == ns_value }
924-
case (ast::def_obj(?id)) { ns == ns_value }
925-
case (ast::def_obj_field(?id)) { ns == ns_value }
926-
case (ast::def_mod(?id)) { true }
927-
case (ast::def_native_mod(?id)) { true }
928-
case (ast::def_const(?id)) { ns == ns_value }
929-
case (ast::def_arg(?id)) { ns == ns_value }
930-
case (ast::def_local(?id)) { ns == ns_value }
931-
case (ast::def_variant(_, ?id)) { ns == ns_value }
932-
case (ast::def_ty(?id)) { ns == ns_type }
933-
case (ast::def_binding(?id)) { ns == ns_type }
934-
case (ast::def_use(?id)) { true }
935-
case (ast::def_native_ty(?id)) { ns == ns_type }
936-
case (ast::def_native_fn(?id)) { ns == ns_value }
925+
case (ast::def_fn(?id)) { ns_value }
926+
case (ast::def_obj(?id)) { ns_value }
927+
case (ast::def_obj_field(?id)) { ns_value }
928+
case (ast::def_mod(?id)) { ns_module }
929+
case (ast::def_native_mod(?id)) { ns_module }
930+
case (ast::def_const(?id)) { ns_value }
931+
case (ast::def_arg(?id)) { ns_value }
932+
case (ast::def_local(?id)) { ns_value }
933+
case (ast::def_variant(_, ?id)) { ns_value }
934+
case (ast::def_ty(?id)) { ns_type }
935+
case (ast::def_binding(?id)) { ns_type }
936+
case (ast::def_use(?id)) { ns_module }
937+
case (ast::def_native_ty(?id)) { ns_type }
938+
case (ast::def_native_fn(?id)) { ns_value }
937939
};
938940
}
939941

940942
fn lookup_external(&env e, int cnum, vec[ident] ids, namespace ns)
941943
-> option::t[def] {
942944
for (def d in creader::lookup_defs(e.sess, cnum, ids)) {
943945
e.ext_map.insert(ast::def_id_of_def(d), ids);
944-
if (check_def_by_ns(d, ns)) { ret some(d); }
946+
if (ns == ns_for_def(d)) { ret some(d); }
945947
}
946948
ret none[def];
947949
}

trunk/src/test/compile-fail/bad-expr-path2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// error-pattern: is a module, not a
1+
// xfail-boot
2+
// xfail-stage0
3+
// error-pattern: unresolved name: a
24

35
mod m1 {
46
mod a {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// xfail-boot
2-
// error-pattern: unresolved name
2+
// error-pattern: unresolved modulename
33
import baz::zed::bar;
44
mod baz {
55
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// xfail-boot
2-
// error-pattern: main is not a module or crate
2+
// xfail-stage0
3+
// error-pattern: unresolved modulename
34
import main::bar;
45

56
fn main(vec[str] args) {

0 commit comments

Comments
 (0)