Improve error recovery in binding patterns (#4309)

Co-authored-by: Richard Smith <richard@metafoo.co.uk>

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
This commit is contained in:
josh11b
2024-09-12 21:12:04 +00:00
committed by GitHub
co-authored by Richard Smith Josh L
parent a8d4f068e3
commit 88f3b3470f
2 changed files with 59 additions and 10 deletions
+10 -10
View File
@@ -26,14 +26,12 @@ auto HandleBindingPattern(Context& context) -> void {
// Handle an invalid pattern introducer for parameters and variables.
auto on_error = [&]() {
CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
"Expected binding pattern.");
context.emitter().Emit(*context.position(), ExpectedBindingPattern);
// Add a placeholder for the type.
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
/*has_error=*/true);
state.has_error = true;
context.PushState(state, State::BindingPatternFinishAsRegular);
if (!state.has_error) {
CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
"Expected binding pattern.");
context.emitter().Emit(*context.position(), ExpectedBindingPattern);
state.has_error = true;
}
};
// The first item should be an identifier or `self`.
@@ -53,7 +51,6 @@ auto HandleBindingPattern(Context& context) -> void {
context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
/*has_error=*/true);
on_error();
return;
}
if (auto kind = context.PositionKind();
@@ -67,7 +64,10 @@ auto HandleBindingPattern(Context& context) -> void {
context.PushStateForExpr(PrecedenceGroup::ForType());
} else {
on_error();
return;
// Add a placeholder for the type.
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
/*has_error=*/true);
context.PushState(state, State::BindingPatternFinishAsRegular);
}
}