mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user