Skip to content

Commit 25c5b03

Browse files
committed
Better diagnostics for '..' pattern fragment not in the last position
1 parent 75af15e commit 25c5b03

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/libsyntax/parse/parser.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3672,7 +3672,13 @@ impl<'a> Parser<'a> {
36723672
if self.token != token::CloseDelim(token::Brace) {
36733673
let token_str = self.this_token_to_string();
36743674
let mut err = self.fatal(&format!("expected `{}`, found `{}`", "}", token_str));
3675-
err.span_label(self.span, "expected `}`");
3675+
if self.token == token::Comma { // Issue #49257
3676+
err.span_label(self.span,
3677+
"`..` must be in the last position, \
3678+
and cannot have a trailing comma");
3679+
} else {
3680+
err.span_label(self.span, "expected `}`");
3681+
}
36763682
return Err(err);
36773683
}
36783684
etc = true;

src/test/ui/issue-49257.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test for #49257:
12+
// emits good diagnostics for `..` pattern fragments not in the last position.
13+
14+
#![allow(unused)]
15+
16+
struct Point { x: u8, y: u8 }
17+
18+
fn main() {
19+
let p = Point { x: 0, y: 0 };
20+
let Point { .., y } = p; //~ ERROR expected `}`, found `,`
21+
//~^ pattern does not mention field `x`
22+
//~^^ pattern does not mention field `y`
23+
}

src/test/ui/issue-49257.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: expected `}`, found `,`
2+
--> $DIR/issue-49257.rs:20:19
3+
|
4+
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
5+
| ^ `..` must be in the last position, and cannot have a trailing comma
6+
7+
error[E0027]: pattern does not mention field `x`
8+
--> $DIR/issue-49257.rs:20:9
9+
|
10+
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
11+
| ^^^^^^^^^^^^^^^ missing field `x`
12+
13+
error[E0027]: pattern does not mention field `y`
14+
--> $DIR/issue-49257.rs:20:9
15+
|
16+
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
17+
| ^^^^^^^^^^^^^^^ missing field `y`
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0027`.

0 commit comments

Comments
 (0)