Support for terse => function bodies (#7895)

Treat the function as having a `-> auto` return type. Reject calls to
functions that still have a non-deduced `-> auto` return type, and
rewrite the function to have the actual return type once it is known.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith authored and GitHub committed 2026-10-16 21:48:48 +00:00
1 parent d08805f1a6
commit b766aa4d16
30 files changed
+1323 -322

No files matched your search

+32
View File
@@ -98,6 +98,28 @@ auto PopReturnSpecifier(Context& context) -> ReturnSpecifierInfo {
.form = context.PopReturnForm()};
}
auto AdjustReturnSpecifierForTerseDefinition(
Context& context, Parse::NodeId start_node_id,
ReturnSpecifierInfo& return_specifier) -> void {
if (return_specifier.has_return_specifier()) {
CARBON_DIAGNOSTIC(ReturnTypeInTerseFunction, Error,
"cannot specify a return {0:type|form} with `=>`",
Diagnostics::BoolAsSelect);
context.emitter().Emit(
return_specifier.node_id, ReturnTypeInTerseFunction,
context.parse_tree().node_kind(return_specifier.node_id) ==
Parse::NodeKind::ReturnType);
return;
}
// The return type is deduced from the body, as if `-> auto` were specified.
// TODO: We always use an initializing form; should we use `->? auto`
// semantics instead?
return_specifier = {
.node_id = start_node_id,
.deduced_return_kind = SemIR::Function::DeducedReturnKind::Init};
}
auto IsValidBuiltinDeclaration(Context& context,
const SemIR::Function& function,
SemIR::BuiltinFunctionKind builtin_kind)
@@ -840,6 +862,16 @@ auto CheckFunctionReturnOnFinish(Context& context, Parse::NodeId node_id,
}
}
auto CheckFunctionTerseBody(Context& context,
Parse::AnyTerseFunctionDefinitionId node_id,
SemIR::FunctionId function_id,
SemIR::InstId body_expr_id) -> void {
// A terse body `=> expr` is equivalent to `{ return expr; }`. The return type
// is deduced from the `return`, just like for `-> auto`.
BuildReturnWithExpr(context, SemIR::LocId(body_expr_id), body_expr_id);
CheckFunctionReturnOnFinish(context, node_id, function_id);
}
auto FinishFunctionDefinition(Context& context, SemIR::FunctionId function_id)
-> void {
context.inst_block_stack().Pop();