Unify more invalid declaration recovery. (#3942)

When we expect a semicolon, we should be able to provide more standard
recovery.

Note this affects a test of `base`, but it looks like a partial
improvement (the prior line was moving too far). Still, it looks like
recovery isn't handling `{}` quite right. I'm not trying to address that
here though -- leaving a TODO.
This commit is contained in:
Jon Ross-Perkins
2024-05-08 22:41:42 +00:00
committed by GitHub
parent 7ef61cd679
commit 5fa341f16a
8 changed files with 65 additions and 83 deletions
+27
View File
@@ -402,6 +402,33 @@ auto Context::ConsumeListToken(NodeKind comma_kind, Lex::TokenKind close_kind,
}
}
auto Context::AddNodeExpectingDeclSemi(StateStackEntry state,
NodeKind node_kind,
Lex::TokenKind decl_kind,
bool is_def_allowed) -> void {
// TODO: This could better handle things like:
// base: { }
// var n: i32;
// ^ Ends up at `n`, instead of `var`.
if (state.has_error) {
RecoverFromDeclError(state, node_kind,
/*skip_past_likely_end=*/true);
return;
}
if (auto semi = ConsumeIf(Lex::TokenKind::Semi)) {
AddNode(node_kind, *semi, state.subtree_start, /*has_error=*/false);
} else {
if (is_def_allowed) {
DiagnoseExpectedDeclSemiOrDefinition(decl_kind);
} else {
DiagnoseExpectedDeclSemi(decl_kind);
}
RecoverFromDeclError(state, node_kind,
/*skip_past_likely_end=*/true);
}
}
auto Context::RecoverFromDeclError(StateStackEntry state, NodeKind node_kind,
bool skip_past_likely_end) -> void {
auto token = state.token;