Avoid consuming a token after an invalid match_first opening (#7538)

If the `match_first` is not followed by `{` avoid consuming whatever
comes after it. Recover by leaving whatever comes next alone. It could
even be the `FileEnd` token, and then we would crash when we read off
the end of the token stream looking for `FileEnd`.
This commit is contained in:
Dana Jansens
2026-07-20 19:09:16 +00:00
committed by GitHub
parent 99cda60df7
commit 085e45093e
2 changed files with 24 additions and 2 deletions
+6 -1
View File
@@ -26,7 +26,12 @@ auto HandleMatchFirst(Context& context) -> void {
auto HandleMatchFirstFinish(Context& context) -> void {
auto state = context.PopState();
context.AddNode(NodeKind::MatchFirst, context.Consume(), state.has_error);
if (auto closing_token = context.ConsumeIf(Lex::TokenKind::CloseCurlyBrace)) {
context.AddNode(NodeKind::MatchFirst, *closing_token, state.has_error);
} else {
// Recover from error by just moving on.
context.AddNode(NodeKind::MatchFirst, state.token, state.has_error);
}
}
} // namespace Carbon::Parse