mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-12 06:53:05 +00:00
Allow methods to call other methods in the same class
This commit is contained in:
parent
bebdfe8ce8
commit
aae14e352a
@ -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};
|
||||
}
|
||||
|
34
src/test/auxiliary/cci_class_4.rs
Normal file
34
src/test/auxiliary/cci_class_4.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
12
src/test/run-pass/classes-cross-crate.rs
Normal file
12
src/test/run-pass/classes-cross-crate.rs
Normal 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());
|
||||
}
|
@ -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());
|
||||
}
|
Loading…
Reference in New Issue
Block a user