Support indirect imports of namespaces. (#7122)

When a namespace that was imported from C++ is indirectly imported, find
the corresponding namespace in the current C++ AST and return that
instead. This namespace may have completely different contents than the
one we found before; that's fine. The current file's view of a namespace
depends on what it imported.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-04-28 23:55:18 +00:00
committed by GitHub
parent 7bb86bad66
commit ab0aff91b8
5 changed files with 202 additions and 59 deletions
@@ -10,41 +10,6 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/basics/import/import.carbon
// ============================================================================
// Import C++ namespace indirectly
// ============================================================================
// --- namespace.h
namespace MyNamespace {
class MyClass {};
} // namespace MyNamespace
// --- namespace_api.carbon
library "[[@TEST_NAME]]";
import Cpp library "namespace.h";
alias MyNamespaceAlias = Cpp.MyNamespace;
// --- fail_import_namespace_api.carbon
library "[[@TEST_NAME]]";
import library "namespace_api";
fn F() {
// Imports are not implicitly re-exported, so lookup is expected to fail.
// CHECK:STDERR: fail_import_namespace_api.carbon:[[@LINE+4]]:17: error: member name `MyClass` not found in `Cpp.MyNamespace` [MemberNameNotFoundInInstScope]
// CHECK:STDERR: var unused x: MyNamespaceAlias.MyClass;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
var unused x: MyNamespaceAlias.MyClass;
}
// ============================================================================
// Import C++ struct indirectly
// ============================================================================