Allow methods to call other methods in the same class

This commit is contained in:
Tim Chevalier 2012-03-23 23:08:41 -07:00
parent bebdfe8ce8
commit aae14e352a
4 changed files with 74 additions and 6 deletions

View File

@ -2241,7 +2241,16 @@ fn trans_var(cx: block, def: ast::def, id: ast::node_id, path: @ast::path)
}
_ { cx.sess().bug("unbound self param in class"); }
}
}
}
ast::def_class_method(parent, did) {
alt cx.fcx.llself {
some(slf) {
ret {env: self_env(slf.v, slf.t, none)
with lval_static_fn(cx, did, id)};
}
none { cx.sess().bug("unbound self param in class"); }
}
}
_ {
let loc = trans_local_var(cx, def);
ret lval_no_env(cx, loc.val, loc.kind);
@ -2266,7 +2275,11 @@ fn trans_rec_field_inner(bcx: block, val: ValueRef, ty: ty::t,
_ { bcx.tcx().sess.span_bug(sp, "trans_rec_field:\
base expr has non-record type"); }
};
let ix = option::get(ty::field_idx(field, fields));
let ix = alt ty::field_idx(field, fields) {
none { bcx.tcx().sess.span_bug(sp, #fmt("trans_rec_field:\
base expr doesn't appear to have a field named %s", field));}
some(i) { i }
};
let val = GEPi(bcx, val, [0, ix as int]);
ret {bcx: bcx, val: val, kind: owned};
}

View File

@ -0,0 +1,34 @@
mod kitties {
class cat {
priv {
let mutable meows : uint;
fn meow() {
#error("Meow");
meows += 1u;
if meows % 5u == 0u {
how_hungry += 1;
}
}
}
let how_hungry : int;
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
fn speak() { meow(); }
fn eat() -> bool {
if how_hungry > 0 {
#error("OM NOM NOM");
how_hungry -= 2;
ret true;
}
else {
#error("Not hungry!");
ret false;
}
}
}
}

View File

@ -0,0 +1,12 @@
// xfail-fast
// aux-build:cci_class_4.rs
use cci_class_4;
import cci_class_4::kitties::*;
fn main() {
let nyan = cat(0u, 2);
nyan.eat();
assert(!nyan.eat());
uint::range(1u, 10u, {|_i| nyan.speak(); });
assert(nyan.eat());
}

View File

@ -1,11 +1,10 @@
// xfail-test
class cat {
priv {
let mutable meows : uint;
fn meow() {
#error("Meow");
meows += 1;
if meows % 5 == 0 {
meows += 1u;
if meows % 5u == 0u {
how_hungry += 1;
}
}
@ -17,13 +16,23 @@ class cat {
fn speak() { meow(); }
fn eat() {
fn eat() -> bool {
if how_hungry > 0 {
#error("OM NOM NOM");
how_hungry -= 2;
ret true;
}
else {
#error("Not hungry!");
ret false;
}
}
}
fn main() {
let nyan = cat(0u, 2);
nyan.eat();
assert(!nyan.eat());
uint::range(1u, 10u, {|_i| nyan.speak(); });
assert(nyan.eat());
}