Skip to content

Commit

Permalink
Allow empty functional updating of structs
Browse files Browse the repository at this point in the history
Fixes #8972
  • Loading branch information
lilyball committed Jan 29, 2014
1 parent 2ba72fa commit 2258243
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
17 changes: 6 additions & 11 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,25 +1918,19 @@ impl Parser {
let mut fields = ~[];
let mut base = None;

fields.push(self.parse_field());
while self.token != token::RBRACE {
self.commit_expr(fields.last().unwrap().expr,
&[token::COMMA], &[token::RBRACE]);

if self.eat(&token::DOTDOT) {
base = Some(self.parse_expr());
break;
}

if self.token == token::RBRACE {
// Accept an optional trailing comma.
break;
}
fields.push(self.parse_field());
self.commit_expr(fields.last().unwrap().expr,
&[token::COMMA], &[token::RBRACE]);
}

hi = pth.span.hi;
self.commit_expr_expecting(fields.last().unwrap().expr, token::RBRACE);
self.expect(&token::RBRACE);
ex = ExprStruct(pth, fields, base);
return self.mk_expr(lo, hi, ex);
}
Expand Down Expand Up @@ -2655,8 +2649,9 @@ impl Parser {
// For distingishing between struct literals and blocks
fn looking_at_struct_literal(&mut self) -> bool {
self.token == token::LBRACE &&
(self.look_ahead(1, |t| token::is_plain_ident(t)) &&
self.look_ahead(2, |t| *t == token::COLON))
((self.look_ahead(1, |t| token::is_plain_ident(t)) &&
self.look_ahead(2, |t| *t == token::COLON))
|| self.look_ahead(1, |t| *t == token::DOTDOT))
}

fn parse_match_expr(&mut self) -> @Expr {
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,10 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) {
match wth {
Some(expr) => {
ibox(s, indent_unit);
word(&mut s.s, ",");
space(&mut s.s);
if !fields.is_empty() {
word(&mut s.s, ",");
space(&mut s.s);
}
word(&mut s.s, "..");
print_expr(s, expr);
end(s);
Expand Down
35 changes: 35 additions & 0 deletions src/test/run-pass/struct-lit-functional-update-no-fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[deriving(Eq,Clone)]
struct Foo<T> {
bar: T,
baz: T
}

pub fn main() {
let foo = Foo {
bar: 0,
baz: 1
};

let foo_ = foo.clone();
let foo = Foo { ..foo };
assert_eq!(foo, foo_);

let foo = Foo {
bar: ~"one",
baz: ~"two"
};

let foo_ = foo.clone();
let foo = Foo { ..foo };
assert_eq!(foo, foo_);
}

5 comments on commit 2258243

@bors
Copy link
Contributor

@bors bors commented on 2258243 Jan 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at lilyball@2258243

@bors
Copy link
Contributor

@bors bors commented on 2258243 Jan 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kballard/rust/empty-functional-update = 2258243 into auto

@bors
Copy link
Contributor

@bors bors commented on 2258243 Jan 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kballard/rust/empty-functional-update = 2258243 merged ok, testing candidate = e3dc5f5

@bors
Copy link
Contributor

@bors bors commented on 2258243 Jan 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 2258243 Jan 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e3dc5f5

Please sign in to comment.