Skip to content

Commit 8e9a5e9

Browse files
Rollup merge of rust-lang#51184 - lambtowolf:master, r=nikomatsakis
Issue rust-lang#50974 : Suboptimal error in case of duplicate `,` in struct constructor Fixes rust-lang#50974
2 parents 4a9c58c + 01559fb commit 8e9a5e9

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,9 @@ impl<'a> Parser<'a> {
767767
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
768768
} else {
769769
err.span_label(self.span, "expected identifier");
770+
if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
771+
err.span_suggestion(self.span, "remove this comma", "".into());
772+
}
770773
}
771774
err
772775
}
@@ -2527,8 +2530,14 @@ impl<'a> Parser<'a> {
25272530
Err(mut e) => {
25282531
e.span_label(struct_sp, "while parsing this struct");
25292532
e.emit();
2530-
self.recover_stmt();
2531-
break;
2533+
2534+
// If the next token is a comma, then try to parse
2535+
// what comes next as additional fields, rather than
2536+
// bailing out until next `}`.
2537+
if self.token != token::Comma {
2538+
self.recover_stmt();
2539+
break;
2540+
}
25322541
}
25332542
}
25342543

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

Lines changed: 27 additions & 0 deletions
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+
Lines changed: 13 additions & 0 deletions
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)