Skip to content

Commit 3dd136d

Browse files
committed
Rollup merge of rust-lang#49268 - ordovicia:dotdot-pattern-diag, r=petrochenkov
Better diagnostics for '..' pattern fragment not in the last position Fixes rust-lang#49257.
2 parents 0e4de4a + 3d0ccb2 commit 3dd136d

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/libsyntax/parse/parser.rs

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

src/test/ui/issue-49257.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
//~| ERROR pattern does not mention fields `x`, `y`
22+
}

src/test/ui/issue-49257.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 fields `x`, `y`
8+
--> $DIR/issue-49257.rs:20:9
9+
|
10+
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
11+
| ^^^^^^^^^^^^^^^ missing fields `x`, `y`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0027`.

0 commit comments

Comments
 (0)