Skip to content

Commit cf50e1d

Browse files
committed
Preserve parenthesization in the AST
for better pretty-printing, as per #1458
1 parent 17a5d0f commit cf50e1d

20 files changed

+148
-217
lines changed

src/libsyntax/ast.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,10 @@ enum expr_ {
744744
expr_struct(@path, ~[field], Option<@expr>),
745745

746746
// A vector literal constructed from one repeated element.
747-
expr_repeat(@expr /* element */, @expr /* count */, mutability)
747+
expr_repeat(@expr /* element */, @expr /* count */, mutability),
748+
749+
// No-op: used solely so we can pretty-print faithfully
750+
expr_paren(@expr)
748751
}
749752

750753
#[auto_serialize]

src/libsyntax/fold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
494494
expr_struct(fld.fold_path(path),
495495
vec::map(fields, |x| fold_field(*x)),
496496
option::map(&maybe_expr, |x| fld.fold_expr(*x)))
497-
}
497+
},
498+
expr_paren(ex) => expr_paren(fld.fold_expr(ex))
498499
}
499500
}
500501

src/libsyntax/parse/classify.rs

-36
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,3 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
3636
}
3737
}
3838
}
39-
40-
fn need_parens(expr: @ast::expr, outer_prec: uint) -> bool {
41-
match expr.node {
42-
ast::expr_binary(op, _, _) => operator_prec(op) < outer_prec,
43-
ast::expr_cast(_, _) => parse::prec::as_prec < outer_prec,
44-
// This may be too conservative in some cases
45-
ast::expr_assign(_, _) => true,
46-
ast::expr_swap(_, _) => true,
47-
ast::expr_assign_op(_, _, _) => true,
48-
ast::expr_ret(_) => true,
49-
ast::expr_assert(_) => true,
50-
ast::expr_log(_, _, _) => true,
51-
_ => !parse::classify::expr_requires_semi_to_be_stmt(expr)
52-
}
53-
}
54-
55-
fn ends_in_lit_int(ex: @ast::expr) -> bool {
56-
match ex.node {
57-
ast::expr_lit(node) => match node {
58-
@{node: ast::lit_int(_, ast::ty_i), _}
59-
| @{node: ast::lit_int_unsuffixed(_), _} => true,
60-
_ => false
61-
},
62-
ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
63-
ast::expr_copy(sub) | ast::expr_assign(_, sub) |
64-
ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) |
65-
ast::expr_log(_, _, sub) | ast::expr_assert(sub) => {
66-
ends_in_lit_int(sub)
67-
}
68-
ast::expr_fail(osub) | ast::expr_ret(osub) => match osub {
69-
Some(ex) => ends_in_lit_int(ex),
70-
_ => false
71-
},
72-
_ => false
73-
}
74-
}

0 commit comments

Comments
 (0)