Skip to content

Commit 5e1a6da

Browse files
committed
Parse nullary ret correctly
ret is similar to fail: if not followed by an expression, it should be parsed as a ret without an argument. The old version would fail if ret was followed by a close paren (for example). Fixed it. Closes #676.
1 parent 0c913e6 commit 5e1a6da

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/comp/syntax/parse/parser.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -948,15 +948,13 @@ fn parse_bottom_expr(&parser p) -> @ast::expr {
948948
hi = e.span.hi;
949949
ex = ast::expr_check(ast::unchecked, e);
950950
} else if (eat_word(p, "ret")) {
951-
alt (p.peek()) {
952-
case (token::SEMI) { ex = ast::expr_ret(none); }
953-
// Handle ret as the block result expression
954-
case (token::RBRACE) { ex = ast::expr_ret(none); }
955-
case (_) {
956-
auto e = parse_expr(p);
957-
hi = e.span.hi;
958-
ex = ast::expr_ret(some(e));
959-
}
951+
if (can_begin_expr(p.peek())) {
952+
auto e = parse_expr(p);
953+
hi = e.span.hi;
954+
ex = ast::expr_ret(some(e));
955+
}
956+
else {
957+
ex = ast::expr_ret(none);
960958
}
961959
} else if (eat_word(p, "break")) {
962960
ex = ast::expr_break;

src/test/run-pass/if-ret.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
fn foo() {
3+
if (ret) { }
4+
}
5+
6+
fn main() { foo(); }

0 commit comments

Comments
 (0)