Fix buggy while and do-while translation in rustc. Add test.

This commit is contained in:
Graydon Hoare 2010-11-04 07:55:33 -07:00
parent 896570a3a9
commit 16faef2218
4 changed files with 35 additions and 12 deletions

View File

@ -447,8 +447,9 @@ TEST_XFAILS_X86 := $(TASK_XFAILS) \
test/run-pass/mlist-cycle.rs \
test/run-pass/obj-as.rs \
test/run-pass/task-comm.rs \
test/run-pass/vec-slice.rs \
test/run-pass/task-comm-3.rs \
test/run-pass/vec-slice.rs \
test/run-pass/while-and-do-while.rs \
test/run-fail/task-comm-14.rs \
test/compile-fail/bad-recv.rs \
test/compile-fail/bad-send.rs \

View File

@ -636,6 +636,7 @@ impure fn parse_do_while_expr(parser p) -> @ast.expr {
expect (p, token.LPAREN);
auto cond = parse_expr(p);
expect(p, token.RPAREN);
expect(p, token.SEMI);
hi = cond.span;
ret @spanned(lo, hi, ast.expr_do_while(body, cond, ast.ann_none));
}

View File

@ -660,13 +660,15 @@ impure fn trans_while(@block_ctxt cx, &ast.expr cond,
auto body_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);
cx.build.Br(cond_cx.llbb);
auto cond_res = trans_expr(cond_cx, cond);
cond_cx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
auto body_res = trans_block(body_cx, body);
body_cx.build.Br(cond_cx.llbb);
auto cond_res = trans_expr(cond_cx, cond);
body_res.bcx.build.Br(cond_cx.llbb);
cond_res.bcx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
cx.build.Br(cond_cx.llbb);
ret res(next_cx, C_nil());
}
@ -676,12 +678,13 @@ impure fn trans_do_while(@block_ctxt cx, &ast.block body,
auto body_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);
cx.build.Br(body_cx.llbb);
auto body_res = trans_block(body_cx, body);
auto cond_res = trans_expr(body_cx, cond);
body_cx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
auto cond_res = trans_expr(body_res.bcx, cond);
cond_res.bcx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
cx.build.Br(body_cx.llbb);
ret res(next_cx, body_res.val);
}
@ -759,6 +762,10 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
ret trans_while(cx, *cond, body);
}
case (ast.expr_do_while(?body, ?cond, _)) {
ret trans_do_while(cx, body, *cond);
}
case (ast.expr_block(?blk, _)) {
auto sub_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);

View File

@ -0,0 +1,14 @@
fn main() {
let int x = 10;
let int y = 0;
while(y < x) {
log y;
log "hello";
y = y + 1;
}
do {
log "goodbye";
x = x - 1;
log x;
} while (x > 0);
}