Skip to content

Commit 8327416

Browse files
committed
Teach the pretty-printer to disambiguate 'if ret { }' et. al
1 parent 664b0ad commit 8327416

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/comp/syntax/print/pprust.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,11 +703,23 @@ fn print_possibly_embedded_block(s: &ps, blk: &ast::blk, embedded: embed_type,
703703
}
704704
}
705705

706+
// ret and fail, without arguments cannot appear is the discriminant of if,
707+
// alt, do, & while unambiguously without being parenthesized
708+
fn print_maybe_parens_discrim(s: &ps, e: &@ast::expr) {
709+
let disambig = alt e.node {
710+
ast::expr_ret(option::none.) { true }
711+
_ { false }
712+
};
713+
if disambig { popen(s) }
714+
print_expr(s, e);
715+
if disambig { pclose(s) }
716+
}
717+
706718
fn print_if(s: &ps, test: &@ast::expr, blk: &ast::blk,
707719
elseopt: &option::t<@ast::expr>, chk: bool) {
708720
head(s, "if");
709721
if chk { word_nbsp(s, "check"); }
710-
print_expr(s, test);
722+
print_maybe_parens_discrim(s, test);
711723
space(s.s);
712724
print_block(s, blk);
713725
fn do_else(s: &ps, els: option::t<@ast::expr>) {
@@ -869,7 +881,7 @@ fn print_expr(s: &ps, expr: &@ast::expr) {
869881
}
870882
ast::expr_while(test, blk) {
871883
head(s, "while");
872-
print_expr(s, test);
884+
print_maybe_parens_discrim(s, test);
873885
space(s.s);
874886
print_block(s, blk);
875887
}
@@ -897,7 +909,7 @@ fn print_expr(s: &ps, expr: &@ast::expr) {
897909
cbox(s, alt_indent_unit);
898910
ibox(s, 4u);
899911
word_nbsp(s, "alt");
900-
print_expr(s, expr);
912+
print_maybe_parens_discrim(s, expr);
901913
space(s.s);
902914
bopen(s);
903915
for arm: ast::arm in arms {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// xfail-pretty
21
fn foo() { if (ret) { } }
32

43
fn main() { foo(); }

0 commit comments

Comments
 (0)