rustc: For one-tuples, make parsing and printing the type work

and add a test to reflect-visit-data
This commit is contained in:
Tim Chevalier 2013-02-18 17:45:56 -08:00
parent 612553cb39
commit aa284de1fc
6 changed files with 26 additions and 5 deletions

View File

@ -43,6 +43,8 @@ impl ToStr for @str {
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
}
// FIXME #4898: impl for one-tuples
impl<A: ToStr, B: ToStr> ToStr for (A, B) {
#[inline(always)]
pure fn to_str(&self) -> ~str {

View File

@ -111,6 +111,8 @@ impl<A: Copy, B: Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
}
}
// FIXME #4898: impl for one-tuples
#[cfg(notest)]
impl<A: Eq, B: Eq> Eq for (A, B) {
#[inline(always)]

View File

@ -576,12 +576,21 @@ pub impl Parser {
self.bump();
ty_nil
} else {
// (t) is a parenthesized ty
// (t,) is the type of a tuple with only one field,
// of type t
let mut ts = ~[self.parse_ty(false)];
let mut one_tuple = false;
while self.token == token::COMMA {
self.bump();
ts.push(self.parse_ty(false));
if self.token != token::RPAREN {
ts.push(self.parse_ty(false));
}
else {
one_tuple = true;
}
}
let t = if vec::len(ts) == 1u { ts[0].node }
let t = if ts.len() == 1 && !one_tuple { ts[0].node }
else { ty_tup(ts) };
self.expect(token::RPAREN);
t

View File

@ -414,6 +414,9 @@ pub fn print_type_ex(s: @ps, &&ty: @ast::Ty, print_colons: bool) {
ast::ty_tup(elts) => {
popen(s);
commasep(s, inconsistent, elts, print_type);
if elts.len() == 1 {
word(s.s, ~",");
}
pclose(s);
}
ast::ty_bare_fn(f) => {

View File

@ -16,5 +16,9 @@ fn main() {
assert x == 'c';
}
}
// test the 1-tuple type too
let x: (char,) = ('d',);
let (y,) = x;
assert y == 'd';
}

View File

@ -636,11 +636,12 @@ struct Triple { x: int, y: int, z: int }
pub fn main() {
unsafe {
let r = (1,2,3,true,false, Triple {x:5,y:4,z:3});
let r = (1,2,3,true,false, Triple {x:5,y:4,z:3}, (12,));
let p = ptr::addr_of(&r) as *c_void;
let u = my_visitor(@Stuff {mut ptr1: p,
mut ptr2: p,
mut vals: ~[]});
mut vals: ~[]
});
let v = ptr_visit_adaptor(Inner {inner: u});
let td = get_tydesc_for(r);
unsafe { error!("tydesc sz: %u, align: %u",
@ -653,7 +654,7 @@ pub fn main() {
}
error!("%?", copy u.vals);
assert u.vals == ~[
~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3"
~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3", ~"12"
];
}
}