Skip to content

Commit 8f4a542

Browse files
committed
Accept .. in incorrect position to avoid further errors
We currently give a specific message when encountering a `..` anywhere other than the end of a pattern. Modify the parser to accept it (while still emitting the error) so that we don't also trigger "missing fields in pattern" errors afterwards.
1 parent 41affd0 commit 8f4a542

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/libsyntax/parse/parser.rs

+10
Original file line numberDiff line numberDiff line change
@@ -3749,6 +3749,16 @@ impl<'a> Parser<'a> {
37493749
err.span_label(self.span,
37503750
"`..` must be in the last position, \
37513751
and cannot have a trailing comma");
3752+
if self.look_ahead(1, |t| {
3753+
t == &token::CloseDelim(token::Brace) || t.is_ident()
3754+
}) {
3755+
// If the struct looks otherwise well formed, recover and continue.
3756+
// This way we avoid "pattern missing fields" errors afterwards.
3757+
err.emit();
3758+
self.bump();
3759+
etc = true;
3760+
break;
3761+
}
37523762
} else {
37533763
err.span_label(self.span, "expected `}`");
37543764
}

src/test/ui/issue-49257.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ struct Point { x: u8, y: u8 }
1818
fn main() {
1919
let p = Point { x: 0, y: 0 };
2020
let Point { .., y } = p; //~ ERROR expected `}`, found `,`
21-
//~| ERROR pattern does not mention fields `x`, `y`
2221
}

src/test/ui/issue-49257.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error: expected `}`, found `,`
44
LL | let Point { .., y } = p; //~ ERROR expected `}`, found `,`
55
| ^ `..` must be in the last position, and cannot have a trailing comma
66

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
7+
error: aborting due to previous error
148

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

0 commit comments

Comments
 (0)