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
+4 -5
View File
@@ -2130,13 +2130,12 @@ auto ConvertForExplicitAs(Context& context, Parse::NodeId as_node,
auto ConvertCallArgs(Context& context, SemIR::LocId call_loc_id,
SemIR::InstId self_id,
llvm::ArrayRef<SemIR::InstId> arg_refs,
llvm::ArrayRef<SemIR::InstId> return_arg_ids,
const SemIR::Function& callee,
SemIR::InstId return_arg_id, const SemIR::Function& callee,
SemIR::SpecificId callee_specific_id,
bool is_operator_syntax) -> SemIR::InstBlockId {
auto param_patterns =
context.inst_blocks().GetOrEmpty(callee.param_patterns_id);
auto return_patterns_id = callee.return_patterns_id;
auto return_pattern_id = callee.return_pattern_id;
// The caller should have ensured this callee has the right arity.
CARBON_CHECK(arg_refs.size() == param_patterns.size());
@@ -2153,8 +2152,8 @@ auto ConvertCallArgs(Context& context, SemIR::LocId call_loc_id,
}
return CallerPatternMatch(context, callee_specific_id, callee.self_param_id,
callee.param_patterns_id, return_patterns_id,
self_id, arg_refs, return_arg_ids,
callee.param_patterns_id, return_pattern_id,
self_id, arg_refs, return_arg_id,
is_operator_syntax);
}