Skip to content

Commit

Permalink
Rollup merge of rust-lang#106919 - compiler-errors:underscore-typo-in…
Browse files Browse the repository at this point in the history
…-field-pat, r=jackh726

Recover `_` as `..` in field pattern
  • Loading branch information
compiler-errors authored Feb 2, 2023
2 parents 8789e53 + 72419ef commit c8f7387
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
35 changes: 21 additions & 14 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,12 +1013,15 @@ impl<'a> Parser<'a> {
}
ate_comma = false;

if self.check(&token::DotDot) || self.token == token::DotDotDot {
if self.check(&token::DotDot)
|| self.check_noexpect(&token::DotDotDot)
|| self.check_keyword(kw::Underscore)
{
etc = true;
let mut etc_sp = self.token.span;

self.recover_one_fewer_dotdot();
self.bump(); // `..` || `...`
self.recover_bad_dot_dot();
self.bump(); // `..` || `...` || `_`

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

/// Recover on `...` as if it were `..` to avoid further errors.
/// Recover on `...` or `_` as if it were `..` to avoid further errors.
/// See issue #46718.
fn recover_one_fewer_dotdot(&self) {
if self.token != token::DotDotDot {
fn recover_bad_dot_dot(&self) {
if self.token == token::DotDot {
return;
}

self.struct_span_err(self.token.span, "expected field pattern, found `...`")
.span_suggestion(
self.token.span,
"to omit remaining fields, use one fewer `.`",
"..",
Applicability::MachineApplicable,
)
.emit();
let token_str = pprust::token_to_string(&self.token);
self.struct_span_err(
self.token.span,
format!("expected field pattern, found `{token_str}`"),
)
.span_suggestion_verbose(
self.token.span,
"to omit remaining fields, use `..`",
"..",
Applicability::MachineApplicable,
)
.emit();
}

fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error: expected field pattern, found `...`
--> $DIR/issue-46718-struct-pattern-dotdotdot.rs:11:55
|
LL | PersonalityInventory { expressivity: exp, ... } => exp
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
| ^^^
|
help: to omit remaining fields, use `..`
|
LL | PersonalityInventory { expressivity: exp, .. } => exp
| ~~

error: aborting due to previous error

7 changes: 6 additions & 1 deletion tests/ui/parser/issue-102806.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ error: expected field pattern, found `...`
--> $DIR/issue-102806.rs:21:22
|
LL | let V3 { z: val, ... } = v;
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
| ^^^
|
help: to omit remaining fields, use `..`
|
LL | let V3 { z: val, .. } = v;
| ~~

error[E0063]: missing fields `x` and `y` in initializer of `V3`
--> $DIR/issue-102806.rs:17:13
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/parser/issues/issue-63135.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ error: expected field pattern, found `...`
--> $DIR/issue-63135.rs:3:8
|
LL | fn i(n{...,f #
| ^^^ help: to omit remaining fields, use one fewer `.`: `..`
| ^^^
|
help: to omit remaining fields, use `..`
|
LL | fn i(n{..,f #
| ~~

error: expected `}`, found `,`
--> $DIR/issue-63135.rs:3:11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ fn main() {
let foo = Some(Foo::Other);

if let Some(Foo::Bar {_}) = foo {}
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR pattern does not mention field `bar` [E0027]
//~^ ERROR expected field pattern, found `_`
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
error: expected identifier, found reserved identifier `_`
error: expected field pattern, found `_`
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:27
|
LL | if let Some(Foo::Bar {_}) = foo {}
| ^ expected identifier, found reserved identifier

error[E0027]: pattern does not mention field `bar`
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:17
|
LL | if let Some(Foo::Bar {_}) = foo {}
| ^^^^^^^^^^^^ missing field `bar`
|
help: include the missing field in the pattern
| ^
|
LL | if let Some(Foo::Bar {_, bar }) = foo {}
| ~~~~~~~
help: if you don't care about this missing field, you can explicitly ignore it
help: to omit remaining fields, use `..`
|
LL | if let Some(Foo::Bar {_, .. }) = foo {}
| ~~~~~~
LL | if let Some(Foo::Bar {..}) = foo {}
| ~~

error: aborting due to 2 previous errors
error: aborting due to previous error

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

0 comments on commit c8f7387

Please sign in to comment.