Fix a crash on invalid found by fuzzing. (#3404)

When a `namespace` keyword has no `;` following it, we recover by
building a parse tree `Namespace` node from the `Namespace` token (as
there isn't a `;` token). Allow this correspondence on errors.

Also teach the diagnostics in this case to avoid the end-of-file token
as that's almost always going to be a less meaningful location. Instead,
we can point at the introducer which should at least be in the code that
led to the error.
This commit is contained in:
Chandler Carruth
2023-11-16 18:26:39 +00:00
committed by GitHub
parent 204c04dbb9
commit d3eae6d1f0
3 changed files with 69 additions and 2 deletions
@@ -27,7 +27,13 @@ static auto HandleDeclNameAndParams(Context& context, State after_name)
CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
"`{0}` introducer should be followed by a name.",
Lex::TokenKind);
context.emitter().Emit(*context.position(), ExpectedDeclName,
Lex::Token location = *context.position();
if (context.tokens().GetKind(location) == Lex::TokenKind::EndOfFile) {
// The end of file is often an especially unhelpful location. If that's
// the best we can do here, back up the location to the introducer itself.
location = state.token;
}
context.emitter().Emit(location, ExpectedDeclName,
context.tokens().GetKind(state.token));
context.ReturnErrorOnState();
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),