@@ -721,6 +721,10 @@ fn demand_expr(&@fn_ctxt fcx, @ty.t expected, @ast.expr e) -> @ast.expr {
721
721
}
722
722
e_1 = ast. expr_if( cond, then_1, else_1, ast. ann_type( t) ) ;
723
723
}
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
+ }
724
728
case ( ast. expr_while( ?cond, ?bloc, ?ann) ) {
725
729
auto t = demand( fcx, e. span, expected, ann_to_type( ann) ) ;
726
730
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 {
1055
1059
ast. ann_type( elsopt_t) ) ) ;
1056
1060
}
1057
1061
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
+
1058
1076
case ( ast. expr_while( ?cond, ?body, _) ) {
1059
1077
auto cond_0 = check_expr( fcx, cond) ;
1060
1078
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 {
1413
1431
ret t;
1414
1432
}
1415
1433
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
+
1416
1469
fn check_stmt( & @fn_ctxt fcx, & @ast. stmt stmt) -> @ast. stmt {
1417
1470
alt ( stmt. node) {
1418
1471
case ( ast. stmt_decl( ?decl) ) {
1419
1472
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) ;
1450
1475
ret @fold. respan[ ast. stmt_] ( stmt. span,
1451
1476
ast. stmt_decl( decl_1) ) ;
1452
1477
}
0 commit comments