Fix resolve bug that made nested classes not work

It wasn't possible to refer to the constructor for a class nested inside
an item from the class's outer scope. Fixed.
This commit is contained in:
Tim Chevalier 2012-06-19 12:00:09 -07:00
parent 6db7843f46
commit 76d6120e52
2 changed files with 32 additions and 18 deletions

View File

@ -573,7 +573,6 @@ fn visit_item_with_scope(e: @env, i: @ast::item,
}
ast::item_class(tps, ifaces, members, ctor, m_dtor, _) {
v.visit_ty_params(tps, sc, v);
// Can maybe skip this now that we require self on class fields
let class_scope = @cons(scope_item(i), sc);
/* visit the constructor... */
let ctor_scope = @cons(scope_method(ctor.node.self_id, tps),
@ -1061,7 +1060,7 @@ fn lookup_in_scope(e: env, &&sc: scopes, sp: span, name: ident, ns: namespace,
}
ast::item_class(tps, _, members, ctor, _, _) {
if ns == ns_type {
ret lookup_in_ty_params(e, name, tps);
ret lookup_in_ty_params(e, name, tps);
}
if ns == ns_val && name == it.ident {
ret some(ast::def_fn(local_def(ctor.node.id),
@ -1317,13 +1316,14 @@ fn found_def_item(i: @ast::item, ns: namespace) -> option<def> {
alt i.node {
ast::item_const(*) {
if ns == ns_val {
ret some(ast::def_const(local_def(i.id))); }
}
ast::item_fn(decl, _, _) {
if ns == ns_val {
ret some(ast::def_fn(local_def(i.id), decl.purity));
ret some(ast::def_const(local_def(i.id)));
}
}
ast::item_fn(decl, _, _) {
if ns == ns_val {
ret some(ast::def_fn(local_def(i.id), decl.purity));
}
}
ast::item_mod(_) {
if ns == ns_module { ret some(ast::def_mod(local_def(i.id))); }
}
@ -1342,9 +1342,16 @@ fn found_def_item(i: @ast::item, ns: namespace) -> option<def> {
_ { }
}
}
ast::item_class(*) {
if ns == ns_type {
ret some(ast::def_class(local_def(i.id)));
ast::item_class(_, _, _members, ct, _, _) {
alt ns {
ns_type {
ret some(ast::def_class(local_def(i.id)));
}
ns_val {
ret some(ast::def_fn(local_def(ct.node.id),
ast::impure_fn));
}
ns_module { }
}
}
ast::item_impl(*) { /* ??? */ }
@ -1653,14 +1660,6 @@ fn index_mod(md: ast::_mod) -> mod_index {
ast::item_class(tps, _, items, ctor, _, _) {
// add the class name itself
add_to_index(index, it.ident, mie_item(it));
// add the constructor decl
add_to_index(index, it.ident,
mie_item(@{ident: it.ident, attrs: [],
id: ctor.node.id,
node:
item_fn(ctor.node.dec, tps, ctor.node.body),
vis: ast::public,
span: ctor.node.body.span}));
}
}
}

View File

@ -0,0 +1,15 @@
fn main() {
class b {
let i: int;
fn do_stuff() -> int { ret 37; }
new(i:int) { self.i = i; }
}
// fn b(x:int) -> int { fail; }
let z = b(42);
assert(z.i == 42);
assert(z.do_stuff() == 37);
}