Rollup merge of #97823 - compiler-errors:missing-comma-match-arm, r=estebank
Recover missing comma after match arm If we're missing a comma after a match arm expression, try parsing another pattern and a following `=>`. If we find both of those, then recover by suggesting to insert a `,`. Fixes #80112
This commit is contained in:
commit
a90c5a3c69
3 changed files with 44 additions and 23 deletions
|
@ -2718,13 +2718,12 @@ impl<'a> Parser<'a> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])
|
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])
|
||||||
.map_err(|mut err| {
|
.or_else(|mut err| {
|
||||||
match (sm.span_to_lines(expr.span), sm.span_to_lines(arm_start_span)) {
|
if this.token == token::FatArrow {
|
||||||
(Ok(ref expr_lines), Ok(ref arm_start_lines))
|
if let Ok(expr_lines) = sm.span_to_lines(expr.span)
|
||||||
if arm_start_lines.lines[0].end_col
|
&& let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span)
|
||||||
== expr_lines.lines[0].end_col
|
&& arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
|
||||||
&& expr_lines.lines.len() == 2
|
&& expr_lines.lines.len() == 2
|
||||||
&& this.token == token::FatArrow =>
|
|
||||||
{
|
{
|
||||||
// We check whether there's any trailing code in the parse span,
|
// We check whether there's any trailing code in the parse span,
|
||||||
// if there isn't, we very likely have the following:
|
// if there isn't, we very likely have the following:
|
||||||
|
@ -2743,15 +2742,41 @@ impl<'a> Parser<'a> {
|
||||||
",".to_owned(),
|
",".to_owned(),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
return Err(err);
|
||||||
}
|
}
|
||||||
_ => {
|
} else {
|
||||||
err.span_label(
|
// FIXME(compiler-errors): We could also recover `; PAT =>` here
|
||||||
arrow_span,
|
|
||||||
"while parsing the `match` arm starting 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 {
|
} else {
|
||||||
this.eat(&token::Comma);
|
this.eat(&token::Comma);
|
||||||
|
|
|
@ -45,9 +45,9 @@ fn main() {
|
||||||
15;
|
15;
|
||||||
}
|
}
|
||||||
match S::get(16) {
|
match S::get(16) {
|
||||||
Some(Val::Foo) => 17
|
Some(Val::Foo) => 17 //~ ERROR expected `,` following `match` arm
|
||||||
_ => 18, //~ ERROR expected one of
|
_ => 18,
|
||||||
}
|
};
|
||||||
match S::get(19) {
|
match S::get(19) {
|
||||||
Some(Val::Foo) =>
|
Some(Val::Foo) =>
|
||||||
20; //~ ERROR `match` arm body without braces
|
20; //~ ERROR `match` arm body without braces
|
||||||
|
|
|
@ -52,15 +52,11 @@ LL ~ { 14;
|
||||||
LL ~ 15; }
|
LL ~ 15; }
|
||||||
|
|
|
|
||||||
|
|
||||||
error: expected one of `,`, `.`, `?`, `}`, or an operator, found reserved identifier `_`
|
error: expected `,` following `match` arm
|
||||||
--> $DIR/match-arm-without-braces.rs:49:9
|
--> $DIR/match-arm-without-braces.rs:48:29
|
||||||
|
|
|
|
||||||
LL | Some(Val::Foo) => 17
|
LL | Some(Val::Foo) => 17
|
||||||
| -- - expected one of `,`, `.`, `?`, `}`, or an operator
|
| ^ help: missing a comma here to end this `match` arm: `,`
|
||||||
| |
|
|
||||||
| while parsing the `match` arm starting here
|
|
||||||
LL | _ => 18,
|
|
||||||
| ^ unexpected token
|
|
||||||
|
|
||||||
error: `match` arm body without braces
|
error: `match` arm body without braces
|
||||||
--> $DIR/match-arm-without-braces.rs:53:11
|
--> $DIR/match-arm-without-braces.rs:53:11
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue