Skip to content

Commit 2258243

Browse files
committed
Allow empty functional updating of structs
Fixes #8972
1 parent 2ba72fa commit 2258243

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/libsyntax/parse/parser.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1918,25 +1918,19 @@ impl Parser {
19181918
let mut fields = ~[];
19191919
let mut base = None;
19201920

1921-
fields.push(self.parse_field());
19221921
while self.token != token::RBRACE {
1923-
self.commit_expr(fields.last().unwrap().expr,
1924-
&[token::COMMA], &[token::RBRACE]);
1925-
19261922
if self.eat(&token::DOTDOT) {
19271923
base = Some(self.parse_expr());
19281924
break;
19291925
}
19301926

1931-
if self.token == token::RBRACE {
1932-
// Accept an optional trailing comma.
1933-
break;
1934-
}
19351927
fields.push(self.parse_field());
1928+
self.commit_expr(fields.last().unwrap().expr,
1929+
&[token::COMMA], &[token::RBRACE]);
19361930
}
19371931

19381932
hi = pth.span.hi;
1939-
self.commit_expr_expecting(fields.last().unwrap().expr, token::RBRACE);
1933+
self.expect(&token::RBRACE);
19401934
ex = ExprStruct(pth, fields, base);
19411935
return self.mk_expr(lo, hi, ex);
19421936
}
@@ -2655,8 +2649,9 @@ impl Parser {
26552649
// For distingishing between struct literals and blocks
26562650
fn looking_at_struct_literal(&mut self) -> bool {
26572651
self.token == token::LBRACE &&
2658-
(self.look_ahead(1, |t| token::is_plain_ident(t)) &&
2659-
self.look_ahead(2, |t| *t == token::COLON))
2652+
((self.look_ahead(1, |t| token::is_plain_ident(t)) &&
2653+
self.look_ahead(2, |t| *t == token::COLON))
2654+
|| self.look_ahead(1, |t| *t == token::DOTDOT))
26602655
}
26612656

26622657
fn parse_match_expr(&mut self) -> @Expr {

src/libsyntax/print/pprust.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,10 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) {
11891189
match wth {
11901190
Some(expr) => {
11911191
ibox(s, indent_unit);
1192-
word(&mut s.s, ",");
1193-
space(&mut s.s);
1192+
if !fields.is_empty() {
1193+
word(&mut s.s, ",");
1194+
space(&mut s.s);
1195+
}
11941196
word(&mut s.s, "..");
11951197
print_expr(s, expr);
11961198
end(s);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 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+
#[deriving(Eq,Clone)]
12+
struct Foo<T> {
13+
bar: T,
14+
baz: T
15+
}
16+
17+
pub fn main() {
18+
let foo = Foo {
19+
bar: 0,
20+
baz: 1
21+
};
22+
23+
let foo_ = foo.clone();
24+
let foo = Foo { ..foo };
25+
assert_eq!(foo, foo_);
26+
27+
let foo = Foo {
28+
bar: ~"one",
29+
baz: ~"two"
30+
};
31+
32+
let foo_ = foo.clone();
33+
let foo = Foo { ..foo };
34+
assert_eq!(foo, foo_);
35+
}

0 commit comments

Comments
 (0)