]> git.lizzy.rs Git - rust.git/commitdiff
fix pretty printer to correctly insert parens for disamb
authorNiko Matsakis <niko@alum.mit.edu>
Thu, 5 Jan 2012 06:01:58 +0000 (22:01 -0800)
committerNiko Matsakis <niko@alum.mit.edu>
Thu, 5 Jan 2012 18:44:38 +0000 (10:44 -0800)
src/comp/syntax/print/pprust.rs
src/test/pretty/block-arg-disambig.rs [new file with mode: 0644]
src/test/pretty/disamb-stmt-expr.rs

index f54771be5728f3cba55c6b297783080cfa4dc0c6..e1a0151a627186302bce163c2a28c5e4087e058d 100644 (file)
@@ -567,11 +567,11 @@ fn print_stmt(s: ps, st: ast::stmt) {
       }
       ast::stmt_expr(expr, _) {
         space_if_not_bol(s);
-        print_tl_expr(s, expr);
+        print_expr(s, expr);
       }
       ast::stmt_semi(expr, _) {
         space_if_not_bol(s);
-        print_tl_expr(s, expr);
+        print_expr(s, expr);
         word(s.s, ";");
       }
     }
@@ -609,7 +609,7 @@ fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
     alt blk.node.expr {
       some(expr) {
         space_if_not_bol(s);
-        print_tl_expr(s, expr);
+        print_expr(s, expr);
         maybe_print_trailing_comment(s, expr.span, some(blk.span.hi));
       }
       _ { }
@@ -697,37 +697,6 @@ fn print_mac(s: ps, m: ast::mac) {
     }
 }
 
-// An expression that begins with a dual-form statement/expression like `{
-// ... }-10` would be parsed as `{ ... };-10` unless parentheses are used (ie,
-// `({...}-10)`).  These parentheses are not, however, preserved by the
-// parser. This function specifies whether parentheses must be inserted.
-fn print_tl_expr(s: ps, &&expr: @ast::expr) {
-    fn stmt_expr_requires_parens(ex: @ast::expr) -> bool {
-        fn helper(ex: @ast::expr, inner: bool) -> bool {
-            log(debug, ("helper", ex, inner));
-            if inner && !parse::parser::expr_requires_semi_to_be_stmt(ex) {
-                ret true;
-            }
-            alt ex.node {
-              ast::expr_call(subex, _, _) | ast::expr_binary(_, subex, _) {
-                be helper(subex, true);
-              }
-              _ { ret false; }
-            }
-        }
-        ret helper(ex, false);
-    }
-
-    #debug("print_tl_expr %s", expr_to_str(expr));
-    if stmt_expr_requires_parens(expr) {
-        popen(s);
-        print_expr(s, expr);
-        pclose(s);
-    } else {
-        print_expr(s, expr);
-    }
-}
-
 fn print_expr(s: ps, &&expr: @ast::expr) {
     maybe_print_comment(s, expr.span.lo);
     ibox(s, indent_unit);
@@ -1062,6 +1031,7 @@ fn print_expr_parens_if_not_bot(s: ps, ex: @ast::expr) {
       ast::expr_copy(_) | ast::expr_assign(_, _) | ast::expr_be(_) |
       ast::expr_assign_op(_, _, _) | ast::expr_swap(_, _) |
       ast::expr_log(_, _, _) | ast::expr_assert(_) |
+      ast::expr_call(_, _, true) |
       ast::expr_check(_, _) { true }
       _ { false }
     };
@@ -1404,7 +1374,7 @@ fn need_parens(expr: @ast::expr, outer_prec: int) -> bool {
       ast::expr_assert(_) { true }
       ast::expr_check(_, _) { true }
       ast::expr_log(_, _, _) { true }
-      _ { false }
+      _ { !parse::parser::expr_requires_semi_to_be_stmt(expr) }
     }
 }
 
diff --git a/src/test/pretty/block-arg-disambig.rs b/src/test/pretty/block-arg-disambig.rs
new file mode 100644 (file)
index 0000000..288e01d
--- /dev/null
@@ -0,0 +1,5 @@
+fn blk1(b: block()) -> fn@() { ret fn@() { }; }
+
+fn test1() { (blk1 {|| #debug["hi"]; })(); }
+
+fn test2() { (blk1 {|| #debug["hi"]; }) {|| #debug["ho"]; }; }
index 5f9d57f94a732b693bd50a04b9d82ab8d814fa95..4f4bf6ff51267b6b43e7d77b959decc62d9a4afe 100644 (file)
@@ -4,5 +4,5 @@
 // preserved.  They are needed to disambiguate `{ret n+1}; - 0` from
 // `({ret n+1}-0)`.
 
-fn wsucc(n: int) -> int { ({ ret n + 1 } - 0); }
+fn wsucc(n: int) -> int { ({ ret n + 1 }) - 0; }
 fn main() { }