Skip to content

Commit 01dbfda

Browse files
committed
Auto merge of #51184 - lambtowolf:master, r=nikomatsakis
Issue #50974 : Suboptimal error in case of duplicate `,` in struct constructor Fixes #50974
2 parents e70ff68 + a99767f commit 01dbfda

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/libsyntax/parse/parser.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ impl<'a> Parser<'a> {
776776
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
777777
} else {
778778
err.span_label(self.span, "expected identifier");
779+
if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
780+
err.span_suggestion(self.span, "remove this comma", "".into());
781+
}
779782
}
780783
err
781784
}
@@ -2446,8 +2449,14 @@ impl<'a> Parser<'a> {
24462449
Err(mut e) => {
24472450
e.span_label(struct_sp, "while parsing this struct");
24482451
e.emit();
2449-
self.recover_stmt();
2450-
break;
2452+
2453+
// If the next token is a comma, then try to parse
2454+
// what comes next as additional fields, rather than
2455+
// bailing out until next `}`.
2456+
if self.token != token::Comma {
2457+
self.recover_stmt();
2458+
break;
2459+
}
24512460
}
24522461
}
24532462

src/test/ui/struct-duplicate-comma.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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 #50974
14+
15+
struct Foo {
16+
a: u8,
17+
b: u8
18+
}
19+
20+
fn main() {
21+
let bar = Foo {
22+
a: 0,,
23+
//~^ ERROR expected identifier
24+
b: 42
25+
};
26+
}
27+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected identifier, found `,`
2+
--> $DIR/struct-duplicate-comma.rs:22:14
3+
|
4+
LL | let bar = Foo {
5+
| --- while parsing this struct
6+
LL | a: 0,,
7+
| ^
8+
| |
9+
| expected identifier
10+
| help: remove this comma
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)