Skip to content

Commit 21fb49a

Browse files
committed
Join the results of alt arms so alt can be used as an expression.
Un-XFAIL expr-alt.rs.
1 parent 203e33b commit 21fb49a

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/comp/middle/trans.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,13 +3549,12 @@ fn trans_pat_binding(@block_ctxt cx, @ast.pat pat, ValueRef llval)
35493549
}
35503550
}
35513551

3552-
fn trans_alt(@block_ctxt cx, @ast.expr expr, vec[ast.arm] arms)
3553-
-> result {
3552+
fn trans_alt(@block_ctxt cx, @ast.expr expr,
3553+
vec[ast.arm] arms, ast.ann ann) -> result {
35543554
auto expr_res = trans_expr(cx, expr);
35553555

3556-
auto last_cx = new_sub_block_ctxt(expr_res.bcx, "last");
3557-
35583556
auto this_cx = expr_res.bcx;
3557+
let vec[result] arm_results = vec();
35593558
for (ast.arm arm in arms) {
35603559
auto next_cx = new_sub_block_ctxt(expr_res.bcx, "next");
35613560
auto match_res = trans_pat_match(this_cx, arm.pat, expr_res.val,
@@ -3568,9 +3567,7 @@ fn trans_alt(@block_ctxt cx, @ast.expr expr, vec[ast.arm] arms)
35683567
expr_res.val);
35693568

35703569
auto block_res = trans_block(binding_res.bcx, arm.block);
3571-
if (!is_terminated(block_res.bcx)) {
3572-
block_res.bcx.build.Br(last_cx.llbb);
3573-
}
3570+
arm_results += vec(block_res);
35743571

35753572
this_cx = next_cx;
35763573
}
@@ -3579,9 +3576,18 @@ fn trans_alt(@block_ctxt cx, @ast.expr expr, vec[ast.arm] arms)
35793576
auto default_res = trans_fail(default_cx, expr.span,
35803577
"non-exhaustive match failure");
35813578

3582-
// FIXME: This is very wrong; we should phi together all the arm blocks,
3583-
// since this is an expression.
3584-
ret res(last_cx, C_nil());
3579+
auto expr_ty = ty.ann_to_type(ann);
3580+
auto expr_llty;
3581+
if (ty.type_has_dynamic_size(expr_ty)) {
3582+
expr_llty = T_typaram_ptr(cx.fcx.ccx.tn);
3583+
} else {
3584+
expr_llty = type_of(cx.fcx.ccx, expr_ty);
3585+
if (ty.type_is_structural(expr_ty)) {
3586+
expr_llty = T_ptr(expr_llty);
3587+
}
3588+
}
3589+
3590+
ret join_results(cx, expr_llty, arm_results);
35853591
}
35863592

35873593
type generic_info = rec(@ty.t item_type,
@@ -4703,8 +4709,8 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
47034709
ret trans_do_while(cx, body, cond);
47044710
}
47054711

4706-
case (ast.expr_alt(?expr, ?arms, _)) {
4707-
ret trans_alt(cx, expr, arms);
4712+
case (ast.expr_alt(?expr, ?arms, ?ann)) {
4713+
ret trans_alt(cx, expr, arms, ann);
47084714
}
47094715

47104716
case (ast.expr_block(?blk, _)) {

src/test/run-pass/expr-alt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// xfail-boot
2-
// xfail-stage0
32
// -*- rust -*-
43

54
// Tests for using alt as an expression

0 commit comments

Comments
 (0)