Skip to content

Commit 612553c

Browse files
committed
syntax: Allow 1-tuple expressions
This is for greater uniformity (for example, macros that generate tuples). rustc already supported 1-tuple patterns, but there was no way to construct a 1-tuple term.
1 parent a6945f2 commit 612553c

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,9 @@ pub impl Parser {
10611061

10621062
if self.token == token::LPAREN {
10631063
self.bump();
1064+
// (e) is parenthesized e
1065+
// (e,) is a tuple with only one field, e
1066+
let mut one_tuple = false;
10641067
if self.token == token::RPAREN {
10651068
hi = self.span.hi;
10661069
self.bump();
@@ -1069,12 +1072,18 @@ pub impl Parser {
10691072
}
10701073
let mut es = ~[self.parse_expr()];
10711074
while self.token == token::COMMA {
1072-
self.bump(); es.push(self.parse_expr());
1075+
self.bump();
1076+
if self.token != token::RPAREN {
1077+
es.push(self.parse_expr());
1078+
}
1079+
else {
1080+
one_tuple = true;
1081+
}
10731082
}
10741083
hi = self.span.hi;
10751084
self.expect(token::RPAREN);
10761085

1077-
return if es.len() == 1 {
1086+
return if es.len() == 1 && !one_tuple {
10781087
self.mk_expr(lo, self.span.hi, expr_paren(es[0]))
10791088
}
10801089
else {
@@ -2158,11 +2167,13 @@ pub impl Parser {
21582167
pat = pat_lit(expr);
21592168
} else {
21602169
let mut fields = ~[self.parse_pat(refutable)];
2161-
while self.token == token::COMMA {
2162-
self.bump();
2163-
fields.push(self.parse_pat(refutable));
2170+
if self.look_ahead(1) != token::RPAREN {
2171+
while self.token == token::COMMA {
2172+
self.bump();
2173+
fields.push(self.parse_pat(refutable));
2174+
}
21642175
}
2165-
if vec::len(fields) == 1u { self.expect(token::COMMA); }
2176+
if fields.len() == 1 { self.expect(token::COMMA); }
21662177
hi = self.span.hi;
21672178
self.expect(token::RPAREN);
21682179
pat = pat_tup(fields);

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,9 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
11991199
ast::expr_tup(exprs) => {
12001200
popen(s);
12011201
commasep_exprs(s, inconsistent, exprs);
1202+
if exprs.len() == 1 {
1203+
word(s.s, ~",");
1204+
}
12021205
pclose(s);
12031206
}
12041207
ast::expr_call(func, args, sugar) => {
@@ -1634,6 +1637,9 @@ pub fn print_pat(s: @ps, &&pat: @ast::pat, refutable: bool) {
16341637
ast::pat_tup(elts) => {
16351638
popen(s);
16361639
commasep(s, inconsistent, elts, |s, p| print_pat(s, p, refutable));
1640+
if elts.len() == 1 {
1641+
word(s.s, ~",");
1642+
}
16371643
pclose(s);
16381644
}
16391645
ast::pat_box(inner) => {

src/test/run-pass/one-tuple.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 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+
// Why one-tuples? Because macros.
12+
13+
fn main() {
14+
match ('c',) {
15+
(x,) => {
16+
assert x == 'c';
17+
}
18+
}
19+
}
20+

0 commit comments

Comments
 (0)