Skip to content

Commit 2daaaf9

Browse files
committed
---
yaml --- r: 11382 b: refs/heads/master c: 2299d20 h: refs/heads/master v: v3
1 parent 96d5930 commit 2daaaf9

File tree

7 files changed

+201
-29
lines changed

7 files changed

+201
-29
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 5837e1e809af6d783984d94ca27fe488823ecfe6
2+
refs/heads/master: 2299d204e40e565396daabc7b8d5141cdef52c8b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/comp/middle/resolve.rs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ enum dir { inside, outside, }
151151
// when looking up a variable name that's not yet in scope to check
152152
// if it's already bound to a enum.
153153
enum namespace { ns_val(ns_value_type), ns_type, ns_module, }
154-
enum ns_value_type { ns_a_enum, ns_any_value, }
154+
enum ns_value_type { ns_an_enum, ns_any_value, }
155155

156156
fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
157157
{def_map: def_map, exp_map: exp_map, impl_map: impl_map} {
@@ -469,7 +469,7 @@ fn resolve_names(e: @env, c: @ast::crate) {
469469
variable a refers to a nullary enum. */
470470
ast::pat_ident(p, none) {
471471
alt lookup_in_scope(*e, sc, p.span, path_to_ident(p),
472-
ns_val(ns_a_enum)) {
472+
ns_val(ns_an_enum)) {
473473
some(fnd@ast::def_variant(_,_)) {
474474
e.def_map.insert(pat.id, fnd);
475475
}
@@ -519,11 +519,16 @@ fn visit_item_with_scope(e: @env, i: @ast::item, sc: scopes, v: vt<scopes>) {
519519
}
520520
ast::item_class(tps, members, ctor_id, ctor_decl, ctor_block) {
521521
visit::visit_ty_params(tps, sc, v);
522-
let ctor_scope = cons(scope_fn_expr(ctor_decl, ctor_id, tps), @sc);
522+
let class_scope = cons(scope_item(i), @sc);
523+
/* visit the constructor... */
524+
visit_fn_with_scope(e, visit::fk_item_fn(i.ident, tps), ctor_decl,
525+
ctor_block, ctor_block.span, ctor_id,
526+
class_scope, v);
527+
/* visit the items */
523528
for cm in members {
524529
alt cm.node.decl {
525-
class_method(i) { visit_item_with_scope(e, i, ctor_scope, v); }
526-
_ { } // instance var -- nothing to do
530+
class_method(i) { visit_item_with_scope(e, i, class_scope, v); }
531+
instance_var(_,t,_,_) { v.visit_ty(t, class_scope, v); }
527532
}
528533
}
529534
}
@@ -622,10 +627,10 @@ fn visit_local_with_scope(e: @env, loc: @local, sc:scopes, v:vt<scopes>) {
622627
// to enum foo, or is it binding a new name foo?)
623628
alt loc.node.pat.node {
624629
pat_ident(an_ident,_) {
625-
// Be sure to pass ns_a_enum to lookup_in_scope so that
630+
// Be sure to pass ns_an_enum to lookup_in_scope so that
626631
// if this is a name that's being shadowed, we don't die
627632
alt lookup_in_scope(*e, sc, loc.span,
628-
path_to_ident(an_ident), ns_val(ns_a_enum)) {
633+
path_to_ident(an_ident), ns_val(ns_an_enum)) {
629634
some(ast::def_variant(enum_id,variant_id)) {
630635
// Declaration shadows a enum that's in scope.
631636
// That's an error.
@@ -804,7 +809,7 @@ fn ns_name(ns: namespace) -> str {
804809
ns_val(v) {
805810
alt (v) {
806811
ns_any_value { "name" }
807-
ns_a_enum { "enum" }
812+
ns_an_enum { "enum" }
808813
}
809814
}
810815
ns_module { "modulename" }
@@ -1000,6 +1005,21 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
10001005
ast::item_native_mod(m) {
10011006
ret lookup_in_local_native_mod(e, it.id, sp, name, ns);
10021007
}
1008+
ast::item_class(tps, members, ctor_id, _, _) {
1009+
if ns == ns_type {
1010+
ret lookup_in_ty_params(e, name, tps);
1011+
}
1012+
if ns == ns_val(ns_any_value) && name == it.ident {
1013+
ret some(ast::def_fn(local_def(ctor_id),
1014+
ast::impure_fn));
1015+
}
1016+
if ns == ns_val(ns_any_value) {
1017+
ret lookup_in_class(local_def(it.id),
1018+
members, name);
1019+
}
1020+
// FIXME: AST allows other items to appear in a class,
1021+
// but that might not be wise
1022+
}
10031023
_ { }
10041024
}
10051025
}
@@ -1071,7 +1091,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
10711091
/* If we were looking for a enum, at this point
10721092
we know it's bound to a non-enum value, and
10731093
we can return none instead of failing */
1074-
ns_a_enum { ret none; }
1094+
ns_an_enum { ret none; }
10751095
_ { "attempted dynamic environment-capture" }
10761096
}
10771097
}
@@ -1146,6 +1166,29 @@ fn lookup_in_fn(e: env, name: ident, decl: ast::fn_decl,
11461166
}
11471167
}
11481168

1169+
/*
1170+
FIXME: not sure about this code. maybe this should be handled
1171+
using the mod_index stuff
1172+
*/
1173+
fn lookup_in_class(parent_id: def_id,
1174+
members: [@class_item], name: ident)
1175+
-> option<def> {
1176+
for m in members {
1177+
alt m.node.decl {
1178+
instance_var(v_name,_,_,id) {
1179+
if v_name == name {
1180+
ret some(def_class_field(parent_id, local_def(id)));
1181+
}
1182+
}
1183+
class_method(i) {
1184+
if i.ident == name {
1185+
ret some(def_class_method(parent_id, local_def(i.id)));
1186+
}
1187+
}
1188+
}
1189+
}
1190+
ret none;
1191+
}
11491192

11501193
fn lookup_in_block(e: env, name: ident, sp: span, b: ast::blk_, pos: uint,
11511194
loc_pos: uint, ns: namespace) -> option<def> {
@@ -1430,7 +1473,7 @@ fn lookup_in_globs(e: env, globs: [glob_imp_def], sp: span, id: ident,
14301473
if vec::len(matches) == 0u {
14311474
ret none;
14321475
}
1433-
else if vec::len(matches) == 1u || ns == ns_val(ns_a_enum) {
1476+
else if vec::len(matches) == 1u || ns == ns_val(ns_an_enum) {
14341477
ret some(matches[0].def);
14351478
} else {
14361479
for match: glob_imp_def in matches {
@@ -1449,7 +1492,7 @@ fn lookup_glob_in_mod(e: env, info: @indexed_mod, sp: span, id: ident,
14491492
if !info.glob_imported_names.contains_key(id) {
14501493
info.glob_imported_names.insert(id, glob_resolving(sp));
14511494
// kludge
1452-
let val_ns = if wanted_ns == ns_val(ns_a_enum) { ns_val(ns_a_enum) }
1495+
let val_ns = if wanted_ns == ns_val(ns_an_enum) { ns_val(ns_an_enum) }
14531496
else { ns_val(ns_any_value) };
14541497
let globs = info.glob_imports;
14551498
let val = lookup_in_globs(e, globs, sp, id, val_ns, dr);
@@ -1615,7 +1658,7 @@ fn index_nmod(md: ast::native_mod) -> mod_index {
16151658
// External lookups
16161659
fn ns_for_def(d: def) -> namespace {
16171660
alt d {
1618-
ast::def_variant(_, _) { ns_val(ns_a_enum) }
1661+
ast::def_variant(_, _) { ns_val(ns_an_enum) }
16191662
ast::def_fn(_, _) | ast::def_self(_) |
16201663
ast::def_const(_) | ast::def_arg(_, _) | ast::def_local(_) |
16211664
ast::def_upvar(_, _, _) | ast::def_self(_) |
@@ -1632,7 +1675,7 @@ fn ns_for_def(d: def) -> namespace {
16321675
// a enum
16331676
fn ns_ok(wanted:namespace, actual:namespace) -> bool {
16341677
alt actual {
1635-
ns_val(ns_a_enum) {
1678+
ns_val(ns_an_enum) {
16361679
alt wanted {
16371680
ns_val(_) { true }
16381681
_ { false }

trunk/src/comp/middle/ty.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,21 @@ mod unify {
21832183
}
21842184
}
21852185
}
2186+
ty_class(expected_class, expected_tys) {
2187+
alt get(actual).struct {
2188+
ty_class(actual_class, actual_tys) {
2189+
if expected_class != actual_class {
2190+
ret ures_err(terr_mismatch);
2191+
}
2192+
ret unify_tps(cx, expected_tys, actual_tys, variance,
2193+
{|tps|
2194+
ures_ok(mk_class(cx.tcx, expected_class, tps))});
2195+
}
2196+
_ {
2197+
ret ures_err(terr_mismatch);
2198+
}
2199+
}
2200+
}
21862201
_ { cx.tcx.sess.bug("unify: unexpected type"); }
21872202
}
21882203
}
@@ -2478,13 +2493,11 @@ fn enum_variant_with_id(cx: ctxt, enum_id: ast::def_id,
24782493

24792494
// If the given item is in an external crate, looks up its type and adds it to
24802495
// the type cache. Returns the type parameters and type.
2496+
// a precondition (did.crate != ast::local_crate) would be nice
24812497
fn lookup_item_type(cx: ctxt, did: ast::def_id) -> ty_param_bounds_and_ty {
24822498
alt cx.tcache.find(did) {
24832499
some(tpt) { ret tpt; }
24842500
none {
2485-
/* where do things get added to the cache?
2486-
Have to add class members */
2487-
24882501
// The item is in this crate. The caller should have added it to the
24892502
// type cache already
24902503
assert did.crate != ast::local_crate;

0 commit comments

Comments
 (0)