diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3955ccb4c420a..dfd896fbda449 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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; + } } } diff --git a/src/test/ui/struct-duplicate-comma.rs b/src/test/ui/struct-duplicate-comma.rs new file mode 100644 index 0000000000000..d7ee2f220d470 --- /dev/null +++ b/src/test/ui/struct-duplicate-comma.rs @@ -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 or the MIT license +// , 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 + }; +} + diff --git a/src/test/ui/struct-duplicate-comma.stderr b/src/test/ui/struct-duplicate-comma.stderr new file mode 100644 index 0000000000000..06e3b95c24830 --- /dev/null +++ b/src/test/ui/struct-duplicate-comma.stderr @@ -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 +