Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover missing comma after match arm #97823

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2718,13 +2718,12 @@ impl<'a> Parser<'a> {
));
}
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])
.map_err(|mut err| {
match (sm.span_to_lines(expr.span), sm.span_to_lines(arm_start_span)) {
(Ok(ref expr_lines), Ok(ref arm_start_lines))
if arm_start_lines.lines[0].end_col
== expr_lines.lines[0].end_col
&& expr_lines.lines.len() == 2
&& this.token == token::FatArrow =>
.or_else(|mut err| {
if this.token == token::FatArrow {
if let Ok(expr_lines) = sm.span_to_lines(expr.span)
&& let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span)
&& arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
&& expr_lines.lines.len() == 2
{
// We check whether there's any trailing code in the parse span,
// if there isn't, we very likely have the following:
Expand All @@ -2743,15 +2742,41 @@ impl<'a> Parser<'a> {
",".to_owned(),
Applicability::MachineApplicable,
);
return Err(err);
}
_ => {
err.span_label(
arrow_span,
"while parsing the `match` arm starting here",
);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this logic be completely replaced with the snapshot parse attempt unconditionally? Or is it that we have advanced too much already and consumed part of the pattern by now specifically because of &?

Copy link
Member Author

@compiler-errors compiler-errors Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, for example, in:

match &Foo {
  &Foo { a: 1 } => ()
  &Foo { a } => (),
}

we would have consumed the & token and Foo { .. } as the operator and RHS of operator & in the second arm before getting here, which means we have to handle it differently. Specifically, we can't recover so that the next arm parse is bound to be successful.

// FIXME(compiler-errors): We could also recover `; PAT =>` here

// Try to parse a following `PAT =>`, if successful
// then we should recover.
let mut snapshot = this.create_snapshot_for_diagnostic();
let pattern_follows = snapshot
.parse_pat_allow_top_alt(
None,
RecoverComma::Yes,
RecoverColon::Yes,
CommaRecoveryMode::EitherTupleOrPipe,
)
.map_err(|err| err.cancel())
.is_ok();
if pattern_follows && snapshot.check(&TokenKind::FatArrow) {
err.cancel();
this.struct_span_err(
hi.shrink_to_hi(),
"expected `,` following `match` arm",
)
.span_suggestion(
hi.shrink_to_hi(),
"missing a comma here to end this `match` arm",
",".to_owned(),
Applicability::MachineApplicable,
)
.emit();
return Ok(true);
}
}
err
err.span_label(arrow_span, "while parsing the `match` arm starting here");
Err(err)
})?;
} else {
this.eat(&token::Comma);
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/parser/match-arm-without-braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ fn main() {
15;
}
match S::get(16) {
Some(Val::Foo) => 17
_ => 18, //~ ERROR expected one of
}
Some(Val::Foo) => 17 //~ ERROR expected `,` following `match` arm
_ => 18,
};
match S::get(19) {
Some(Val::Foo) =>
20; //~ ERROR `match` arm body without braces
Expand Down
10 changes: 3 additions & 7 deletions src/test/ui/parser/match-arm-without-braces.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ LL ~ { 14;
LL ~ 15; }
|

error: expected one of `,`, `.`, `?`, `}`, or an operator, found reserved identifier `_`
--> $DIR/match-arm-without-braces.rs:49:9
error: expected `,` following `match` arm
--> $DIR/match-arm-without-braces.rs:48:29
|
LL | Some(Val::Foo) => 17
| -- - expected one of `,`, `.`, `?`, `}`, or an operator
| |
| while parsing the `match` arm starting here
LL | _ => 18,
| ^ unexpected token
| ^ help: missing a comma here to end this `match` arm: `,`

error: `match` arm body without braces
--> $DIR/match-arm-without-braces.rs:53:11
Expand Down