mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 05:48:32 +00:00
Add support for -> auto deduced return types. (#7931)
The return type is deduced from the function's `return` statements (currently required to all have the same type), or from its first `returned var`. Calls to generic functions with deduced return types are not yet supported. When a function has a deduced return type, we defer the return type portion of the callee pattern match until deduction, and during deduction we extend the decl block and the pattern block with the return-related instructions. Conversions for `return expr;` statements are deferred until the return type has been deduced. The design requires that we take the common type of the returned types as the deduced return type, so we can't in general perform any conversions for returned expressions until we reach the end of the function. This is accomplished by inserting a `splice_block` where the conversions will go, and later updating that instruction to contain the conversion. Assisted-by: Claude Opus 5.5 via Antigravity
This commit is contained in:
1 parent
1c69fe3447
commit
bf5d2a2ace
41 files changed
+2631
-214
No files matched your search
@@ -26,6 +26,8 @@ auto ScopeStack::sem_ir() const -> const SemIR::File& {
|
||||
|
||||
auto ScopeStack::VerifyOnFinish() const -> void {
|
||||
CARBON_CHECK(return_scope_stack_.empty(), "{0}", return_scope_stack_.size());
|
||||
CARBON_CHECK(pending_return_stack_.empty(), "{0}",
|
||||
pending_return_stack_.all_values_size());
|
||||
CARBON_CHECK(break_continue_stack_.empty(), "{0}",
|
||||
break_continue_stack_.size());
|
||||
CARBON_CHECK(scope_stack_.empty(), "{0}", scope_stack_.size());
|
||||
@@ -136,6 +138,7 @@ auto ScopeStack::PushForFunctionBody(SemIR::InstId scope_inst_id) -> void {
|
||||
|
||||
return_scope_stack_.push_back(
|
||||
{.decl_id = scope_inst_id, .cleanup_scope_depth = cleanup_scope_depth()});
|
||||
pending_return_stack_.PushArray();
|
||||
}
|
||||
|
||||
auto ScopeStack::Pop(bool check_unused) -> void {
|
||||
@@ -172,6 +175,9 @@ auto ScopeStack::Pop(bool check_unused) -> void {
|
||||
|
||||
if (return_scope_stack_.back().decl_id == scope.scope_inst_id) {
|
||||
// Leaving the function scope.
|
||||
CARBON_CHECK(pending_return_stack_.PeekArray().empty(),
|
||||
"Leaving function with pending `return`s");
|
||||
pending_return_stack_.PopArray();
|
||||
return_scope_stack_.pop_back();
|
||||
} else {
|
||||
if (return_scope_stack_.back().nested_scope_index == scope.index) {
|
||||
|
||||
Reference in new issue
Block a user