Skip to content

Commit f683351

Browse files
committed
auto merge of #5002 : catamorphism/rust/one-tuples, r=graydon
r? @graydon - 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. @graydon , as far as your comment on #4898 - it did turn out to be solvable inside the macro (since @luqmana already fixed it using structs instead), but I still think it's a good idea to allow 1-tuples, for uniformity. I don't think anyone is likely to trip over it, and I'm not too worried that it changes the amount of ambiguity.
2 parents 2ec958d + aa284de commit f683351

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

src/libcore/to_str.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ impl ToStr for @str {
4343
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
4444
}
4545

46+
// FIXME #4898: impl for one-tuples
47+
4648
impl<A: ToStr, B: ToStr> ToStr for (A, B) {
4749
#[inline(always)]
4850
pure fn to_str(&self) -> ~str {

src/libcore/tuple.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ impl<A: Copy, B: Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
111111
}
112112
}
113113

114+
// FIXME #4898: impl for one-tuples
115+
114116
#[cfg(notest)]
115117
impl<A: Eq, B: Eq> Eq for (A, B) {
116118
#[inline(always)]

src/libsyntax/parse/parser.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,21 @@ pub impl Parser {
576576
self.bump();
577577
ty_nil
578578
} else {
579+
// (t) is a parenthesized ty
580+
// (t,) is the type of a tuple with only one field,
581+
// of type t
579582
let mut ts = ~[self.parse_ty(false)];
583+
let mut one_tuple = false;
580584
while self.token == token::COMMA {
581585
self.bump();
582-
ts.push(self.parse_ty(false));
586+
if self.token != token::RPAREN {
587+
ts.push(self.parse_ty(false));
588+
}
589+
else {
590+
one_tuple = true;
591+
}
583592
}
584-
let t = if vec::len(ts) == 1u { ts[0].node }
593+
let t = if ts.len() == 1 && !one_tuple { ts[0].node }
585594
else { ty_tup(ts) };
586595
self.expect(token::RPAREN);
587596
t
@@ -1061,6 +1070,9 @@ pub impl Parser {
10611070

10621071
if self.token == token::LPAREN {
10631072
self.bump();
1073+
// (e) is parenthesized e
1074+
// (e,) is a tuple with only one field, e
1075+
let mut one_tuple = false;
10641076
if self.token == token::RPAREN {
10651077
hi = self.span.hi;
10661078
self.bump();
@@ -1069,12 +1081,18 @@ pub impl Parser {
10691081
}
10701082
let mut es = ~[self.parse_expr()];
10711083
while self.token == token::COMMA {
1072-
self.bump(); es.push(self.parse_expr());
1084+
self.bump();
1085+
if self.token != token::RPAREN {
1086+
es.push(self.parse_expr());
1087+
}
1088+
else {
1089+
one_tuple = true;
1090+
}
10731091
}
10741092
hi = self.span.hi;
10751093
self.expect(token::RPAREN);
10761094

1077-
return if es.len() == 1 {
1095+
return if es.len() == 1 && !one_tuple {
10781096
self.mk_expr(lo, self.span.hi, expr_paren(es[0]))
10791097
}
10801098
else {
@@ -2158,11 +2176,13 @@ pub impl Parser {
21582176
pat = pat_lit(expr);
21592177
} else {
21602178
let mut fields = ~[self.parse_pat(refutable)];
2161-
while self.token == token::COMMA {
2162-
self.bump();
2163-
fields.push(self.parse_pat(refutable));
2179+
if self.look_ahead(1) != token::RPAREN {
2180+
while self.token == token::COMMA {
2181+
self.bump();
2182+
fields.push(self.parse_pat(refutable));
2183+
}
21642184
}
2165-
if vec::len(fields) == 1u { self.expect(token::COMMA); }
2185+
if fields.len() == 1 { self.expect(token::COMMA); }
21662186
hi = self.span.hi;
21672187
self.expect(token::RPAREN);
21682188
pat = pat_tup(fields);

src/libsyntax/print/pprust.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ pub fn print_type_ex(s: @ps, &&ty: @ast::Ty, print_colons: bool) {
414414
ast::ty_tup(elts) => {
415415
popen(s);
416416
commasep(s, inconsistent, elts, print_type);
417+
if elts.len() == 1 {
418+
word(s.s, ~",");
419+
}
417420
pclose(s);
418421
}
419422
ast::ty_bare_fn(f) => {
@@ -1199,6 +1202,9 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
11991202
ast::expr_tup(exprs) => {
12001203
popen(s);
12011204
commasep_exprs(s, inconsistent, exprs);
1205+
if exprs.len() == 1 {
1206+
word(s.s, ~",");
1207+
}
12021208
pclose(s);
12031209
}
12041210
ast::expr_call(func, args, sugar) => {
@@ -1634,6 +1640,9 @@ pub fn print_pat(s: @ps, &&pat: @ast::pat, refutable: bool) {
16341640
ast::pat_tup(elts) => {
16351641
popen(s);
16361642
commasep(s, inconsistent, elts, |s, p| print_pat(s, p, refutable));
1643+
if elts.len() == 1 {
1644+
word(s.s, ~",");
1645+
}
16371646
pclose(s);
16381647
}
16391648
ast::pat_box(inner) => {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// test the 1-tuple type too
20+
let x: (char,) = ('d',);
21+
let (y,) = x;
22+
assert y == 'd';
23+
}
24+

src/test/run-pass/reflect-visit-data.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,12 @@ struct Triple { x: int, y: int, z: int }
636636

637637
pub fn main() {
638638
unsafe {
639-
let r = (1,2,3,true,false, Triple {x:5,y:4,z:3});
639+
let r = (1,2,3,true,false, Triple {x:5,y:4,z:3}, (12,));
640640
let p = ptr::addr_of(&r) as *c_void;
641641
let u = my_visitor(@Stuff {mut ptr1: p,
642642
mut ptr2: p,
643-
mut vals: ~[]});
643+
mut vals: ~[]
644+
});
644645
let v = ptr_visit_adaptor(Inner {inner: u});
645646
let td = get_tydesc_for(r);
646647
unsafe { error!("tydesc sz: %u, align: %u",
@@ -653,7 +654,7 @@ pub fn main() {
653654
}
654655
error!("%?", copy u.vals);
655656
assert u.vals == ~[
656-
~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3"
657+
~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3", ~"12"
657658
];
658659
}
659660
}

0 commit comments

Comments
 (0)