diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 632fdaedd5aa..96e92aeb8521 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -3706,8 +3706,15 @@ fn trans_if(&@block_ctxt cx, &@ast::expr cond, alt (els) { case (some[@ast::expr](?elexpr)) { alt (elexpr.node) { - case (ast::expr_if(_, _, _, _)) { - else_res = trans_expr(else_cx, elexpr); + case (ast::expr_if(?cond, ?thn, ?els, _)) { + else_res = trans_if(else_cx, cond, thn, els); + // The if expression may need to use the else context to + // drop the refcount of its result so we need to run the + // cleanups + auto bcx = else_res.bcx; + bcx = trans_block_cleanups(bcx, + find_scope_cx(bcx)); + else_res = res(bcx, else_res.val); } case (ast::expr_block(?blk, _)) { // Calling trans_block directly instead of trans_expr diff --git a/src/test/run-pass/expr-elseif-ref.rs b/src/test/run-pass/expr-elseif-ref.rs new file mode 100644 index 000000000000..03ba523b709f --- /dev/null +++ b/src/test/run-pass/expr-elseif-ref.rs @@ -0,0 +1,17 @@ +// xfail-stage0 + +// Make sure we drop the refs of the temporaries needed to return the +// values from the else if branch + +fn main() { + let vec[uint] y = [10u]; + auto x = if (false) { + y + } else if (true) { + y + } else { + y + }; + + assert y.(0) == 10u; +}