Skip to content

Commit f40ccbc

Browse files
dwblaikiebricknerb
authored andcommitted
Refactor interface and impl NameScope importing to resemble classes (carbon-language#5585)
Similar to the class change made in c6f25e9 but without tests as it doesn't appear that the same bug is reachable for these cases at the moment - but seems good to match the approach in case cycles appear in the future and to make the code consistent. The impl case didn't seem to be able to use the new common utility function, since it splits the two pieces of work and only does the second conditionally.
1 parent d96688a commit f40ccbc

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

toolchain/check/import_ref.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,14 @@ static auto MakeIncompleteClass(ImportContext& context,
16041604
return {class_decl.class_id, self_const_id};
16051605
}
16061606

1607+
static auto InitializeNameScopeAndImportRefs(
1608+
ImportContext& context, const SemIR::NameScope& import_scope,
1609+
SemIR::NameScope& new_scope, SemIR::InstId decl_id, SemIR::NameId name_id,
1610+
SemIR::NameScopeId parent_scope_id) {
1611+
new_scope.Set(decl_id, name_id, parent_scope_id);
1612+
AddNameScopeImportRefs(context, import_scope, new_scope);
1613+
}
1614+
16071615
// Fills out the class definition for an incomplete class.
16081616
static auto AddClassDefinition(ImportContext& context,
16091617
const SemIR::Class& import_class,
@@ -1621,9 +1629,9 @@ static auto AddClassDefinition(ImportContext& context,
16211629

16221630
// Push a block so that we can add scoped instructions to it.
16231631
context.local_context().inst_block_stack().Push();
1624-
new_scope.Set(new_class.first_owning_decl_id, SemIR::NameId::None,
1625-
new_class.parent_scope_id);
1626-
AddNameScopeImportRefs(context, import_scope, new_scope);
1632+
InitializeNameScopeAndImportRefs(
1633+
context, import_scope, new_scope, new_class.first_owning_decl_id,
1634+
SemIR::NameId::None, new_class.parent_scope_id);
16271635
new_class.body_block_id = context.local_context().inst_block_stack().Pop();
16281636

16291637
if (import_class.base_id.has_value()) {
@@ -2008,6 +2016,8 @@ static auto MakeImplDeclaration(ImportContext& context,
20082016
.constraint_id = SemIR::TypeInstId::None,
20092017
.interface = SemIR::SpecificInterface::None,
20102018
.witness_id = witness_id,
2019+
.scope_id = import_impl.is_complete() ? AddPlaceholderNameScope(context)
2020+
: SemIR::NameScopeId::None,
20112021
.is_final = import_impl.is_final}});
20122022

20132023
// Write the impl ID into the ImplDecl.
@@ -2024,9 +2034,9 @@ static auto AddImplDefinition(ImportContext& context,
20242034
new_impl.defined = true;
20252035

20262036
if (import_impl.scope_id.has_value()) {
2027-
new_impl.scope_id = context.local_name_scopes().Add(
2028-
new_impl.first_owning_decl_id, SemIR::NameId::None,
2029-
new_impl.parent_scope_id);
2037+
auto& new_scope = context.local_name_scopes().Get(new_impl.scope_id);
2038+
new_scope.Set(new_impl.first_owning_decl_id, SemIR::NameId::None,
2039+
new_impl.parent_scope_id);
20302040
// Import the contents of the definition scope, if we might need it. Name
20312041
// lookup is never performed into this scope by a user of the impl, so
20322042
// this is only necessary in the same library that defined the impl, in
@@ -2035,7 +2045,6 @@ static auto AddImplDefinition(ImportContext& context,
20352045
// TODO: Check to see if this impl is owned by the API file, rather than
20362046
// merely being imported into it.
20372047
if (context.import_ir_id() == SemIR::ImportIRId::ApiForImpl) {
2038-
auto& new_scope = context.local_name_scopes().Get(new_impl.scope_id);
20392048
const auto& import_scope =
20402049
context.import_name_scopes().Get(import_impl.scope_id);
20412050

@@ -2161,7 +2170,9 @@ static auto MakeInterfaceDecl(ImportContext& context,
21612170
interface_decl.interface_id = context.local_interfaces().Add(
21622171
{GetIncompleteLocalEntityBase(context, interface_decl_id,
21632172
import_interface),
2164-
{}});
2173+
{.scope_id = import_interface.is_complete()
2174+
? AddPlaceholderNameScope(context)
2175+
: SemIR::NameScopeId::None}});
21652176

21662177
if (import_interface.has_parameters()) {
21672178
interface_decl.type_id = GetGenericInterfaceType(
@@ -2181,17 +2192,16 @@ static auto AddInterfaceDefinition(ImportContext& context,
21812192
const SemIR::Interface& import_interface,
21822193
SemIR::Interface& new_interface,
21832194
SemIR::InstId self_param_id) -> void {
2184-
new_interface.scope_id = context.local_name_scopes().Add(
2185-
new_interface.first_owning_decl_id, SemIR::NameId::None,
2186-
new_interface.parent_scope_id);
21872195
auto& new_scope = context.local_name_scopes().Get(new_interface.scope_id);
2188-
new_scope.set_is_interface_definition();
21892196
const auto& import_scope =
21902197
context.import_name_scopes().Get(import_interface.scope_id);
21912198

21922199
// Push a block so that we can add scoped instructions to it.
21932200
context.local_context().inst_block_stack().Push();
2194-
AddNameScopeImportRefs(context, import_scope, new_scope);
2201+
InitializeNameScopeAndImportRefs(
2202+
context, import_scope, new_scope, new_interface.first_owning_decl_id,
2203+
SemIR::NameId::None, new_interface.parent_scope_id);
2204+
new_scope.set_is_interface_definition();
21952205
new_interface.associated_entities_id = AddAssociatedEntities(
21962206
context, new_interface.scope_id, import_interface.associated_entities_id);
21972207
new_interface.body_block_id =

0 commit comments

Comments
 (0)