Files
carbon-lang/toolchain/check/testdata/interop/cpp/class/export/generic.carbon
T
Richard Smith 09b79784dc Include the Carbon package name in the exported C++ namespace. (#7935)
When exporting `X` from Carbon package `Foo`, expose it to C++ with
fully-qualified name `Carbon::Foo::X`, not simply `Carbon::X`. This
ensures that C++ mangled names properly distinguish between same-named
Carbon entities in different packages.

Name *lookup* into `Carbon::` still uses the same rules as before: it
finds what a `package.` lookup would find in Carbon. So `X` is named
from C++ as `Carbon::X` still, and a lookup for `Carbon::Foo` fails,
even though `X`'s "true name" is now `Carbon::Foo::X`.

Assisted-by: Gemini via Antigravity
2026-10-15 20:26:05 +00:00

84 lines
2.4 KiB
Plaintext

// 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/generic.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/generic.carbon
// --- generic_class.carbon
library "[[@TEST_NAME]]";
import Cpp;
class GenericClass(T: type) {
var t: T;
}
inline Cpp '''
void FGeneric() {
Carbon::GenericClass<int> c;
c.t = 123;
}
''';
// --- specific_alias.carbon
library "[[@TEST_NAME]]";
import Cpp;
class SpecificClass(T: type) {
var t: T;
}
alias AliasInt = SpecificClass(i32);
inline Cpp '''
void FSpecific() {
Carbon::AliasInt a;
a.t = 123;
}
''';
// --- fail_unsupported_specific_arg.carbon
library "[[@TEST_NAME]]";
import Cpp;
// CHECK:STDERR: fail_unsupported_specific_arg.carbon:[[@LINE+2]]:1-33: error: semantics TODO: `failed to map specific type arg to C++` [SemanticsTodo]
// CHECK:STDERR:
class UnsupportedClass(T: type) {
var t: T;
}
alias AliasEmptyTuple = UnsupportedClass(());
inline Cpp '''
void FUnsupported() {
// CHECK:STDERR: fail_unsupported_specific_arg.carbon:[[@LINE+4]]:11: error: semantics TODO: `interop with unsupported type` [SemanticsTodo]
// CHECK:STDERR:
// CHECK:STDERR: fail_unsupported_specific_arg.carbon:[[@LINE+2]]:11: error: no type named 'AliasEmptyTuple' in namespace 'Carbon' [CppInteropParseError]
// CHECK:STDERR:
Carbon::AliasEmptyTuple a;
a.t = 123;
}
''';
// --- fail_todo_generic_class_method_call.carbon
library "[[@TEST_NAME]]";
import Cpp;
class ClassWithMethod(T: type) {
// CHECK:STDERR: fail_todo_generic_class_method_call.carbon:[[@LINE+2]]:3-25: error: semantics TODO: `non-class non-namespace name scope` [SemanticsTodo]
// CHECK:STDERR:
fn M(unused ref self) {}
}
inline Cpp '''
void FMethodCall() {
Carbon::ClassWithMethod<int> c;
// CHECK:STDERR: fail_todo_generic_class_method_call.carbon:[[@LINE+2]]:5: error: no member named 'M' in 'Carbon::Main::ClassWithMethod<int>' [CppInteropParseError]
// CHECK:STDERR:
c.M();
}
''';