Skip to content

Commit 23b96af

Browse files
committed
---
yaml --- r: 1326 b: refs/heads/master c: 3fedb18 h: refs/heads/master v: v3
1 parent 1337340 commit 23b96af

File tree

8 files changed

+99
-79
lines changed

8 files changed

+99
-79
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: ef50d0e668635824cc5ed86aaa385dd6f3181b53
2+
refs/heads/master: 3fedb18c0af0bd9fa5e4973936003c0b57e4d3e8

trunk/src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ TEST_XFAILS_RUSTC := $(filter-out \
447447
div-mod.rs \
448448
drop-bind-thunk-args.rs \
449449
drop-on-ret.rs \
450+
else-if.rs \
450451
fact.rs \
451452
fn-lval.rs \
452453
fun-call-variants.rs \

trunk/src/comp/front/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ tag expr_ {
149149
expr_unary(unop, @expr, ann);
150150
expr_lit(@lit, ann);
151151
expr_cast(@expr, @ty, ann);
152-
expr_if(@expr, block, option.t[block], ann);
152+
expr_if(@expr, block, option.t[@expr], ann);
153153
expr_while(@expr, block, ann);
154154
expr_for(@decl, @expr, block, ann);
155155
expr_do_while(block, @expr, ann);

trunk/src/comp/front/parser.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -891,20 +891,33 @@ impure fn parse_if_expr(parser p) -> @ast.expr {
891891
auto cond = parse_expr(p);
892892
expect(p, token.RPAREN);
893893
auto thn = parse_block(p);
894-
let option.t[ast.block] els = none[ast.block];
894+
let option.t[@ast.expr] els = none[@ast.expr];
895895
hi = thn.span;
896896
alt (p.peek()) {
897897
case (token.ELSE) {
898-
p.bump();
899-
auto eblk = parse_block(p);
900-
els = some(eblk);
901-
hi = eblk.span;
898+
auto elexpr = parse_else_expr(p);
899+
els = some(elexpr);
900+
hi = elexpr.span;
902901
}
903902
case (_) { /* fall through */ }
904903
}
905904
ret @spanned(lo, hi, ast.expr_if(cond, thn, els, ast.ann_none));
906905
}
907906

907+
impure fn parse_else_expr(parser p) -> @ast.expr {
908+
expect(p, token.ELSE);
909+
alt (p.peek()) {
910+
case (token.IF) {
911+
ret parse_if_expr(p);
912+
}
913+
case (_) {
914+
auto blk = parse_block(p);
915+
ret @spanned(blk.span, blk.span,
916+
ast.expr_block(blk, ast.ann_none));
917+
}
918+
}
919+
}
920+
908921
impure fn parse_head_local(parser p) -> @ast.decl {
909922
auto lo = p.get_span();
910923
let @ast.local local;

trunk/src/comp/middle/fold.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type ast_fold[ENV] =
100100

101101
(fn(&ENV e, &span sp,
102102
@expr cond, &block thn,
103-
&option.t[block] els,
103+
&option.t[@expr] els,
104104
ann a) -> @expr) fold_expr_if,
105105

106106
(fn(&ENV e, &span sp,
@@ -504,10 +504,10 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
504504
case (ast.expr_if(?cnd, ?thn, ?els, ?t)) {
505505
auto ccnd = fold_expr(env_, fld, cnd);
506506
auto tthn = fold_block(env_, fld, thn);
507-
auto eels = none[block];
507+
auto eels = none[@expr];
508508
alt (els) {
509-
case (some[block](?b)) {
510-
eels = some(fold_block(env_, fld, b));
509+
case (some[@expr](?e)) {
510+
eels = some(fold_expr(env_, fld, e));
511511
}
512512
case (_) { /* fall through */ }
513513
}
@@ -961,7 +961,7 @@ fn identity_fold_expr_cast[ENV](&ENV env, &span sp, @ast.expr e,
961961

962962
fn identity_fold_expr_if[ENV](&ENV env, &span sp,
963963
@expr cond, &block thn,
964-
&option.t[block] els, ann a) -> @expr {
964+
&option.t[@expr] els, ann a) -> @expr {
965965
ret @respan(sp, ast.expr_if(cond, thn, els, a));
966966
}
967967

trunk/src/comp/middle/trans.rs

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ fn get_tydesc(&@block_ctxt cx, @ty.t t) -> result {
997997
vec(p2i(bcx.fcx.ccx.crate_ptr),
998998
sz.val,
999999
align.val,
1000-
C_int((1u + n_params) as int),
1000+
C_int(n_params as int),
10011001
bcx.build.PtrToInt(tydescs, T_int())));
10021002

10031003
ret res(v.bcx, v.bcx.build.IntToPtr(v.val, T_ptr(T_tydesc())));
@@ -1931,7 +1931,7 @@ fn join_results(@block_ctxt parent_cx,
19311931
}
19321932

19331933
fn trans_if(@block_ctxt cx, @ast.expr cond,
1934-
&ast.block thn, &option.t[ast.block] els) -> result {
1934+
&ast.block thn, &option.t[@ast.expr] els) -> result {
19351935

19361936
auto cond_res = trans_expr(cx, cond);
19371937

@@ -1942,8 +1942,19 @@ fn trans_if(@block_ctxt cx, @ast.expr cond,
19421942
auto else_res = res(else_cx, C_nil());
19431943

19441944
alt (els) {
1945-
case (some[ast.block](?eblk)) {
1946-
else_res = trans_block(else_cx, eblk);
1945+
case (some[@ast.expr](?elexpr)) {
1946+
// FIXME: Shouldn't need to unwrap the block here,
1947+
// instead just use 'else_res = trans_expr(else_cx, elexpr)',
1948+
// but either a) trans_expr doesn't handle expr_block
1949+
// correctly or b) I have no idea what I'm doing...
1950+
alt (elexpr.node) {
1951+
case (ast.expr_if(_, _, _, _)) {
1952+
else_res = trans_expr(else_cx, elexpr);
1953+
}
1954+
case (ast.expr_block(?b, _)) {
1955+
else_res = trans_block(else_cx, b);
1956+
}
1957+
}
19471958
}
19481959
case (_) { /* fall through */ }
19491960
}
@@ -2193,34 +2204,6 @@ fn lval_val(@block_ctxt cx, ValueRef val) -> lval_result {
21932204
llobj=none[ValueRef]);
21942205
}
21952206

2196-
fn lval_generic_fn(@block_ctxt cx,
2197-
ty.ty_params_and_ty tpt,
2198-
ast.def_id fn_id,
2199-
&ast.ann ann)
2200-
-> lval_result {
2201-
2202-
check (cx.fcx.ccx.fn_pairs.contains_key(fn_id));
2203-
auto lv = lval_val(cx, cx.fcx.ccx.fn_pairs.get(fn_id));
2204-
auto monoty = node_ann_type(cx.fcx.ccx, ann);
2205-
auto tys = ty.resolve_ty_params(tpt, monoty);
2206-
2207-
if (_vec.len[@ty.t](tys) != 0u) {
2208-
auto bcx = cx;
2209-
let vec[ValueRef] tydescs = vec();
2210-
for (@ty.t t in tys) {
2211-
auto td = get_tydesc(bcx, t);
2212-
bcx = td.bcx;
2213-
append[ValueRef](tydescs, td.val);
2214-
}
2215-
auto gen = rec( item_type = tpt._1,
2216-
tydescs = tydescs );
2217-
lv = rec(res = res(bcx, lv.res.val),
2218-
generic = some[generic_info](gen)
2219-
with lv);
2220-
}
2221-
ret lv;
2222-
}
2223-
22242207
fn trans_path(@block_ctxt cx, &ast.path p, &option.t[ast.def] dopt,
22252208
&ast.ann ann) -> lval_result {
22262209
alt (dopt) {
@@ -2243,33 +2226,39 @@ fn trans_path(@block_ctxt cx, &ast.path p, &option.t[ast.def] dopt,
22432226
ret lval_mem(cx, cx.fcx.llobjfields.get(did));
22442227
}
22452228
case (ast.def_fn(?did)) {
2246-
check (cx.fcx.ccx.items.contains_key(did));
2229+
check (cx.fcx.ccx.fn_pairs.contains_key(did));
2230+
check (cx.fcx.ccx.item_ids.contains_key(did));
2231+
22472232
auto fn_item = cx.fcx.ccx.items.get(did);
2248-
ret lval_generic_fn(cx, ty.item_ty(fn_item), did, ann);
2233+
auto lv = lval_val(cx, cx.fcx.ccx.fn_pairs.get(did));
2234+
auto monoty = node_ann_type(cx.fcx.ccx, ann);
2235+
auto tys = ty.resolve_ty_params(fn_item, monoty);
2236+
2237+
if (_vec.len[@ty.t](tys) != 0u) {
2238+
auto bcx = cx;
2239+
let vec[ValueRef] tydescs = vec();
2240+
for (@ty.t t in tys) {
2241+
auto td = get_tydesc(bcx, t);
2242+
bcx = td.bcx;
2243+
append[ValueRef](tydescs, td.val);
2244+
}
2245+
auto gen = rec( item_type = ty.item_ty(fn_item)._1,
2246+
tydescs = tydescs );
2247+
lv = rec(res = res(bcx, lv.res.val),
2248+
generic = some[generic_info](gen)
2249+
with lv);
2250+
}
2251+
2252+
ret lv;
22492253
}
22502254
case (ast.def_obj(?did)) {
2251-
check (cx.fcx.ccx.items.contains_key(did));
2252-
auto fn_item = cx.fcx.ccx.items.get(did);
2253-
ret lval_generic_fn(cx, ty.item_ty(fn_item), did, ann);
2255+
check (cx.fcx.ccx.fn_pairs.contains_key(did));
2256+
ret lval_val(cx, cx.fcx.ccx.fn_pairs.get(did));
22542257
}
22552258
case (ast.def_variant(?tid, ?vid)) {
22562259
check (cx.fcx.ccx.tags.contains_key(tid));
22572260
if (cx.fcx.ccx.fn_pairs.contains_key(vid)) {
2258-
check (cx.fcx.ccx.items.contains_key(tid));
2259-
auto tag_item = cx.fcx.ccx.items.get(tid);
2260-
auto params = ty.item_ty(tag_item)._0;
2261-
auto fty = ty.plain_ty(ty.ty_nil);
2262-
alt (tag_item.node) {
2263-
case (ast.item_tag(_, ?variants, _, _)) {
2264-
for (ast.variant v in variants) {
2265-
if (v.id == vid) {
2266-
fty = node_ann_type(cx.fcx.ccx,
2267-
v.ann);
2268-
}
2269-
}
2270-
}
2271-
}
2272-
ret lval_generic_fn(cx, tup(params, fty), vid, ann);
2261+
ret lval_val(cx, cx.fcx.ccx.fn_pairs.get(vid));
22732262
} else {
22742263
// Nullary variants are just scalar constants.
22752264
check (cx.fcx.ccx.item_ids.contains_key(vid));
@@ -3820,13 +3809,15 @@ fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
38203809

38213810
alt (i.node) {
38223811
case (ast.item_fn(?name, ?f, _, ?fid, ?ann)) {
3812+
// TODO: type-params
38233813
cx.items.insert(fid, i);
38243814
if (! cx.obj_methods.contains_key(fid)) {
38253815
decl_fn_and_pair(cx, "fn", name, ann, fid);
38263816
}
38273817
}
38283818

38293819
case (ast.item_obj(?name, ?ob, _, ?oid, ?ann)) {
3820+
// TODO: type-params
38303821
cx.items.insert(oid, i);
38313822
decl_fn_and_pair(cx, "obj_ctor", name, ann, oid);
38323823
for (@ast.method m in ob.methods) {

trunk/src/comp/middle/ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ fn is_fn_ty(@t fty) -> bool {
571571

572572
// Given an item, returns the associated type as well as a list of the IDs of
573573
// its type parameters.
574-
type ty_params_and_ty = tup(vec[ast.def_id], @t);
575-
fn item_ty(@ast.item it) -> ty_params_and_ty {
574+
fn item_ty(@ast.item it) -> tup(vec[ast.def_id], @t) {
576575
let vec[ast.ty_param] ty_params;
577576
auto result_ty;
578577
alt (it.node) {
@@ -1234,8 +1233,7 @@ fn type_err_to_str(&ty.type_err err) -> str {
12341233

12351234
// Type parameter resolution, used in translation
12361235

1237-
fn resolve_ty_params(ty_params_and_ty ty_params_and_polyty,
1238-
@t monoty) -> vec[@t] {
1236+
fn resolve_ty_params(@ast.item item, @t monoty) -> vec[@t] {
12391237
obj resolve_ty_params_handler(@hashmap[ast.def_id,@t] bindings) {
12401238
fn resolve_local(ast.def_id id) -> @t { log "resolve local"; fail; }
12411239
fn record_local(ast.def_id id, @t ty) { log "record local"; fail; }
@@ -1251,6 +1249,8 @@ fn resolve_ty_params(ty_params_and_ty ty_params_and_polyty,
12511249
}
12521250
}
12531251

1252+
auto ty_params_and_polyty = item_ty(item);
1253+
12541254
auto bindings = @new_def_hash[@t]();
12551255
auto handler = resolve_ty_params_handler(bindings);
12561256

trunk/src/comp/middle/typeck.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,10 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e,
870870
auto then_1 = demand_block(fcx, expected, then_0);
871871
auto else_1;
872872
alt (else_0) {
873-
case (none[ast.block]) { else_1 = none[ast.block]; }
874-
case (some[ast.block](?b_0)) {
875-
auto b_1 = demand_block(fcx, expected, b_0);
876-
else_1 = some[ast.block](b_1);
873+
case (none[@ast.expr]) { else_1 = none[@ast.expr]; }
874+
case (some[@ast.expr](?e_0)) {
875+
auto e_1 = demand_expr(fcx, expected, e_0);
876+
else_1 = some[@ast.expr](e_1);
877877
}
878878
}
879879
e_1 = ast.expr_if(cond, then_1, else_1, ast.ann_type(t));
@@ -1205,14 +1205,14 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
12051205
auto elsopt_1;
12061206
auto elsopt_t;
12071207
alt (elsopt) {
1208-
case (some[ast.block](?els)) {
1209-
auto els_0 = check_block(fcx, els);
1210-
auto els_1 = demand_block(fcx, thn_t, els_0);
1211-
elsopt_1 = some[ast.block](els_1);
1212-
elsopt_t = block_ty(els_1);
1208+
case (some[@ast.expr](?els)) {
1209+
auto els_0 = check_expr(fcx, els);
1210+
auto els_1 = demand_expr(fcx, thn_t, els_0);
1211+
elsopt_1 = some[@ast.expr](els_1);
1212+
elsopt_t = expr_ty(els_1);
12131213
}
1214-
case (none[ast.block]) {
1215-
elsopt_1 = none[ast.block];
1214+
case (none[@ast.expr]) {
1215+
elsopt_1 = none[@ast.expr];
12161216
elsopt_t = plain_ty(ty.ty_nil);
12171217
}
12181218
}
@@ -1308,6 +1308,21 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
13081308
ast.expr_alt(expr_1, arms_1, ann));
13091309
}
13101310

1311+
case (ast.expr_block(?b, _)) {
1312+
auto b_0 = check_block(fcx, b);
1313+
auto ann;
1314+
alt (b_0.node.expr) {
1315+
case (some[@ast.expr](?expr)) {
1316+
ann = ast.ann_type(expr_ty(expr));
1317+
}
1318+
case (none[@ast.expr]) {
1319+
ann = ast.ann_type(plain_ty(ty.ty_nil));
1320+
}
1321+
}
1322+
ret @fold.respan[ast.expr_](expr.span,
1323+
ast.expr_block(b_0, ann));
1324+
}
1325+
13111326
case (ast.expr_bind(?f, ?args, _)) {
13121327
auto f_0 = check_expr(fcx, f);
13131328
auto t_0 = expr_ty(f_0);

0 commit comments

Comments
 (0)