Skip to content

Commit d4bbb4b

Browse files
lkupergraydon
authored andcommitted
---
yaml --- r: 2021 b: refs/heads/master c: 1092bbf h: refs/heads/master i: 2019: 5ae190f v: v3
1 parent daff545 commit d4bbb4b

File tree

10 files changed

+121
-42
lines changed

10 files changed

+121
-42
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 39774e88b42409c3a432a8426cfef253f67d7709
2+
refs/heads/master: 1092bbfff0cf932ef14e5b92cd54133571ca4727

trunk/src/comp/front/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ tag expr_ {
258258
expr_tup(vec[elt], ann);
259259
expr_rec(vec[field], option.t[@expr], ann);
260260
expr_call(@expr, vec[@expr], ann);
261-
expr_call_self(ident, vec[@expr], ann);
261+
expr_self_method(ident, ann);
262262
expr_bind(@expr, vec[option.t[@expr]], ann);
263263
expr_spawn(spawn_dom, option.t[str], @expr, vec[@expr], ann);
264264
expr_binary(binop, @expr, @expr, ann);

trunk/src/comp/front/parser.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -893,14 +893,14 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
893893
p.bump();
894894
expect(p, token.DOT);
895895
// The rest is a call expression.
896-
auto e = parse_ident(p);
896+
let @ast.expr f = parse_self_method(p);
897897
auto pf = parse_expr;
898898
auto es = parse_seq[@ast.expr](token.LPAREN,
899899
token.RPAREN,
900900
some(token.COMMA),
901901
pf, p);
902902
hi = es.span;
903-
ex = ast.expr_call_self(e, es.node, ast.ann_none);
903+
ex = ast.expr_call(f, es.node, ast.ann_none);
904904
}
905905

906906
case (_) {
@@ -966,6 +966,13 @@ impure fn extend_expr_by_ident(parser p, span lo, span hi,
966966
ret @spanned(lo, hi, e_);
967967
}
968968

969+
impure fn parse_self_method(parser p) -> @ast.expr {
970+
auto lo = p.get_span();
971+
let ast.ident f_name = parse_ident(p);
972+
auto hi = p.get_span();
973+
ret @spanned(lo, hi, ast.expr_self_method(f_name, ast.ann_none));
974+
}
975+
969976
impure fn parse_dot_or_call_expr(parser p) -> @ast.expr {
970977
auto lo = p.get_span();
971978
auto e = parse_bottom_expr(p);
@@ -1634,7 +1641,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
16341641
case (ast.expr_tup(_,_)) { ret true; }
16351642
case (ast.expr_rec(_,_,_)) { ret true; }
16361643
case (ast.expr_call(_,_,_)) { ret true; }
1637-
case (ast.expr_call_self(_,_,_)){ ret true; }
1644+
case (ast.expr_self_method(_,_)){ ret false; }
16381645
case (ast.expr_binary(_,_,_,_)) { ret true; }
16391646
case (ast.expr_unary(_,_,_)) { ret true; }
16401647
case (ast.expr_lit(_,_)) { ret true; }

trunk/src/comp/middle/fold.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ type ast_fold[ENV] =
8989
ann a) -> @expr) fold_expr_call,
9090

9191
(fn(&ENV e, &span sp,
92-
ident id, vec[@expr] args,
93-
ann a) -> @expr) fold_expr_call_self,
92+
ident id, ann a) -> @expr) fold_expr_self_method,
9493

