diff options
| author | Brian Anderson <[email protected]> | 2011-03-12 20:44:09 -0500 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-03-14 16:41:46 -0700 |
| commit | 644d8b95da51aeded917e0d8c9e7debb4cf1c64c (patch) | |
| tree | a5799d5be57ea07a7601a756bd3ec20e9e251c21 /src | |
| parent | Add folding and type checking for ports and chans (diff) | |
| download | rust-644d8b95da51aeded917e0d8c9e7debb4cf1c64c.tar.xz rust-644d8b95da51aeded917e0d8c9e7debb4cf1c64c.zip | |
Factor out expression checking for forms that look like assignment
Diffstat (limited to 'src')
| -rw-r--r-- | src/comp/middle/typeck.rs | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 78524c2d..f44292d5 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1644,6 +1644,22 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { ret tup(f_1, args_1); } + // A generic function for checking expressions that have a form + // similar to assignment. + fn check_assignment_like(&@fn_ctxt fcx, @ast.expr lhs, @ast.expr rhs) + -> tup(@ast.expr, @ast.expr, ast.ann) { + auto lhs_0 = check_expr(fcx, lhs); + auto rhs_0 = check_expr(fcx, rhs); + auto lhs_t0 = expr_ty(lhs_0); + auto rhs_t0 = expr_ty(rhs_0); + + auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0); + auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0); + + auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]); + ret tup(lhs_1, rhs_1, ann); + } + alt (expr.node) { case (ast.expr_lit(?lit, _)) { auto typ = check_lit(lit); @@ -1798,32 +1814,20 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { } case (ast.expr_assign(?lhs, ?rhs, _)) { - auto lhs_0 = check_expr(fcx, lhs); - auto rhs_0 = check_expr(fcx, rhs); - auto lhs_t0 = expr_ty(lhs_0); - auto rhs_t0 = expr_ty(rhs_0); - - auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0); - auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0); - - auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]); - ret @fold.respan[ast.expr_](expr.span, - ast.expr_assign(lhs_1, rhs_1, ann)); + auto checked = check_assignment_like(fcx, lhs, rhs); + auto newexpr = ast.expr_assign(checked._0, + checked._1, + checked._2); + ret @fold.respan[ast.expr_](expr.span, newexpr); } case (ast.expr_assign_op(?op, ?lhs, ?rhs, _)) { - auto lhs_0 = check_expr(fcx, lhs); - auto rhs_0 = check_expr(fcx, rhs); - auto lhs_t0 = expr_ty(lhs_0); - auto rhs_t0 = expr_ty(rhs_0); - - auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0); - auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0); - - auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]); - ret @fold.respan[ast.expr_](expr.span, - ast.expr_assign_op(op, lhs_1, rhs_1, - ann)); + auto checked = check_assignment_like(fcx, lhs, rhs); + auto newexpr = ast.expr_assign_op(op, + checked._0, + checked._1, + checked._2); + ret @fold.respan[ast.expr_](expr.span, newexpr); } case (ast.expr_if(?cond, ?thn, ?elifs, ?elsopt, _)) { |