]> git.lizzy.rs Git - rust.git/commitdiff
Parse nullary ret correctly
authorTim Chevalier <chevalier@alum.wellesley.edu>
Wed, 13 Jul 2011 22:00:59 +0000 (15:00 -0700)
committerTim Chevalier <chevalier@alum.wellesley.edu>
Wed, 13 Jul 2011 22:00:59 +0000 (15:00 -0700)
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.

src/comp/syntax/parse/parser.rs
src/test/run-pass/if-ret.rs [new file with mode: 0644]

index fb9eca8640df9ed1fb09add4e38163c54b69ad71..b2aa0e7b4dc89a66f78a01cea870071a74d7414d 100644 (file)
@@ -948,15 +948,13 @@ fn parse_expr_opt(&parser p) -> option::t[@ast::expr] {
         hi = e.span.hi;
         ex = ast::expr_check(ast::unchecked, e);
     } else if (eat_word(p, "ret")) {
-        alt (p.peek()) {
-            case (token::SEMI) { ex = ast::expr_ret(none); }
-            // Handle ret as the block result expression
-            case (token::RBRACE) { ex = ast::expr_ret(none); }
-            case (_) {
-                auto e = parse_expr(p);
-                hi = e.span.hi;
-                ex = ast::expr_ret(some(e));
-            }
+        if (can_begin_expr(p.peek())) {
+            auto e = parse_expr(p);
+            hi = e.span.hi;
+            ex = ast::expr_ret(some(e));
+        }
+        else {
+            ex = ast::expr_ret(none);
         }
     } else if (eat_word(p, "break")) {
         ex = ast::expr_break;
diff --git a/src/test/run-pass/if-ret.rs b/src/test/run-pass/if-ret.rs
new file mode 100644 (file)
index 0000000..7d0d165
--- /dev/null
@@ -0,0 +1,6 @@
+// xfail-stage0
+fn foo() {
+    if (ret) { }
+}
+
+fn main() { foo(); }