Skip to content

Commit c8f7387

Browse files
Rollup merge of rust-lang#106919 - compiler-errors:underscore-typo-in-field-pat, r=jackh726
Recover `_` as `..` in field pattern
2 parents 8789e53 + 72419ef commit c8f7387

File tree

6 files changed

+46
-36
lines changed

6 files changed

+46
-36
lines changed

compiler/rustc_parse/src/parser/pat.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,15 @@ impl<'a> Parser<'a> {
10131013
}
10141014
ate_comma = false;
10151015

1016-
if self.check(&token::DotDot) || self.token == token::DotDotDot {
1016+
if self.check(&token::DotDot)
1017+
|| self.check_noexpect(&token::DotDotDot)
1018+
|| self.check_keyword(kw::Underscore)
1019+
{
10171020
etc = true;
10181021
let mut etc_sp = self.token.span;
10191022

1020-
self.recover_one_fewer_dotdot();
1021-
self.bump(); // `..` || `...`
1023+
self.recover_bad_dot_dot();
1024+
self.bump(); // `..` || `...` || `_`
10221025

10231026
if self.token == token::CloseDelim(Delimiter::Brace) {
10241027
etc_span = Some(etc_sp);
@@ -1111,21 +1114,25 @@ impl<'a> Parser<'a> {
11111114
Ok((fields, etc))
11121115
}
11131116

1114-
/// Recover on `...` as if it were `..` to avoid further errors.
1117+
/// Recover on `...` or `_` as if it were `..` to avoid further errors.
11151118
/// See issue #46718.
1116-
fn recover_one_fewer_dotdot(&self) {
1117-
if self.token != token::DotDotDot {
1119+
fn recover_bad_dot_dot(&self) {
1120+
if self.token == token::DotDot {
11181121
return;
11191122
}
11201123

1121-
self.struct_span_err(self.token.span, "expected field pattern, found `...`")
1122-
.span_suggestion(
1123-
self.token.span,
1124-
"to omit remaining fields, use one fewer `.`",
1125-
"..",
1126-
Applicability::MachineApplicable,
1127-
)
1128-
.emit();
1124+
let token_str = pprust::token_to_string(&self.token);
1125+
self.struct_span_err(
1126+
self.token.span,
1127+
format!("expected field pattern, found `{token_str}`"),
1128+
)
1129+
.span_suggestion_verbose(
1130+
self.token.span,
1131+
"to omit remaining fields, use `..`",
1132+
"..",
1133+
Applicability::MachineApplicable,
1134+
)
1135+
.emit();
11291136
}
11301137

11311138
fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> {

tests/ui/did_you_mean/issue-46718-struct-pattern-dotdotdot.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ error: expected field pattern, found `...`
22
--> $DIR/issue-46718-struct-pattern-dotdotdot.rs:11:55
33
|
44
LL | PersonalityInventory { expressivity: exp, ... } => exp
5-
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
5+
| ^^^
6+
|
7+
help: to omit remaining fields, use `..`
8+
|
9+
LL | PersonalityInventory { expressivity: exp, .. } => exp
10+
| ~~
611

712
error: aborting due to previous error
813

tests/ui/parser/issue-102806.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ error: expected field pattern, found `...`
3232
--> $DIR/issue-102806.rs:21:22
3333
|
3434
LL | let V3 { z: val, ... } = v;
35-
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
35+
| ^^^
36+
|
37+
help: to omit remaining fields, use `..`
38+
|
39+
LL | let V3 { z: val, .. } = v;
40+
| ~~
3641

3742
error[E0063]: missing fields `x` and `y` in initializer of `V3`
3843
--> $DIR/issue-102806.rs:17:13

tests/ui/parser/issues/issue-63135.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ error: expected field pattern, found `...`
2020
--> $DIR/issue-63135.rs:3:8
2121
|
2222
LL | fn i(n{...,f #
23-
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
23+
| ^^^
24+
|
25+
help: to omit remaining fields, use `..`
26+
|
27+
LL | fn i(n{..,f #
28+
| ~~
2429

2530
error: expected `}`, found `,`
2631
--> $DIR/issue-63135.rs:3:11

tests/ui/structs-enums/struct-enum-ignoring-field-with-underscore.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ fn main() {
77
let foo = Some(Foo::Other);
88

99
if let Some(Foo::Bar {_}) = foo {}
10-
//~^ ERROR expected identifier, found reserved identifier `_`
11-
//~| ERROR pattern does not mention field `bar` [E0027]
10+
//~^ ERROR expected field pattern, found `_`
1211
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
error: expected identifier, found reserved identifier `_`
1+
error: expected field pattern, found `_`
22
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:27
33
|
44
LL | if let Some(Foo::Bar {_}) = foo {}
5-
| ^ expected identifier, found reserved identifier
6-
7-
error[E0027]: pattern does not mention field `bar`
8-
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:17
9-
|
10-
LL | if let Some(Foo::Bar {_}) = foo {}
11-
| ^^^^^^^^^^^^ missing field `bar`
12-
|
13-
help: include the missing field in the pattern
5+
| ^
146
|
15-
LL | if let Some(Foo::Bar {_, bar }) = foo {}
16-
| ~~~~~~~
17-
help: if you don't care about this missing field, you can explicitly ignore it
7+
help: to omit remaining fields, use `..`
188
|
19-
LL | if let Some(Foo::Bar {_, .. }) = foo {}
20-
| ~~~~~~
9+
LL | if let Some(Foo::Bar {..}) = foo {}
10+
| ~~
2111

22-
error: aborting due to 2 previous errors
12+
error: aborting due to previous error
2313

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

0 commit comments

Comments
 (0)