Treat associated constants as entities parameterized by Self (#4837)

Add a full entity representation for associated constants, and build a
`Generic` object for them. This `Generic` is parameterized by the
enclosing `Self` type, allowing the use of `Self` within the type of the
associated constant to be supported.

When performing impl lookup for an associated constant, produce the type
with the provided self type substituted for its `Self` along with any
generic parameters of the interface.

Split the handling of associated constant declarations into two parts,
corresponding to the code before the `=`, and the code between the `=`
and `;` (if any). The former goes into the generic declaration region;
the latter into the generic definition region. This prepares us to
handle the default value for an associated constant, but for now we're
just storing the information and not actually using it.

Remove the entity type field from `assoc_entity_type`, because it's
almost unused and is an attractive nuisance -- it must necessarily be a
type in the generic scope of the associated constant rather than in the
scope of the instruction (because there is no `Self` anywhere else),
which means that it's hard to substitute into or derive meaning from.

See `toolchain/check/testdata/impl/assoc_const_self.carbon` for tests of
the new functionality; these used to cause the toolchain to crash.
This commit is contained in:
Richard Smith
2025-01-25 02:13:52 +00:00
committed by GitHub
parent b06fcc97f6
commit 5f888e1124
108 changed files with 3727 additions and 1681 deletions
+13 -13
View File
@@ -159,23 +159,23 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
return context.emitter().Build(type_node, IncompleteTypeInAssociatedDecl,
cast_type_id);
});
context.entity_names().Add(
SemIR::AssociatedConstantDecl assoc_const_decl = {
.type_id = cast_type_id,
.assoc_const_id = SemIR::AssociatedConstantId::None,
.decl_block_id = SemIR::InstBlockId::None};
auto decl_id = context.AddPlaceholderInstInNoBlock(SemIR::LocIdAndInst(
context.parse_tree().As<Parse::CompileTimeBindingPatternId>(node_id),
assoc_const_decl));
assoc_const_decl.assoc_const_id = context.associated_constants().Add(
{.name_id = name_id,
.parent_scope_id = context.scope_stack().PeekNameScopeId(),
.bind_index = SemIR::CompileTimeBindIndex::None});
.decl_id = decl_id,
.generic_id = SemIR::GenericId::None,
.default_value_id = SemIR::InstId::None});
context.ReplaceInstBeforeConstantUse(decl_id, assoc_const_decl);
SemIR::InstId decl_id = context.AddInst<SemIR::AssociatedConstantDecl>(
context.parse_tree().As<Parse::CompileTimeBindingPatternId>(node_id),
{cast_type_id, name_id});
context.node_stack().Push(node_id, decl_id);
// Add an associated entity name to the interface scope.
auto assoc_id = BuildAssociatedEntity(
context, parent_interface_decl->interface_id, decl_id);
auto name_context =
context.decl_name_stack().MakeUnqualifiedName(node_id, name_id);
context.decl_name_stack().AddNameOrDiagnose(
name_context, assoc_id, introducer.modifier_set.GetAccessKind());
return true;
}