Restructure return declaration handling (#7076)

- A function with a return declaration always has exactly one
`ReturnSlotPattern`, representing the whole return declaration (whereas
previously that was omitted for value and reference returns).
- The `ReturnSlotPattern` always has a subpattern with the same form.
`OutParamPattern` already plays that role for initializing forms, and
`TuplePattern` will play that role for tuple forms. This change
introduces `ValueReturnPattern` and `RefReturnPattern` to represent
value and reference return forms.
- As before, the `ReturnSlotPattern` has a corresponding `ReturnSlot`
that represents the output that is initialized by a `return` statement.
Its structure parallels the structure of the `ReturnSlotPattern`, so we
need `ValueReturn` and `RefReturn` insts that correspond to
`ValueReturnPattern` and `RefReturnPattern`.

This is a step toward supporting generic return forms, where the
`ReturnSlotPattern`'s subpattern may be an action: this change ensures
that evaluating the action for a specific form produces the same SemIR
as if the form were concrete to begin with. More speculatively, this
should simplify the implementation of `return` statements with compound
return forms.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Geoff Romer
2026-04-22 23:38:21 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent 3abc334c42
commit 49c7288619
40 changed files with 598 additions and 390 deletions
+27 -5
View File
@@ -1568,6 +1568,23 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver, ParamPatternT inst,
.pretty_name_id = name_id});
}
template <typename ReturnPatternT>
requires SemIR::Internal::HasInstCategory<SemIR::AnyReturnPattern,
ReturnPatternT>
static auto TryResolveTypedInst(ImportRefResolver& resolver,
ReturnPatternT inst,
SemIR::InstId import_inst_id) -> ResolveResult {
auto type_const_id = GetLocalConstantId(resolver, inst.type_id);
if (resolver.HasNewWork()) {
return ResolveResult::Retry();
}
return ResolveResult::Unique<ReturnPatternT>(
resolver, import_inst_id,
{.type_id =
resolver.local_types().GetTypeIdForTypeConstantId(type_const_id)});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::ArrayType inst) -> ResolveResult {
CARBON_CHECK(inst.type_id == SemIR::TypeType::TypeId);
@@ -2227,7 +2244,7 @@ static auto ImportFunctionDecl(ImportContext& context,
.call_param_ranges = import_function.call_param_ranges,
.return_type_inst_id = SemIR::TypeInstId::None,
.return_form_inst_id = SemIR::InstId::None,
.return_patterns_id = SemIR::InstBlockId::None,
.return_pattern_id = SemIR::InstId::None,
.virtual_modifier = import_function.virtual_modifier,
.virtual_index = import_function.virtual_index,
.evaluation_mode = import_function.evaluation_mode}});
@@ -2304,8 +2321,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto generic_data = GetLocalGenericData(resolver, import_function.generic_id);
auto self_param_id =
GetLocalConstantInstId(resolver, import_function.self_param_id);
auto return_patterns =
GetLocalInstBlockContents(resolver, import_function.return_patterns_id);
auto return_pattern_id =
GetLocalConstantInstId(resolver, import_function.return_pattern_id);
auto& new_function = resolver.local_functions().Get(function_id);
if (resolver.HasNewWork()) {
return ResolveResult::Retry(function_const_id,
@@ -2333,8 +2350,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver, SemIR::FormType::TypeId, import_function.return_form_inst_id,
return_form_const_id);
}
new_function.return_patterns_id = GetLocalCanonicalInstBlockId(
resolver, import_function.return_patterns_id, return_patterns);
new_function.return_pattern_id = return_pattern_id;
if (import_function.definition_id.has_value()) {
new_function.definition_id = new_function.first_owning_decl_id;
}
@@ -4266,6 +4282,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
case CARBON_KIND(SemIR::RefParamPattern inst): {
return TryResolveTypedInst(resolver, inst, constant_inst_id);
}
case CARBON_KIND(SemIR::RefReturnPattern inst): {
return TryResolveTypedInst(resolver, inst, constant_inst_id);
}
case CARBON_KIND(SemIR::RequireCompleteType inst): {
return TryResolveTypedInst(resolver, inst);
}
@@ -4320,6 +4339,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
case CARBON_KIND(SemIR::ValueParamPattern inst): {
return TryResolveTypedInst(resolver, inst, constant_inst_id);
}
case CARBON_KIND(SemIR::ValueReturnPattern inst): {
return TryResolveTypedInst(resolver, inst, constant_inst_id);
}
case CARBON_KIND(SemIR::VarParamPattern inst): {
return TryResolveTypedInst(resolver, inst, constant_inst_id);
}