Skip to content

Commit 1200ad0

Browse files
committed
Fix a bug pretty printing match { 5i } { _ => { } }
This also always puts a trailing comma on the last non-block expr.
1 parent a46463d commit 1200ad0

File tree

5 files changed

+71
-50
lines changed

5 files changed

+71
-50
lines changed

src/libsyntax/ext/quote.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub mod rt {
147147
impl_to_source!(Gc<ast::Stmt>, stmt_to_string)
148148
impl_to_source!(Gc<ast::Expr>, expr_to_string)
149149
impl_to_source!(Gc<ast::Pat>, pat_to_string)
150+
impl_to_source!(ast::Arm, arm_to_string)
150151
impl_to_source_slice!(ast::Ty, ", ")
151152
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
152153

@@ -240,6 +241,7 @@ pub mod rt {
240241
impl_to_tokens!(ast::Ident)
241242
impl_to_tokens!(Gc<ast::Item>)
242243
impl_to_tokens!(Gc<ast::Pat>)
244+
impl_to_tokens!(ast::Arm)
243245
impl_to_tokens!(Gc<ast::Method>)
244246
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
245247
impl_to_tokens!(ast::Ty)

src/libsyntax/print/pprust.rs

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use attr::{AttrMetaMethods, AttributeMethods};
1818
use codemap::{CodeMap, BytePos};
1919
use codemap;
2020
use diagnostic;
21-
use parse::classify::expr_is_simple_block;
2221
use parse::token;
2322
use parse::lexer::comments;
2423
use parse;
@@ -151,6 +150,10 @@ pub fn pat_to_string(pat: &ast::Pat) -> String {
151150
to_string(|s| s.print_pat(pat))
152151
}
153152

153+
pub fn arm_to_string(arm: &ast::Arm) -> String {
154+
to_string(|s| s.print_arm(arm))
155+
}
156+
154157
pub fn expr_to_string(e: &ast::Expr) -> String {
155158
to_string(|s| s.print_expr(e))
156159
}
@@ -1402,53 +1405,8 @@ impl<'a> State<'a> {
14021405
try!(self.print_expr(&**expr));
14031406
try!(space(&mut self.s));
14041407
try!(self.bopen());
1405-
let len = arms.len();
1406-
for (i, arm) in arms.iter().enumerate() {
1407-
// I have no idea why this check is necessary, but here it
1408-
// is :(
1409-
if arm.attrs.is_empty() {
1410-
try!(space(&mut self.s));
1411-
}
1412-
try!(self.cbox(indent_unit));
1413-
try!(self.ibox(0u));
1414-
try!(self.print_outer_attributes(arm.attrs.as_slice()));
1415-
let mut first = true;
1416-
for p in arm.pats.iter() {
1417-
if first {
1418-
first = false;
1419-
} else {
1420-
try!(space(&mut self.s));
1421-
try!(self.word_space("|"));
1422-
}
1423-
try!(self.print_pat(&**p));
1424-
}
1425-
try!(space(&mut self.s));
1426-
match arm.guard {
1427-
Some(ref e) => {
1428-
try!(self.word_space("if"));
1429-
try!(self.print_expr(&**e));
1430-
try!(space(&mut self.s));
1431-
}
1432-
None => ()
1433-
}
1434-
try!(self.word_space("=>"));
1435-
1436-
match arm.body.node {
1437-
ast::ExprBlock(ref blk) => {
1438-
// the block will close the pattern's ibox
1439-
try!(self.print_block_unclosed_indent(&**blk,
1440-
indent_unit));
1441-
}
1442-
_ => {
1443-
try!(self.end()); // close the ibox for the pattern
1444-
try!(self.print_expr(&*arm.body));
1445-
}
1446-
}
1447-
if !expr_is_simple_block(expr.clone())
1448-
&& i < len - 1 {
1449-
try!(word(&mut self.s, ","));
1450-
}
1451-
try!(self.end()); // close enclosing cbox
1408+
for arm in arms.iter() {
1409+
try!(self.print_arm(arm));
14521410
}
14531411
try!(self.bclose_(expr.span, indent_unit));
14541412
}
@@ -1882,6 +1840,51 @@ impl<'a> State<'a> {
18821840
self.ann.post(self, NodePat(pat))
18831841
}
18841842

1843+
fn print_arm(&mut self, arm: &ast::Arm) -> IoResult<()> {
1844+
// I have no idea why this check is necessary, but here it
1845+
// is :(
1846+
if arm.attrs.is_empty() {
1847+
try!(space(&mut self.s));
1848+
}
1849+
try!(self.cbox(indent_unit));
1850+
try!(self.ibox(0u));
1851+
try!(self.print_outer_attributes(arm.attrs.as_slice()));
1852+
let mut first = true;
1853+
for p in arm.pats.iter() {
1854+
if first {
1855+
first = false;
1856+
} else {
1857+
try!(space(&mut self.s));
1858+
try!(self.word_space("|"));
1859+
}
1860+
try!(self.print_pat(&**p));
1861+
}
1862+
try!(space(&mut self.s));
1863+
match arm.guard {
1864+
Some(ref e) => {
1865+
try!(self.word_space("if"));
1866+
try!(self.print_expr(&**e));
1867+
try!(space(&mut self.s));
1868+
}
1869+
None => ()
1870+
}
1871+
try!(self.word_space("=>"));
1872+
1873+
match arm.body.node {
1874+
ast::ExprBlock(ref blk) => {
1875+
// the block will close the pattern's ibox
1876+
try!(self.print_block_unclosed_indent(&**blk,
1877+
indent_unit));
1878+
}
1879+
_ => {
1880+
try!(self.end()); // close the ibox for the pattern
1881+
try!(self.print_expr(&*arm.body));
1882+
try!(word(&mut self.s, ","));
1883+
}
1884+
}
1885+
self.end() // close enclosing cbox
1886+
}
1887+
18851888
// Returns whether it printed anything
18861889
fn print_explicit_self(&mut self,
18871890
explicit_self: ast::ExplicitSelf_,

src/test/pretty/match-block-expr.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// pp-exact
12+
13+
fn main() {
14+
let x = match { 5i } { 1 => 5i, 2 => 6, _ => 7, };
15+
assert_eq!(x , 7);
16+
}

src/test/pretty/match-naked-expr-medium.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ fn main() {
1919
"long".to_string(), "string".to_string()],
2020
None =>
2121
["none".to_string(), "a".to_string(), "a".to_string(),
22-
"a".to_string(), "a".to_string()]
22+
"a".to_string(), "a".to_string()],
2323
};
2424
}

src/test/pretty/match-naked-expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn main() {
1515
let _y =
1616
match x {
1717
Some(_) => "some(_)".to_string(),
18-
None => "none".to_string()
18+
None => "none".to_string(),
1919
};
2020
}

0 commit comments

Comments
 (0)