mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 11:21:05 +01:00
Recover an incomplete lambda as a complete expression. (#7435)
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
This commit is contained in:
@@ -62,8 +62,17 @@ auto HandleLambdaAfterParams(Context& context) -> void {
|
||||
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.ReturnErrorOnState();
|
||||
context.PushState(state, StateKind::LambdaBodyFinish);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user