about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-06-22 11:10:20 +0000
committerbors <bors@rust-lang.org>2018-06-22 11:10:20 +0000
commit01dbfdaf4f45b68b49332b8785262a3a780d0a19 (patch)
treed837ad0585b2ced405e20bc7f08c60162b8e0c4a /src
parente70ff68aafbb923946856fea201e0251869f0dd5 (diff)
parenta99767f64f400f694aec02655af4b5cb5913609e (diff)
Auto merge of #51184 - lambtowolf:master, r=nikomatsakis
Issue #50974 : Suboptimal error in case of duplicate `,` in struct constructor

Fixes #50974
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/parse/parser.rs13
-rw-r--r--src/test/ui/struct-duplicate-comma.rs27
-rw-r--r--src/test/ui/struct-duplicate-comma.stderr13
3 files changed, 51 insertions, 2 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 3955ccb4c42..dfd896fbda4 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 00000000000..d7ee2f220d4
--- /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 <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
+    };
+}
+
diff --git a/src/test/ui/struct-duplicate-comma.stderr b/src/test/ui/struct-duplicate-comma.stderr
new file mode 100644
index 00000000000..06e3b95c248
--- /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
+