Support C++ calling Carbon functions with ref parameters (#7107)

When creating the C++ thunk, make the parameters references if the
corresponding callee parameters are `ref`s.

When creating the Carbon thunk, tag the call arguments as `ref` if the
corresponding callee parameters are `ref`s.
This commit is contained in:
Nicholas Bishop
2026-04-23 23:45:26 +00:00
committed by GitHub
parent 709776ad1c
commit 3f63cf4b10
4 changed files with 151 additions and 30 deletions
@@ -0,0 +1,84 @@
// 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/class/export/method.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/method.carbon
// --- static_method.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M();
}
inline Cpp '''
void F() {
Carbon::C::M();
}
''';
// --- method_lvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M[self: Self]();
}
inline Cpp '''
void F() {
Carbon::C c;
c.M();
}
''';
// --- method_rvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M[self: Self]();
}
inline Cpp '''
void F() {
Carbon::C().M();
}
''';
// --- ref_method_lvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M[ref self: Self]();
}
inline Cpp '''
void F() {
Carbon::C c;
c.M();
}
''';
// --- todo_ref_method_rvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M[ref self: Self]();
}
inline Cpp '''
void F() {
// TODO: this should be disallowed.
Carbon::C().M();
}
''';
@@ -20,10 +20,6 @@ fn F3(_: i32) {}
fn HasGenericArg(T:! type, a: T) { a; }
fn HasDeducedArg[T:! type](a: T) { a; }
class C {
fn Method[self: Self]() { self; }
}
// --- function.carbon
library "[[@TEST_NAME]]";
@@ -57,6 +53,21 @@ void G() {
}
''';
// --- ref_arg.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn F(ref n: i32);
inline Cpp '''
void G() {
int n = 0;
Carbon::F(n);
}
''';
// --- using.carbon
library "[[@TEST_NAME]]";
@@ -113,14 +124,3 @@ void G() {
Carbon::Other::HasDeducedArg(123);
}
''';
// --- method.carbon
library "[[@TEST_NAME]]";
import Other;
import Cpp inline '''
void G() {
Carbon::Other::C().Method();
}
''';