9594
(fn(&ENV e, &span sp,
9695
@expr f, vec[option.t[@expr]] args,
@@ -569,9 +568,8 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
569568
ret fld.fold_expr_call(env_, e.span, ff, aargs, t);
570569
}
571570

572-
case (ast.expr_call_self(?ident, ?args, ?t)) {
573-
auto aargs = fold_exprs(env_, fld, args);
574-
ret fld.fold_expr_call_self(env_, e.span, ident, aargs, t);
571+
case (ast.expr_self_method(?ident, ?t)) {
572+
ret fld.fold_expr_self_method(env_, e.span, ident, t);
575573
}
576574

577575
case (ast.expr_bind(?f, ?args_opt, ?t)) {
@@ -1187,9 +1185,9 @@ fn identity_fold_expr_call[ENV](&ENV env, &span sp, @expr f,
11871185
ret @respan(sp, ast.expr_call(f, args, a));
11881186
}
11891187

1190-
fn identity_fold_expr_call_self[ENV](&ENV env, &span sp, ident id,
1191-
vec[@expr] args, ann a) -> @expr {
1192-
ret @respan(sp, ast.expr_call_self(id, args, a));
1188+
fn identity_fold_expr_self_method[ENV](&ENV env, &span sp, ident id,
1189+
ann a) -> @expr {
1190+
ret @respan(sp, ast.expr_self_method(id, a));
11931191
}
11941192

11951193
fn identity_fold_expr_bind[ENV](&ENV env, &span sp, @expr f,
@@ -1601,8 +1599,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
16011599
fold_expr_tup = bind identity_fold_expr_tup[ENV](_,_,_,_),
16021600
fold_expr_rec = bind identity_fold_expr_rec[ENV](_,_,_,_,_),
16031601
fold_expr_call = bind identity_fold_expr_call[ENV](_,_,_,_,_),
1604-
fold_expr_call_self
1605-
= bind identity_fold_expr_call_self[ENV](_,_,_,_,_),
1602+
fold_expr_self_method
1603+
= bind identity_fold_expr_self_method[ENV](_,_,_,_),
16061604
fold_expr_bind = bind identity_fold_expr_bind[ENV](_,_,_,_,_),
16071605
fold_expr_spawn = bind identity_fold_expr_spawn[ENV](_,_,_,_,_,_,_),
16081606
fold_expr_binary = bind identity_fold_expr_binary[ENV](_,_,_,_,_,_),

trunk/src/comp/middle/trans.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -3911,10 +3911,7 @@ fn trans_lval(@block_ctxt cx, @ast.expr e) -> lval_result {
39113911
C_int(abi.box_rc_field_body)));
39123912
ret lval_mem(sub.bcx, val);
39133913
}
3914-
3915-
// Kind of bizarre to pass an *entire* self-call here...but let's try
3916-
// it
3917-
case (ast.expr_call_self(?ident, _, ?ann)) {
3914+
case (ast.expr_self_method(?ident, ?ann)) {
39183915
alt (cx.fcx.llself) {
39193916
case (some[self_vt](?s_vt)) {
39203917
auto r = s_vt.v;
@@ -4765,11 +4762,6 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
47654762
ret trans_call(cx, f, none[ValueRef], args, ann);
47664763
}
47674764

4768-
case (ast.expr_call_self(?ident, ?args, ?ann)) {
4769-
// A weird hack to make self-calls work.
4770-
ret trans_call(cx, e, none[ValueRef], args, ann);
4771-
}
4772-
47734765
case (ast.expr_cast(?e, _, ?ann)) {
47744766
ret trans_cast(cx, e, ann);
47754767
}

trunk/src/comp/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ fn expr_ty(@ast.expr expr) -> @t {
764764
case (ast.expr_rec(_, _, ?ann)) { ret ann_to_type(ann); }
765765
case (ast.expr_bind(_, _, ?ann)) { ret ann_to_type(ann); }
766766
case (ast.expr_call(_, _, ?ann)) { ret ann_to_type(ann); }
767-
case (ast.expr_call_self(_, _, ?ann)) { ret ann_to_type(ann); }
767+
case (ast.expr_self_method(_, ?ann)) { ret ann_to_type(ann); }
768768
case (ast.expr_spawn(_, _, _, _, ?ann))
769769
{ ret ann_to_type(ann); }
770770
case (ast.expr_binary(_, _, _, ?ann)) { ret ann_to_type(ann); }

trunk/src/comp/middle/typeck.rs

+48-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import std.option;
3434
import std.option.none;
3535
import std.option.some;
3636

37+
import pretty.pprust;
38+
3739
import util.typestate_ann.ts_ann;
3840

3941
type ty_table = hashmap[ast.def_id, @ty.t];
@@ -49,6 +51,7 @@ type crate_ctxt = rec(session.session sess,
4951
ty.type_cache type_cache,
5052
@ty_item_table item_items,
5153
vec[ast.obj_field] obj_fields,
54+
option.t[ast.def_id] this_obj,
5255
mutable int next_var_id);
5356

5457
type fn_ctxt = rec(@ty.t ret_ty,
@@ -1242,10 +1245,9 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e,
12421245
ann_to_type(ann), adk);
12431246
e_1 = ast.expr_call(sube, es, triv_ann(t));
12441247
}
1245-
case (ast.expr_call_self(?sube, ?es, ?ann)) {
1246-
auto t = demand_full(fcx, e.span, expected,
1247-
ann_to_type(ann), adk);
1248-
e_1 = ast.expr_call_self(sube, es, triv_ann(t));
1248+
case (ast.expr_self_method(?id, ?ann)) {
1249+
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
1250+
e_1 = ast.expr_self_method(id, triv_ann(t));
12491251
}
12501252
case (ast.expr_binary(?bop, ?lhs, ?rhs, ?ann)) {
12511253
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
@@ -1572,6 +1574,8 @@ fn check_pat(&@fn_ctxt fcx, @ast.pat pat) -> @ast.pat {
15721574
}
15731575

15741576
fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
1577+
// log "typechecking expr " + pretty.pprust.expr_to_str(expr);
1578+
15751579
// A generic function to factor out common logic from call and bind
15761580
// expressions.
15771581
fn check_call_or_bind(&@fn_ctxt fcx, &@ast.expr f,
@@ -2125,16 +2129,44 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
21252129
ast.expr_call(f_1, args_1, ann));
21262130
}
21272131

2128-
case (ast.expr_call_self(?ident, ?args, _)) {
2129-
// FIXME: What's to check here?
2132+
case (ast.expr_self_method(?id, _)) {
2133+
auto t = plain_ty(ty.ty_nil);
2134+
let @ty.t this_obj_ty;
2135+
2136+
// Grab the type of the current object
2137+
auto this_obj_id = fcx.ccx.this_obj;
2138+
alt (this_obj_id) {
2139+
case (some[ast.def_id](?def_id)) {
2140+
auto this_obj_tpt = fcx.ccx.type_cache.find(def_id);
2141+
alt (this_obj_tpt) {
2142+
case (some[ty_params_opt_and_ty](?tpt)) {
2143+
this_obj_ty = tpt._1;
2144+
}
2145+
case (_) { fail; }
2146+
}
2147+
}
2148+
case (_) { fail; }
2149+
}
2150+
21302151

2131-
// FIXME: These two lines are ripped off from the expr_call case;
2132-
// what should they be really?
2133-
auto rt_1 = plain_ty(ty.ty_nil);
2134-
auto ann = triv_ann(rt_1);
2152+
// Grab this method's type out of the current object type
2153+
2154+
// this_obj_ty is an @ty.t
2155+
alt (this_obj_ty.struct) {
2156+
case (ty.ty_obj(?methods)) {
2157+
for (ty.method method in methods) {
2158+
if (method.ident == id) {
2159+
t = ty.method_ty_to_fn_ty(method);
2160+
}
2161+
}
2162+
}
2163+
case (_) { fail; }
2164+
}
2165+
2166+
auto ann = triv_ann(t);
21352167

21362168
ret @fold.respan[ast.expr_](expr.span,
2137-
ast.expr_call_self(ident, args, ann));
2169+
ast.expr_self_method(id, ann));
21382170
}
21392171

21402172
case (ast.expr_spawn(?dom, ?name, ?f, ?args, _)) {
@@ -2596,8 +2628,10 @@ fn check_item_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f,
25962628

25972629
fn update_obj_fields(&@crate_ctxt ccx, @ast.item i) -> @crate_ctxt {
25982630
alt (i.node) {
2599-
case (ast.item_obj(_, ?ob, _, _, _)) {
2600-
ret @rec(obj_fields = ob.fields with *ccx);
2631+
case (ast.item_obj(_, ?ob, _, ?obj_def_ids, _)) {
2632+
let ast.def_id di = obj_def_ids.ty;
2633+
ret @rec(obj_fields = ob.fields,
2634+
this_obj = some[ast.def_id](di) with *ccx);
26012635
}
26022636
case (_) {
26032637
}
@@ -2617,6 +2651,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> typecheck_result {
26172651
type_cache=result._1,
26182652
item_items=result._2,
26192653
obj_fields=fields,
2654+
this_obj=none[ast.def_id],
26202655
mutable next_var_id=0);
26212656

26222657
auto fld = fold.new_identity_fold[@crate_ctxt]();

trunk/src/comp/pretty/pprust.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,9 @@ impure fn print_expr(ps s, &@ast.expr expr) {
454454
commasep_exprs(s, args);
455455
pclose(s);
456456
}
457-
case (ast.expr_call_self(?ident,?args,_)) {
457+
case (ast.expr_self_method(?ident,_)) {
458458
wrd(s.s, "self.");
459459
print_ident(s, ident);
460-
popen(s);
461-
commasep_exprs(s, args);
462-
pclose(s);
463460
}
464461
case (ast.expr_bind(?func,?args,_)) {
465462
impure fn print_opt(ps s, &option.t[@ast.expr] expr) {

trunk/src/test/run-pass/obj-self-2.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// xfail-boot
2+
fn main() {
3+
4+
obj foo() {
5+
impure fn m1(mutable int i) {
6+
i += 1;
7+
log "hi!";
8+
}
9+
impure fn m2(mutable int i) {
10+
i += 1;
11+
self.m1(i);
12+
}
13+
}
14+
15+
auto a = foo();
16+
let int i = 0;
17+
a.m1(i);
18+
a.m2(i);
19+
}

trunk/src/test/run-pass/obj-self-3.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// xfail-boot
2+
fn main() {
3+
4+
obj foo() {
5+
impure fn m1(mutable int i) -> int {
6+
i += 1;
7+
ret i;
8+
}
9+
impure fn m2(mutable int i) -> int {
10+
ret self.m1(i);
11+
}
12+
impure fn m3(mutable int i) -> int {
13+
i += 1;
14+
ret self.m1(i);
15+
}
16+
}
17+
18+
auto a = foo();
19+
let int i = 0;
20+
21+
// output should be: 0, 1, 2, 4
22+
log i;
23+
i = a.m1(i);
24+
log i;
25+
i = a.m2(i);
26+
log i;
27+
i = a.m3(i);
28+
log i;
29+
}
30+
31+

0 commit comments

Comments
 (0)