Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better diagnostics for '..' pattern fragment not in the last position #49268

Merged
merged 3 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/issue-49257.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 `,`
//~| ERROR pattern does not mention fields `x`, `y`
}
15 changes: 15 additions & 0 deletions src/test/ui/issue-49257.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 fields `x`, `y`
--> $DIR/issue-49257.rs:20:9
|
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
| ^^^^^^^^^^^^^^^ missing fields `x`, `y`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0027`.