mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
A lambda introducer (`fn` in expression context) with no parameters and no body -- for example `(fn)`, where `)` immediately follows `fn` -- left `HandleLambdaAfterParams` calling `ReturnErrorOnState()` without ever emitting a `Lambda` node. The orphaned `LambdaIntroducer` leaf was then left where an expression was required, so `Parse` produced a tree that failed its own verification and aborted via `CARBON_FATAL`. Recover the way `HandleLambdaBody` already does for a missing body after a return type: emit a placeholder `InvalidParse` body and finish a complete `Lambda` node, so the lambda stays a valid expression and the surrounding construct (here a `ParenExpr`) extracts cleanly. Found by fuzzing. Assisted-by: Claude Code
117 lines
4.4 KiB
C++
117 lines
4.4 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/parse/context.h"
|
|
#include "toolchain/parse/handle.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
auto HandleLambdaIntroducer(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
context.AddLeafNode(NodeKind::LambdaIntroducer, context.Consume());
|
|
context.PushState(state, StateKind::LambdaAfterIntroducer);
|
|
}
|
|
|
|
auto HandleLambdaAfterIntroducer(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
|
|
context.PushState(state, StateKind::LambdaAfterImplicitParams);
|
|
context.PushState(StateKind::PatternListAsImplicit);
|
|
} else if (context.PositionIs(Lex::TokenKind::OpenParen)) {
|
|
context.PushState(state, StateKind::LambdaAfterParams);
|
|
context.PushState(StateKind::PatternListAsExplicit);
|
|
} else {
|
|
// No implicit or explicit params.
|
|
context.PushState(state, StateKind::LambdaAfterParams);
|
|
}
|
|
}
|
|
|
|
auto HandleLambdaAfterImplicitParams(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (context.PositionIs(Lex::TokenKind::OpenParen)) {
|
|
context.PushState(state, StateKind::LambdaAfterParams);
|
|
context.PushState(StateKind::PatternListAsExplicit);
|
|
} else {
|
|
// No explicit params after implicit params.
|
|
context.PushState(state, StateKind::LambdaAfterParams);
|
|
}
|
|
}
|
|
|
|
auto HandleLambdaAfterParams(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (context.PositionIs(Lex::TokenKind::MinusGreater)) {
|
|
// Has return type.
|
|
context.PushState(state, StateKind::LambdaBody);
|
|
context.PushState(StateKind::FunctionReturnTypeFinish);
|
|
context.ConsumeAndDiscard();
|
|
context.PushStateForExpr(PrecedenceGroup::ForType());
|
|
} else if (context.PositionIs(Lex::TokenKind::EqualGreater)) {
|
|
// Terse body `=> expr`
|
|
context.AddLeafNode(NodeKind::TerseBodyArrow, context.Consume());
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
|
|
} else if (context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
|
|
// Block body `{ ... }`
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
context.PushState(StateKind::CodeBlock);
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ExpectedLambdaBody, Error,
|
|
"expected `->`, `=>`, or `{{`");
|
|
context.emitter().Emit(*context.position(), ExpectedLambdaBody);
|
|
|
|
// Add a dummy node for the missing body without consuming the current
|
|
// token, then bundle everything into a complete lambda node. This keeps the
|
|
// lambda a valid expression for error recovery -- otherwise the orphaned
|
|
// `LambdaIntroducer` would be left where an expression is required, for
|
|
// example in `(fn)`.
|
|
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
|
/*has_error=*/true);
|
|
|
|
state.has_error = true;
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
}
|
|
}
|
|
|
|
auto HandleLambdaBody(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
// We arrive here after parsing return type.
|
|
// So we look for `=>` or `{`.
|
|
|
|
if (context.PositionIs(Lex::TokenKind::EqualGreater)) {
|
|
// Terse body `=> expr`
|
|
context.AddLeafNode(NodeKind::TerseBodyArrow, context.Consume());
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
|
|
} else if (context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
|
|
// Block body `{ ... }`
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
context.PushState(StateKind::CodeBlock);
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ExpectedLambdaBodyAfterReturnType, Error,
|
|
"expected `=>` or `{{` after return type");
|
|
context.emitter().Emit(*context.position(),
|
|
ExpectedLambdaBodyAfterReturnType);
|
|
|
|
// Add a dummy node for the missing body without consuming the current
|
|
// token.
|
|
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
|
/*has_error=*/true);
|
|
|
|
state.has_error = true;
|
|
// Bundle all nodes into a complete lambda node.
|
|
context.PushState(state, StateKind::LambdaBodyFinish);
|
|
}
|
|
}
|
|
|
|
auto HandleLambdaBodyFinish(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
context.AddNode(NodeKind::Lambda, state.token, state.has_error);
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|