Support calls to generic functions with deduced return types. (#7981)

When a deduced return type depends on the function's generic parameters,
it's computed by the definition's eval block, so resolve the specific's
definition at the call. Calling a specific during its own instantiation,
before the point where its return type is deduced, is diagnosed like any
other use of the function before its return type is known. Also allow
exporting such functions to C++.

Resolving such a specific from within a checked generic can perform
template actions with symbolic arguments. Perform delayed actions in a
discarded generic region so the instructions they create aren't attached
to the generic being checked, and check that evaluating a specific
doesn't add anything to an enclosing generic region.

Assisted-by: Claude via Antigravity
This commit is contained in:
Richard Smith authored and GitHub committed 2026-10-16 19:41:02 +00:00
1 parent 6d80ceb58e
commit 0d3b606a56
14 files changed
+1679 -36

No files matched your search

+45
View File
@@ -546,6 +546,51 @@ auto SetDeducedReturnType(Context& context, SemIR::FunctionId function_id,
}
}
auto ResolveReturnTypeInSpecific(Context& context, SemIR::LocId loc_id,
const SemIR::Function& function,
SemIR::SpecificId specific_id) -> bool {
if (function.has_pending_deduced_return_type()) {
return false;
}
// A declared return type is known once the declaration of the specific is
// resolved.
if (!function.has_deduced_return_type() || !specific_id.has_value()) {
return true;
}
// So is a deduced return type that doesn't depend on the generic's
// parameters.
auto pattern_const_id =
context.constant_values().Get(function.return_pattern_id);
if (!pattern_const_id.is_symbolic()) {
return true;
}
// Otherwise, the return type and the return pattern were created
// within the definition of the generic, and are computed by its eval block.
if (!ResolveSpecificDefinition(context, loc_id, specific_id)) {
// We're within the definition of the function, after its return type was
// deduced. In the self specific, the return type is just the deduced type,
// but we can't compute it for any other specific until the definition is
// complete.
return specific_id ==
context.generics().GetSelfSpecific(function.generic_id);
}
// If we're in the middle of resolving the definition of this specific, its
// eval block is only partially evaluated. `SetDeducedReturnType` creates the
// return pattern after the return type and form, so if the return pattern
// has a value, the instantiation has reached the point where the return type
// was deduced.
//
// Note that a return pattern never evaluates to `NotConstant`, so this can
// only mean that we haven't reached it yet.
return SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
function.return_pattern_id) !=
SemIR::ConstantId::NotConstant;
}
auto StartFunctionSignature(Context& context) -> void {
context.scope_stack().PushForDeclName();
context.inst_block_stack().Push();