From 3bfed9e43f5ebe76719e3c30c8c8cefc81b08f80 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Thu, 22 Mar 2018 20:57:12 +0900 Subject: [PATCH 1/3] Better diagnostics for '..' pattern fragment not in the last position --- src/libsyntax/parse/parser.rs | 8 +++++++- src/test/ui/issue-49257.rs | 23 +++++++++++++++++++++++ src/test/ui/issue-49257.stderr | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/issue-49257.rs create mode 100644 src/test/ui/issue-49257.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a07279acae2d3..98e2528d30f24 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 0000000000000..75b9e12684165 --- /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 or the MIT license +// , 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 0000000000000..ecaf36520aec9 --- /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`. From f8fc5c0523c174e27e670525f5fa22d087315507 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Fri, 23 Mar 2018 01:15:27 +0900 Subject: [PATCH 2/3] Fix error annotations in test --- src/test/ui/issue-49257.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/issue-49257.rs b/src/test/ui/issue-49257.rs index 75b9e12684165..61883294594bc 100644 --- a/src/test/ui/issue-49257.rs +++ b/src/test/ui/issue-49257.rs @@ -18,6 +18,6 @@ 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` + //~| ERROR pattern does not mention field `x` + //~| ERROR pattern does not mention field `y` } From 3d0ccb2a22a229ddee842c0c1bf4ca231e77c78b Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Sat, 24 Mar 2018 08:10:51 +0900 Subject: [PATCH 3/3] Fix test for PR #49268 --- src/test/ui/issue-49257.rs | 3 +-- src/test/ui/issue-49257.stderr | 12 +++--------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/test/ui/issue-49257.rs b/src/test/ui/issue-49257.rs index 61883294594bc..a319849223740 100644 --- a/src/test/ui/issue-49257.rs +++ b/src/test/ui/issue-49257.rs @@ -18,6 +18,5 @@ struct Point { x: u8, y: u8 } fn main() { let p = Point { x: 0, y: 0 }; let Point { .., y } = p; //~ ERROR expected `}`, found `,` - //~| ERROR pattern does not mention field `x` - //~| ERROR pattern does not mention field `y` + //~| ERROR pattern does not mention fields `x`, `y` } diff --git a/src/test/ui/issue-49257.stderr b/src/test/ui/issue-49257.stderr index ecaf36520aec9..fec990764bb14 100644 --- a/src/test/ui/issue-49257.stderr +++ b/src/test/ui/issue-49257.stderr @@ -4,18 +4,12 @@ error: expected `}`, found `,` 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` +error[E0027]: pattern does not mention fields `x`, `y` --> $DIR/issue-49257.rs:20:9 | LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,` - | ^^^^^^^^^^^^^^^ missing field `x` + | ^^^^^^^^^^^^^^^ missing fields `x`, `y` -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 +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0027`.