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
+16 -3
View File
@@ -3699,11 +3699,24 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto name_id = GetLocalNameId(resolver, name_scope.name_id());
namespace_decl.name_scope_id =
resolver.local_name_scopes().Add(inst_id, name_id, parent_scope_id);
auto& local_scope =
resolver.local_name_scopes().Get(namespace_decl.name_scope_id);
// Namespaces from this package are eagerly imported, so anything we load here
// must be a closed import.
resolver.local_name_scopes()
.Get(namespace_decl.name_scope_id)
.set_is_closed_import(true);
local_scope.set_is_closed_import(true);
// If this was a C++ namespace, connect it to the corresponding C++
// declaration in this file.
if (name_scope.is_cpp_scope()) {
if (auto key = FindCorrespondingClangDeclKey(
resolver.local_context(), SemIR::LocId(inst_id),
resolver.import_ir(), name_scope.clang_decl_context_id())) {
auto clang_decl_id = resolver.local_context().clang_decls().Add(
{.key = *key, .inst_id = inst_id});
local_scope.set_clang_decl_context_id(clang_decl_id, true);
}
}
auto namespace_const_id =
ReplacePlaceholderImportedInst(resolver, inst_id, namespace_decl);
return {.const_id = namespace_const_id};