Refactor handling of cross-package imports. (#3783)

This revamps the support for cross-package imports, making them look
more like a namespace. The planned model is mentioned on
[#toolchain](https://discord.com/channels/655572317891461132/655578254970716160/1217586076022210670).
This does not implement name lookup into the new namespace structure.

A few key changes in this PR (it's a little sprawling) are:

- Moves logic for adding package imports from context.* to import.*
- Remove SemIR::Import, which was the prior model. This is instead now a
SemIR::Namespace with the NameScope getting a new import_ir_scopes
field.
- Allow SemIR::Namespace to use Parse::ImportDirectiveId in addition to
the prior Parse::NamespaceId
- The import_ir_scopes field includes a NameScopeId so that as we
traverse to child namespaces, we can directly perform name lookup in the
other IR.
- is_closed_import now tracks whether a namespace comes from a different
package. This has a diagnostic implemented in decl_name_stack.
This commit is contained in:
Jon Ross-Perkins
2024-03-14 22:03:30 +00:00
committed by GitHub
parent 39462d49b4
commit 8567e02aa7
22 changed files with 487 additions and 169 deletions
+13
View File
@@ -214,6 +214,19 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context,
auto scope_id = resolved_inst.As<SemIR::Namespace>().name_scope_id;
name_context.state = NameContext::State::Resolved;
name_context.target_scope_id = scope_id;
auto& scope = context_->name_scopes().Get(scope_id);
if (scope.is_closed_import) {
CARBON_DIAGNOSTIC(QualifiedDeclOutsidePackage, Error,
"Imported packages cannot be used for declarations.");
CARBON_DIAGNOSTIC(QualifiedDeclOutsidePackageSource, Note,
"Package imported here.");
context_->emitter()
.Build(name_context.node_id, QualifiedDeclOutsidePackage)
.Note(scope.inst_id, QualifiedDeclOutsidePackageSource)
.Emit();
// Only error once per package.
scope.is_closed_import = false;
}
if (!is_unqualified) {
PushNameQualifierScope(*context_, name_context.resolved_inst_id,
scope_id,