From e9b2487db73a6088267650538f20d12c5a5dbf33 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 27 Jul 2011 13:48:28 +0200 Subject: [PATCH] Have the pretty-printer parentesize unary ops when needed --- src/comp/syntax/print/pprust.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 4ee25df2607..0a83f826c49 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -741,7 +741,7 @@ fn print_expr(&ps s, &@ast::expr expr) { word(s.s, "}"); } case (ast::expr_call(?func, ?args)) { - print_expr(s, func); + print_expr_parens_if_unary(s, func); popen(s); commasep_exprs(s, inconsistent, args); pclose(s); @@ -909,12 +909,12 @@ fn print_expr(&ps s, &@ast::expr expr) { print_expr(s, rhs); } case (ast::expr_field(?expr, ?id)) { - print_expr(s, expr); + print_expr_parens_if_unary(s, expr); word(s.s, "."); word(s.s, id); } case (ast::expr_index(?expr, ?index)) { - print_expr(s, expr); + print_expr_parens_if_unary(s, expr); word(s.s, "."); popen(s); print_expr(s, index); @@ -1051,6 +1051,16 @@ fn print_expr(&ps s, &@ast::expr expr) { end(s); } +fn print_expr_parens_if_unary(&ps s, &@ast::expr ex) { + auto parens = alt ex.node { + ast::expr_unary(_, _) { true } + _ { false } + }; + if parens { popen(s); } + print_expr(s, ex); + if parens { pclose(s); } +} + fn print_decl(&ps s, &@ast::decl decl) { maybe_print_comment(s, decl.span.lo); alt (decl.node) {