Skip to content

Commit 72cfd20

Browse files
committed
Add error for comma after base struct field
`let x = { ..default(), } // This comma is an error`
1 parent 0217315 commit 72cfd20

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/libsyntax/parse/parser.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,7 @@ impl<'a> Parser<'a> {
23142314

23152315
while self.token != token::CloseDelim(token::Brace) {
23162316
if self.eat(&token::DotDot) {
2317+
let exp_span = self.prev_span;
23172318
match self.parse_expr() {
23182319
Ok(e) => {
23192320
base = Some(e);
@@ -2323,6 +2324,16 @@ impl<'a> Parser<'a> {
23232324
self.recover_stmt();
23242325
}
23252326
}
2327+
if self.token == token::Comma {
2328+
let mut err = self.sess.span_diagnostic.mut_span_err(
2329+
exp_span.to(self.prev_span),
2330+
"cannot use a comma after the base struct",
2331+
);
2332+
err.span_suggestion_short(self.span, "remove this comma", "".to_owned());
2333+
err.note("the base struct must always be the last field");
2334+
err.emit();
2335+
self.recover_stmt();
2336+
}
23262337
break;
23272338
}
23282339

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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+
// compile-flags: -Z parse-only
12+
13+
// issue #41834
14+
15+
fn main() {
16+
let foo = Foo {
17+
one: 111,
18+
..Foo::default(),
19+
//~^ ERROR cannot use a comma after struct expansion
20+
};
21+
22+
let foo = Foo {
23+
..Foo::default(),
24+
//~^ ERROR cannot use a comma after struct expansion
25+
one: 111,
26+
};
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: cannot use a comma after the base struct
2+
--> $DIR/struct-field-init-syntax.rs:18:9
3+
|
4+
18 | ..Foo::default(),
5+
| ^^^^^^^^^^^^^^^^- help: remove this comma
6+
|
7+
= note: the base struct must always be the last field
8+
9+
error: cannot use a comma after the base struct
10+
--> $DIR/struct-field-init-syntax.rs:23:9
11+
|
12+
23 | ..Foo::default(),
13+
| ^^^^^^^^^^^^^^^^- help: remove this comma
14+
|
15+
= note: the base struct must always be the last field
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)