about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHidehito Yabuuchi <hdht.ybuc@gmail.com>2018-03-22 20:57:12 +0900
committerHidehito Yabuuchi <hdht.ybuc@gmail.com>2018-03-24 07:54:20 +0900
commit3bfed9e43f5ebe76719e3c30c8c8cefc81b08f80 (patch)
tree16bd8a1e6b3f06212408a9d0397230079e9c3bfd
parentc08480fce0f39f5c9c6db6dde0dccb375ca0ab14 (diff)
downloadrust-3bfed9e43f5ebe76719e3c30c8c8cefc81b08f80.tar.gz
rust-3bfed9e43f5ebe76719e3c30c8c8cefc81b08f80.zip
Better diagnostics for '..' pattern fragment not in the last position
-rw-r--r--src/libsyntax/parse/parser.rs8
-rw-r--r--src/test/ui/issue-49257.rs23
-rw-r--r--src/test/ui/issue-49257.stderr21
3 files changed, 51 insertions, 1 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index a07279acae2..98e2528d30f 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3675,7 +3675,13 @@ impl<'a> Parser<'a> {
                 if self.token != token::CloseDelim(token::Brace) {
                     let token_str = self.this_token_to_string();
                     let mut err = self.fatal(&format!("expected `{}`, found `{}`", "}", token_str));
-                    err.span_label(self.span, "expected `}`");
+                    if self.token == token::Comma { // Issue #49257
+                        err.span_label(self.span,
+                                       "`..` must be in the last position, \
+                                        and cannot have a trailing comma");
+                    } else {
+                        err.span_label(self.span, "expected `}`");
+                    }
                     return Err(err);
                 }
                 etc = true;
diff --git a/src/test/ui/issue-49257.rs b/src/test/ui/issue-49257.rs
new file mode 100644
index 00000000000..75b9e126841
--- /dev/null
+++ b/src/test/ui/issue-49257.rs
@@ -0,0 +1,23 @@
+// 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.
+
+// Test for #49257:
+// emits good diagnostics for `..` pattern fragments not in the last position.
+
+#![allow(unused)]
+
+struct Point { x: u8, y: u8 }
+
+fn main() {
+    let p = Point { x: 0, y: 0 };
+    let Point { .., y } = p; //~ ERROR expected `}`, found `,`
+    //~^ pattern does not mention field `x`
+    //~^^ pattern does not mention field `y`
+}
diff --git a/src/test/ui/issue-49257.stderr b/src/test/ui/issue-49257.stderr
new file mode 100644
index 00000000000..ecaf36520ae
--- /dev/null
+++ b/src/test/ui/issue-49257.stderr
@@ -0,0 +1,21 @@
+error: expected `}`, found `,`
+  --> $DIR/issue-49257.rs:20:19
+   |
+LL |     let Point { .., y } = p; //~ ERROR expected `}`, found `,`
+   |                   ^ `..` must be in the last position, and cannot have a trailing comma
+
+error[E0027]: pattern does not mention field `x`
+  --> $DIR/issue-49257.rs:20:9
+   |
+LL |     let Point { .., y } = p; //~ ERROR expected `}`, found `,`
+   |         ^^^^^^^^^^^^^^^ missing field `x`
+
+error[E0027]: pattern does not mention field `y`
+  --> $DIR/issue-49257.rs:20:9
+   |
+LL |     let Point { .., y } = p; //~ ERROR expected `}`, found `,`
+   |         ^^^^^^^^^^^^^^^ missing field `y`
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0027`.