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

@@ -0,0 +1,37 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/function/export/deduced_return.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/function/export/deduced_return.carbon
// --- non_generic.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn F() -> auto { return 1 as i32; }
inline Cpp '''
int CallF() { return Carbon::F(); }
''';
// --- generic.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn PointerId[T: type](p: T*) -> auto { return p; }
inline Cpp '''
int CallPointerId() {
int x = 0;
int* _Nonnull p = &x;
return *Carbon::PointerId(p);
}
''';