Auto merge of #51184 - lambtowolf:master, r=nikomatsakis

Issue #50974 : Suboptimal error in case of duplicate `,` in struct constructor

Fixes #50974
This commit is contained in:
bors 2018-06-22 11:10:20 +00:00
commit 01dbfdaf4f
3 changed files with 51 additions and 2 deletions

View file

@ -776,6 +776,9 @@ impl<'a> Parser<'a> {
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
} else {
err.span_label(self.span, "expected identifier");
if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
err.span_suggestion(self.span, "remove this comma", "".into());
}
}
err
}
@ -2446,8 +2449,14 @@ impl<'a> Parser<'a> {
Err(mut e) => {
e.span_label(struct_sp, "while parsing this struct");
e.emit();
self.recover_stmt();
break;
// If the next token is a comma, then try to parse
// what comes next as additional fields, rather than
// bailing out until next `}`.
if self.token != token::Comma {
self.recover_stmt();
break;
}
}
}

View file

@ -0,0 +1,27 @@
// Copyright 2018 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.
// compile-flags: -Z parse-only
// Issue #50974
struct Foo {
a: u8,
b: u8
}
fn main() {
let bar = Foo {
a: 0,,
//~^ ERROR expected identifier
b: 42
};
}

View file

@ -0,0 +1,13 @@
error: expected identifier, found `,`
--> $DIR/struct-duplicate-comma.rs:22:14
|
LL | let bar = Foo {
| --- while parsing this struct
LL | a: 0,,
| ^
| |
| expected identifier
| help: remove this comma
error: aborting due to previous error