Skip to content

Commit aa284de

Browse files
committed
rustc: For one-tuples, make parsing and printing the type work
and add a test to reflect-visit-data
1 parent 612553c commit aa284de

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
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: 11 additions & 2 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

src/libsyntax/print/pprust.rs

Lines changed: 3 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) => {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ fn main() {
1616
assert x == 'c';
1717
}
1818
}
19+
// test the 1-tuple type too
20+
let x: (char,) = ('d',);
21+
let (y,) = x;
22+
assert y == 'd';
1923
}
2024

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)