Files
carbon-lang/toolchain/check/testdata/interop/cpp/basics/share_ast.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

101 lines
2.3 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
// EXTRA-ARGS: --share-cpp-ast
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
// --- a.h
#ifndef A_H
#define A_H
inline int GetA() { return 42; }
#endif
// --- b.h
#ifndef B_H
#define B_H
#include "a.h"
inline int GetB() { return 100; }
#endif
// --- a.carbon
library "[[@TEST_NAME]]";
import Cpp library "a.h";
fn CallA() -> i32 {
return Cpp.GetA();
}
// --- fail_a_does_not_leak.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn CallLeakedA() -> i32 {
// This should not be found as we have not imported "a.h".
// CHECK:STDERR: fail_a_does_not_leak.carbon:[[@LINE+2]]:10-17: error: member name `GetA` not found in `Cpp` [MemberNameNotFoundInInstScope]
// CHECK:STDERR:
return Cpp.GetA();
}
// --- a_again.carbon
library "[[@TEST_NAME]]";
import Cpp library "a.h";
fn CallAAgain() -> i32 {
// This should be accepted as we have imported "a.h", even though we will just reuse the version from `a.carbon`.
return Cpp.GetA();
}
// --- b.carbon
library "[[@TEST_NAME]]";
import Cpp library "b.h";
fn CallAIndirect() -> i32 {
return Cpp.GetA();
}
fn CallB() -> i32 {
return Cpp.GetB();
}
// --- pkg_a.carbon
package PkgA;
import Cpp inline '''
template <typename T> void Accept(T) {}
''';
class SameName {}
fn TestPkgA(s: SameName) {
Cpp.Accept(s);
}
// --- pkg_b.carbon
package PkgB;
import Cpp inline '''
template <typename T> void Accept(T) {}
''';
class SameName {}
fn TestPkgB(s: SameName) {
Cpp.Accept(s);
}
// --- use_both_packages.carbon
library "[[@TEST_NAME]]";
import PkgA;
import PkgB;
import Cpp inline '''
// SameName in PkgA and PkgB have distinct C++ DeclContexts (Carbon::PkgA vs
// Carbon::PkgB), so overloading on them in C++ succeeds.
void OverloadByPackage(Carbon::PkgA::SameName*) {}
void OverloadByPackage(Carbon::PkgB::SameName*) {}
''';