Adjust how invalid declarations pass errors. (#2413)

When there's no semicolon for an invalid EmptyDeclaration, rather than producing nothing, produce an EmptyDeclaration with the original location that led to the error.

Note this removes a direct edit (the only one) of the parse tree's error state. Elsewhere it's an indirection from adding an error node.
This commit is contained in:
Jon Ross-Perkins authored and GitHub committed 2022-11-18 08:02:07 -08:00
1 parent 4c8fdf5124
commit d9ce1827c8
3 files changed
+8 -5

No files matched your search

+6 -5
View File
@@ -705,11 +705,12 @@ auto Parser::HandleDeclarationLoopState() -> void {
CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
"Unrecognized declaration introducer.");
emitter_->Emit(*position_, UnrecognizedDeclaration);
tree_->has_errors_ = true;
if (auto semi = SkipPastLikelyEnd(*position_)) {
AddLeafNode(ParseNodeKind::EmptyDeclaration(), *semi,
/*has_error=*/true);
}
auto cursor = *position_;
auto semi = SkipPastLikelyEnd(cursor);
// Locate the EmptyDeclaration at the semi when found, but use the
// original cursor location for an error when not.
AddLeafNode(ParseNodeKind::EmptyDeclaration(), semi ? *semi : cursor,
/*has_error=*/true);
break;
}
}