Initial support for fn expressions. (#7893)

Support for `fn` expressions with the same feature set as `fn`
declarations. In particular, there is no support for captures,
positional parameters, `=>` bodies, or `auto` return types.

Note that this results in two parse nodes for each `=>` token. That's a
bug, but it matches the behavior of function declarations, and will be
fixed in a follow-up change.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith authored and GitHub committed 2026-10-06 20:12:58 +00:00
1 parent 66bf1415ac
commit 1b0a5709e6
24 files changed
+2015 -308

No files matched your search

+11 -3
View File
@@ -67,7 +67,11 @@ static auto BranchAndStartLoopBody(Context& context, Parse::NodeId node_id,
{.break_target = loop_exit_id,
.break_depth = context.scope_stack().cleanup_scope_depth(),
.continue_target = loop_header_id,
.continue_depth = continue_depth});
.continue_depth = continue_depth,
.return_scope_decl_id =
context.scope_stack().IsInFunctionScope()
? context.scope_stack().GetReturnScopeDeclId()
: SemIR::InstId::None});
}
// Finishes emitting the body for a `while`-like loop. Adds a back-edge to the
@@ -276,7 +280,9 @@ auto HandleParseNode(Context& context, Parse::ForStatementId node_id) -> bool {
auto HandleParseNode(Context& context, Parse::BreakStatementStartId node_id)
-> bool {
auto& stack = context.break_continue_stack();
if (stack.empty()) {
if (stack.empty() || !context.scope_stack().IsInFunctionScope() ||
stack.back().return_scope_decl_id !=
context.scope_stack().GetReturnScopeDeclId()) {
CARBON_DIAGNOSTIC(BreakOutsideLoop, Error,
"`break` can only be used in a loop");
context.emitter().Emit(node_id, BreakOutsideLoop);
@@ -301,7 +307,9 @@ auto HandleParseNode(Context& /*context*/, Parse::BreakStatementId /*node_id*/)
auto HandleParseNode(Context& context, Parse::ContinueStatementStartId node_id)
-> bool {
auto& stack = context.break_continue_stack();
if (stack.empty()) {
if (stack.empty() || !context.scope_stack().IsInFunctionScope() ||
stack.back().return_scope_decl_id !=
context.scope_stack().GetReturnScopeDeclId()) {
CARBON_DIAGNOSTIC(ContinueOutsideLoop, Error,
"`continue` can only be used in a loop");
context.emitter().Emit(node_id, ContinueOutsideLoop);