Skip to content

Commit b6d2fe4

Browse files
committed
rustc: Translate else if blocks the same as other blocks. Closes #388
1 parent 879a952 commit b6d2fe4

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/comp/middle/trans.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,15 +3670,18 @@ fn trans_if(&@block_ctxt cx, &@ast::expr cond,
36703670
alt (els) {
36713671
case (some[@ast::expr](?elexpr)) {
36723672
alt (elexpr.node) {
3673-
case (ast::expr_if(?cond, ?thn, ?els, _)) {
3674-
else_res = trans_if(else_cx, cond, thn, els);
3675-
// The if expression may need to use the else context to
3676-
// drop the refcount of its result so we need to run the
3677-
// cleanups
3678-
auto bcx = else_res.bcx;
3679-
bcx = trans_block_cleanups(bcx,
3680-
find_scope_cx(bcx));
3681-
else_res = res(bcx, else_res.val);
3673+
case (ast::expr_if(_, _, _, ?ann)) {
3674+
// Synthesize a block here to act as the else block
3675+
// containing an if expression. Needed in order for the
3676+
// else scope to behave like a normal block scope. A tad
3677+
// ugly.
3678+
let ast::block_ elseif_blk_
3679+
= rec(stmts = [],
3680+
expr = some[@ast::expr](elexpr),
3681+
a = ann);
3682+
auto elseif_blk = rec(node = elseif_blk_,
3683+
span = elexpr.span);
3684+
else_res = trans_block(else_cx, elseif_blk);
36823685
}
36833686
case (ast::expr_block(?blk, _)) {
36843687
// Calling trans_block directly instead of trans_expr

src/test/run-pass/expr-block-ref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// xfail-stage0
22

3+
// Regression test for issue #388
4+
35
fn main() {
46
auto x = {{[10]}};
57
}

src/test/run-pass/expr-elseif-ref2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// xfail-stage0
2-
// xfail-stage1
3-
// xfail-stage2
2+
3+
// Regression test for issue #388
44

55
fn main() {
66
auto x = if (false) {

0 commit comments

Comments
 (0)