Skip to content

Commit d7be25e

Browse files
committed
---
yaml --- r: 1273 b: refs/heads/master c: 45fd05a h: refs/heads/master i: 1271: b822dc2 v: v3
1 parent 8624a91 commit d7be25e

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
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: 61379af1a979aa7813aa9470e2558953eef166b6
2+
refs/heads/master: 45fd05ac4293e9cb9bbcf0ec89539f54d0de6059

trunk/src/comp/middle/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ fn expr_ty(@ast.expr expr) -> @t {
646646
case (ast.expr_lit(_, ?ann)) { ret ann_to_type(ann); }
647647
case (ast.expr_cast(_, _, ?ann)) { ret ann_to_type(ann); }
648648
case (ast.expr_if(_, _, _, ?ann)) { ret ann_to_type(ann); }
649+
case (ast.expr_for(_, _, _, ?ann)) { ret ann_to_type(ann); }
649650
case (ast.expr_while(_, _, ?ann)) { ret ann_to_type(ann); }
650651
case (ast.expr_do_while(_, _, ?ann)) { ret ann_to_type(ann); }
651652
case (ast.expr_alt(_, _, ?ann)) { ret ann_to_type(ann); }

trunk/src/comp/middle/typeck.rs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ fn demand_expr(&@fn_ctxt fcx, @ty.t expected, @ast.expr e) -> @ast.expr {
721721
}
722722
e_1 = ast.expr_if(cond, then_1, else_1, ast.ann_type(t));
723723
}
724+
case (ast.expr_for(?decl, ?seq, ?bloc, ?ann)) {
725+
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
726+
e_1 = ast.expr_for(decl, seq, bloc, ast.ann_type(t));
727+
}
724728
case (ast.expr_while(?cond, ?bloc, ?ann)) {
725729
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
726730
e_1 = ast.expr_while(cond, bloc, ast.ann_type(t));
@@ -1055,6 +1059,20 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
10551059
ast.ann_type(elsopt_t)));
10561060
}
10571061

1062+
case (ast.expr_for(?decl, ?seq, ?body, _)) {
1063+
auto decl_1 = check_decl_local(fcx, decl);
1064+
auto seq_1 = check_expr(fcx, seq);
1065+
auto body_1 = check_block(fcx, body);
1066+
1067+
// FIXME: enforce that the type of the decl is the element type
1068+
// of the seq.
1069+
1070+
auto ann = ast.ann_type(plain_ty(ty.ty_nil));
1071+
ret @fold.respan[ast.expr_](expr.span,
1072+
ast.expr_for(decl_1, seq_1,
1073+
body_1, ann));
1074+
}
1075+
10581076
case (ast.expr_while(?cond, ?body, _)) {
10591077
auto cond_0 = check_expr(fcx, cond);
10601078
auto cond_1 = demand_expr(fcx, plain_ty(ty.ty_bool), cond_0);
@@ -1413,40 +1431,47 @@ fn next_ty_var(@crate_ctxt ccx) -> @ty.t {
14131431
ret t;
14141432
}
14151433

1434+
fn check_decl_local(&@fn_ctxt fcx, &@ast.decl decl) -> @ast.decl {
1435+
alt (decl.node) {
1436+
case (ast.decl_local(?local)) {
1437+
1438+
auto local_ty;
1439+
alt (local.ty) {
1440+
case (none[@ast.ty]) {
1441+
// Auto slot. Assign a ty_var.
1442+
local_ty = next_ty_var(fcx.ccx);
1443+
}
1444+
1445+
case (some[@ast.ty](?ast_ty)) {
1446+
local_ty = ast_ty_to_ty_crate(fcx.ccx, ast_ty);
1447+
}
1448+
}
1449+
fcx.locals.insert(local.id, local_ty);
1450+
1451+
auto rhs_ty = local_ty;
1452+
auto init = local.init;
1453+
alt (local.init) {
1454+
case (some[@ast.expr](?expr)) {
1455+
auto expr_0 = check_expr(fcx, expr);
1456+
auto lty = plain_ty(ty.ty_local(local.id));
1457+
auto expr_1 = demand_expr(fcx, lty, expr_0);
1458+
init = some[@ast.expr](expr_1);
1459+
}
1460+
case (_) { /* fall through */ }
1461+
}
1462+
auto local_1 = @rec(init = init with *local);
1463+
ret @rec(node=ast.decl_local(local_1)
1464+
with *decl);
1465+
}
1466+
}
1467+
}
1468+
14161469
fn check_stmt(&@fn_ctxt fcx, &@ast.stmt stmt) -> @ast.stmt {
14171470
alt (stmt.node) {
14181471
case (ast.stmt_decl(?decl)) {
14191472
alt (decl.node) {
1420-
case (ast.decl_local(?local)) {
1421-
1422-
auto local_ty;
1423-
alt (local.ty) {
1424-
case (none[@ast.ty]) {
1425-
// Auto slot. Assign a ty_var.
1426-
local_ty = next_ty_var(fcx.ccx);
1427-
}
1428-
1429-
case (some[@ast.ty](?ast_ty)) {
1430-
local_ty = ast_ty_to_ty_crate(fcx.ccx, ast_ty);
1431-
}
1432-
}
1433-
fcx.locals.insert(local.id, local_ty);
1434-
1435-
auto rhs_ty = local_ty;
1436-
auto init = local.init;
1437-
alt (local.init) {
1438-
case (some[@ast.expr](?expr)) {
1439-
auto expr_0 = check_expr(fcx, expr);
1440-
auto lty = plain_ty(ty.ty_local(local.id));
1441-
auto expr_1 = demand_expr(fcx, lty, expr_0);
1442-
init = some[@ast.expr](expr_1);
1443-
}
1444-
case (_) { /* fall through */ }
1445-
}
1446-
1447-
auto local_1 = @rec(init = init with *local);
1448-
auto decl_1 = @rec(node=ast.decl_local(local_1)
1449-
with *decl);
1473+
case (ast.decl_local(_)) {
1474+
auto decl_1 = check_decl_local(fcx, decl);
14501475
ret @fold.respan[ast.stmt_](stmt.span,
14511476
ast.stmt_decl(decl_1));
14521477
}

0 commit comments

Comments
 (0)