Skip to content

Commit 644d8b9

Browse files
brsongraydon
authored andcommitted
Factor out expression checking for forms that look like assignment
1 parent 7464237 commit 644d8b9

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/comp/middle/typeck.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,22 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
16441644
ret tup(f_1, args_1);
16451645
}
16461646

1647+
// A generic function for checking expressions that have a form
1648+
// similar to assignment.
1649+
fn check_assignment_like(&@fn_ctxt fcx, @ast.expr lhs, @ast.expr rhs)
1650+
-> tup(@ast.expr, @ast.expr, ast.ann) {
1651+
auto lhs_0 = check_expr(fcx, lhs);
1652+
auto rhs_0 = check_expr(fcx, rhs);
1653+
auto lhs_t0 = expr_ty(lhs_0);
1654+
auto rhs_t0 = expr_ty(rhs_0);
1655+
1656+
auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0);
1657+
auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0);
1658+
1659+
auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]);
1660+
ret tup(lhs_1, rhs_1, ann);
1661+
}
1662+
16471663
alt (expr.node) {
16481664
case (ast.expr_lit(?lit, _)) {
16491665
auto typ = check_lit(lit);
@@ -1798,32 +1814,20 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
17981814
}
17991815

18001816
case (ast.expr_assign(?lhs, ?rhs, _)) {
1801-
auto lhs_0 = check_expr(fcx, lhs);
1802-
auto rhs_0 = check_expr(fcx, rhs);
1803-
auto lhs_t0 = expr_ty(lhs_0);
1804-
auto rhs_t0 = expr_ty(rhs_0);
1805-
1806-
auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0);
1807-
auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0);
1808-
1809-
auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]);
1810-
ret @fold.respan[ast.expr_](expr.span,
1811-
ast.expr_assign(lhs_1, rhs_1, ann));
1817+
auto checked = check_assignment_like(fcx, lhs, rhs);
1818+
auto newexpr = ast.expr_assign(checked._0,
1819+
checked._1,
1820+
checked._2);
1821+
ret @fold.respan[ast.expr_](expr.span, newexpr);
18121822
}
18131823

18141824
case (ast.expr_assign_op(?op, ?lhs, ?rhs, _)) {
1815-
auto lhs_0 = check_expr(fcx, lhs);
1816-
auto rhs_0 = check_expr(fcx, rhs);
1817-
auto lhs_t0 = expr_ty(lhs_0);
1818-
auto rhs_t0 = expr_ty(rhs_0);
1819-
1820-
auto lhs_1 = demand_expr(fcx, rhs_t0, lhs_0);
1821-
auto rhs_1 = demand_expr(fcx, expr_ty(lhs_1), rhs_0);
1822-
1823-
auto ann = ast.ann_type(rhs_t0, none[vec[@ty.t]]);
1824-
ret @fold.respan[ast.expr_](expr.span,
1825-
ast.expr_assign_op(op, lhs_1, rhs_1,
1826-
ann));
1825+
auto checked = check_assignment_like(fcx, lhs, rhs);
1826+
auto newexpr = ast.expr_assign_op(op,
1827+
checked._0,
1828+
checked._1,
1829+
checked._2);
1830+
ret @fold.respan[ast.expr_](expr.span, newexpr);
18271831
}
18281832

18291833
case (ast.expr_if(?cond, ?thn, ?elifs, ?elsopt, _)) {

0 commit comments

Comments
 (0)