Skip to content

Commit f712b2d

Browse files
committed
accept naked exprs with commas in pattern arms
pretty printing will use them, but indentation is slightly off if the expr is long
1 parent 22f492a commit f712b2d

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

src/libsyntax/ast_util.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ fn view_path_id(p: @view_path) -> node_id {
568568
}
569569
}
570570

571+
fn lone_block_expr(blk: blk) -> option<@ast::expr> {
572+
if blk.node.view_items.len() != 0 { ret none; }
573+
if blk.node.stmts.len() != 0 { ret none; }
574+
if blk.node.rules != default_blk { ret none; }
575+
ret blk.node.expr;
576+
}
577+
571578
// Local Variables:
572579
// mode: rust
573580
// fill-column: 78;

src/libsyntax/parse/parser.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,8 +1509,25 @@ class parser {
15091509
let pats = self.parse_pats();
15101510
let mut guard = none;
15111511
if self.eat_keyword(~"if") { guard = some(self.parse_expr()); }
1512-
if self.token == token::FAT_ARROW { self.bump(); }
1513-
let blk = self.parse_block();
1512+
let blk = if self.token != token::FAT_ARROW {
1513+
self.parse_block()
1514+
} else {
1515+
self.bump();
1516+
if self.token == token::LBRACE {
1517+
self.parse_block()
1518+
} else {
1519+
let expr = self.parse_expr();
1520+
if self.token != token::RBRACE {
1521+
self.expect(token::COMMA);
1522+
}
1523+
{node: {view_items: ~[],
1524+
stmts: ~[],
1525+
expr: some(expr),
1526+
id: self.get_id(),
1527+
rules: default_blk},
1528+
span: expr.span}
1529+
}
1530+
};
15141531
vec::push(arms, {pats: pats, guard: guard, body: blk});
15151532
}
15161533
let mut hi = self.span.hi;

src/libsyntax/print/pprust.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import pp::{break_offset, word, printer,
66
inconsistent, eof};
77
import diagnostic;
88
import ast::{required, provided};
9-
import ast_util::operator_prec;
9+
import ast_util::{operator_prec, lone_block_expr};
1010
import dvec::{dvec, extensions};
1111
import parse::classify::*;
1212

@@ -998,7 +998,8 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
998998
print_maybe_parens_discrim(s, expr);
999999
space(s.s);
10001000
bopen(s);
1001-
for arms.each |arm| {
1001+
let len = arms.len();
1002+
for arms.eachi |i, arm| {
10021003
space(s.s);
10031004
cbox(s, alt_indent_unit);
10041005
ibox(s, 0u);
@@ -1014,8 +1015,19 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
10141015
some(e) { word_space(s, ~"if"); print_expr(s, e); space(s.s); }
10151016
none { }
10161017
}
1017-
print_possibly_embedded_block(s, arm.body, block_normal,
1018-
alt_indent_unit);
1018+
word_space(s, ~"=>");
1019+
alt lone_block_expr(arm.body) {
1020+
some(expr) => {
1021+
end(s); // close the ibox for the pattern
1022+
print_expr(s, expr);
1023+
if i < len - 1 { word_space(s, ~","); }
1024+
end(s); // close enclosing cbox
1025+
}
1026+
none => {
1027+
print_possibly_embedded_block(s, arm.body, block_normal,
1028+
alt_indent_unit);
1029+
}
1030+
}
10191031
}
10201032
bclose_(s, expr.span, alt_indent_unit);
10211033
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// pretty-exact
2+
3+
// actually this doesn't quite look how I want it to, but I can't
4+
// get the prettyprinter to indent the long expr
5+
6+
fn main() {
7+
let x = some(3);
8+
let y =
9+
alt x {
10+
some(_) =>
11+
"some" + "very" + "very" + "very" + "very" + "very" + "very" +
12+
"very" + "very" + "long" + "string",
13+
14+
none => "none"
15+
};
16+
assert y == "some(_)";
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// pretty-exact
2+
3+
fn main() {
4+
let x = some(3);
5+
let _y =
6+
alt x {
7+
some(_) => ~[~"some(_)", ~"not", ~"SO", ~"long", ~"string"],
8+
none => ~[~"none"]
9+
};
10+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// pretty-exact
2+
3+
fn main() {
4+
let x = some(3);
5+
let y = alt x { some(_) => "some(_)", none => "none" };
6+
assert y == "some(_)";
7+
}

0 commit comments

Comments
 (0)