Fix handling of interface redefinitions. (#4198)

The prior code crashed when trying to find the function's `Self`
parameter, I believe. `fail_redefine_with_dependents.carbon` handles
this case. It wasn't caught by the prior case because the `F` didn't
have any dependent parameters.

Note this also ran into a formatter crash, with invalid constants. I'm
fixing that here, but will also note it on #4145 (the crash in
FinishGenericDecl was muddled by a crash in Formatter code).
This commit is contained in:
Jon Ross-Perkins
2024-08-07 21:30:44 +00:00
committed by GitHub
parent 0d106fbf90
commit 3d13b8f71c
4 changed files with 197 additions and 72 deletions
+39 -37
View File
@@ -29,7 +29,8 @@ auto HandleParseNode(Context& context, Parse::InterfaceIntroducerId node_id)
}
static auto BuildInterfaceDecl(Context& context,
Parse::AnyInterfaceDeclId node_id)
Parse::AnyInterfaceDeclId node_id,
bool is_definition)
-> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
auto name = PopNameComponent(context);
auto name_context = context.decl_name_stack().FinishName(name);
@@ -58,21 +59,37 @@ static auto BuildInterfaceDecl(Context& context,
if (existing_id.is_valid()) {
if (auto existing_interface_decl =
context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
// TODO: Implement full redeclaration checking. See `MergeClassDecl`. For
// now we just check the generic parameters match.
auto existing_interface =
context.interfaces().Get(existing_interface_decl->interface_id);
if (CheckRedeclParamsMatch(
context,
DeclParams(interface_decl_id, name.first_param_node_id,
name.last_param_node_id, name.implicit_params_id,
name.params_id),
DeclParams(context.interfaces().Get(
existing_interface_decl->interface_id)))) {
// This is a redeclaration of an existing interface.
interface_decl.interface_id = existing_interface_decl->interface_id;
interface_decl.type_id = existing_interface_decl->type_id;
// TODO: If the new declaration is a definition, keep its parameter
// and implicit parameter lists rather than the ones from the
// previous declaration.
DeclParams(existing_interface))) {
// TODO: This should be refactored a little, particularly for
// prev_import_ir_id. See similar logic for classes and functions, which
// might also be refactored to merge.
CheckIsAllowedRedecl(context, Lex::TokenKind::Interface,
existing_interface.name_id,
{.loc = node_id,
.is_definition = is_definition,
.is_extern = false},
{.loc = existing_interface.decl_id,
.is_definition = existing_interface.is_defined(),
.is_extern = false},
/*prev_import_ir_id=*/SemIR::ImportIRId::Invalid);
// Can't merge interface definitions due to the generic requirements.
// TODO: Should this also be mirrored to classes/functions for generics?
if (!is_definition || !existing_interface.is_defined()) {
// This is a redeclaration of an existing interface.
interface_decl.interface_id = existing_interface_decl->interface_id;
interface_decl.type_id = existing_interface_decl->type_id;
// TODO: If the new declaration is a definition, keep its parameter
// and implicit parameter lists rather than the ones from the
// previous declaration.
}
}
} else {
// This is a redeclaration of something other than a interface.
@@ -82,20 +99,13 @@ static auto BuildInterfaceDecl(Context& context,
// Create a new interface if this isn't a valid redeclaration.
if (!interface_decl.interface_id.is_valid()) {
auto generic_id = FinishGenericDecl(context, interface_decl_id);
// TODO: If this is an invalid redeclaration of a non-interface entity or
// there was an error in the qualifier, we will have lost track of the
// interface name here. We should keep track of it even if the name is
// invalid.
SemIR::Interface interface_info = {
{.name_id = name_context.name_id_for_new_inst(),
.parent_scope_id = name_context.parent_scope_id_for_new_inst(),
.generic_id = generic_id,
.first_param_node_id = name.first_param_node_id,
.last_param_node_id = name.last_param_node_id,
.implicit_param_refs_id = name.implicit_params_id,
.param_refs_id = name.params_id,
.decl_id = interface_decl_id}};
name_context.MakeEntityWithParamsBase(interface_decl_id, name)};
interface_info.generic_id = FinishGenericDecl(context, interface_decl_id);
interface_decl.interface_id = context.interfaces().Add(interface_info);
if (interface_info.has_parameters()) {
interface_decl.type_id =
@@ -114,32 +124,24 @@ static auto BuildInterfaceDecl(Context& context,
}
auto HandleParseNode(Context& context, Parse::InterfaceDeclId node_id) -> bool {
BuildInterfaceDecl(context, node_id);
BuildInterfaceDecl(context, node_id, /*is_definition=*/false);
context.decl_name_stack().PopScope();
return true;
}
auto HandleParseNode(Context& context,
Parse::InterfaceDefinitionStartId node_id) -> bool {
auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
auto [interface_id, interface_decl_id] =
BuildInterfaceDecl(context, node_id, /*is_definition=*/true);
auto& interface_info = context.interfaces().Get(interface_id);
// Track that this declaration is the definition.
if (interface_info.is_defined()) {
CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
"Redefinition of interface {0}.", SemIR::NameId);
CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
"Previous definition was here.");
context.emitter()
.Build(node_id, InterfaceRedefinition, interface_info.name_id)
.Note(interface_info.definition_id, InterfacePreviousDefinition)
.Emit();
} else {
interface_info.definition_id = interface_decl_id;
interface_info.scope_id =
context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
interface_info.parent_scope_id);
}
CARBON_CHECK(!interface_info.is_defined())
<< "Can't merge with defined interfaces.";
interface_info.definition_id = interface_decl_id;
interface_info.scope_id =
context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
interface_info.parent_scope_id);
auto self_specific_id =
context.generics().GetSelfSpecific(interface_info.generic_id);