Initial support for empty named constraints (#6245)

Type check named constraint decls and definitions. We don't correctly
error if you put a `fn` inside them. There is no support for `require`
or `alias` yet, so there's nothing useful you can do with them yet.

We have attempted to share code between `interface` and `constraint` as
they are quite similar. First by splitting out some of
handle_interface.cpp to a separate file. Second by sharing some code
paths when you want a facet type from either one, as they both turn into
a facet type.
This commit is contained in:
Dana Jansens
2025-10-22 18:26:32 +00:00
committed by GitHub
parent a340808062
commit 22580a47d3
41 changed files with 1771 additions and 142 deletions
+38 -88
View File
@@ -10,11 +10,15 @@
#include "toolchain/check/generic.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/interface.h"
#include "toolchain/check/merge.h"
#include "toolchain/check/modifiers.h"
#include "toolchain/check/name_component.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/check/type.h"
#include "toolchain/sem_ir/entity_with_params_base.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/interface.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
@@ -26,11 +30,12 @@ auto HandleParseNode(Context& context, Parse::InterfaceIntroducerId node_id)
// Create an instruction block to hold the instructions created as part of the
// interface signature, such as generic parameters.
context.inst_block_stack().Push();
// Push the bracketing node.
context.node_stack().Push(node_id);
// Optional modifiers and the name follow.
context.decl_introducer_state_stack().Push<Lex::TokenKind::Interface>();
context.decl_name_stack().PushScopeAndStartName();
// Push the bracketing node.
context.node_stack().Push(node_id);
return true;
}
@@ -56,87 +61,44 @@ static auto BuildInterfaceDecl(Context& context,
// Add the interface declaration.
auto interface_decl = SemIR::InterfaceDecl{
SemIR::TypeType::TypeId, SemIR::InterfaceId::None, decl_block_id};
auto interface_decl_id = AddPlaceholderInst(context, node_id, interface_decl);
auto decl_inst_id = AddPlaceholderInst(context, node_id, interface_decl);
SemIR::Interface interface_info = {name_context.MakeEntityWithParamsBase(
name, interface_decl_id, /*is_extern=*/false,
SemIR::LibraryNameId::None)};
name, decl_inst_id, /*is_extern=*/false, SemIR::LibraryNameId::None)};
DiagnoseIfGenericMissingExplicitParameters(context, interface_info);
// Check whether this is a redeclaration.
SemIR::ScopeLookupResult lookup_result =
context.decl_name_stack().LookupOrAddName(
name_context, interface_decl_id,
introducer.modifier_set.GetAccessKind());
if (lookup_result.is_poisoned()) {
// This is a declaration of a poisoned name.
DiagnosePoisonedName(context, name_context.name_id_for_new_inst(),
lookup_result.poisoning_loc_id(), name_context.loc_id);
} else if (lookup_result.is_found()) {
SemIR::InstId existing_id = lookup_result.target_inst_id();
if (auto existing_interface_decl =
context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
auto existing_interface =
context.interfaces().Get(existing_interface_decl->interface_id);
if (CheckRedeclParamsMatch(
context,
DeclParams(SemIR::LocId(interface_decl_id),
name.first_param_node_id, name.last_param_node_id,
name.implicit_param_patterns_id,
name.param_patterns_id),
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.
DiagnoseIfInvalidRedecl(
context, Lex::TokenKind::Interface, existing_interface.name_id,
RedeclInfo(interface_info, node_id, is_definition),
RedeclInfo(existing_interface,
SemIR::LocId(existing_interface.latest_decl_id()),
existing_interface.has_definition_started()),
/*prev_import_ir_id=*/SemIR::ImportIRId::None);
name_context, decl_inst_id, introducer.modifier_set.GetAccessKind());
if (auto existing_decl = TryGetExistingDecl(context, name, lookup_result,
interface_info, is_definition)) {
auto existing_interface_decl = existing_decl->As<SemIR::InterfaceDecl>();
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.
// Can't merge interface definitions due to the generic requirements.
if (!is_definition || !existing_interface.has_definition_started()) {
// 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.
DiagnoseDuplicateName(context, name_context.name_id, name_context.loc_id,
SemIR::LocId(existing_id));
}
}
// Create a new interface if this isn't a valid redeclaration.
if (!interface_decl.interface_id.has_value()) {
// 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.
interface_info.generic_id = BuildGenericDecl(context, interface_decl_id);
auto prev_decl_generic_id =
context.interfaces().Get(interface_decl.interface_id).generic_id;
FinishGenericRedecl(context, prev_decl_generic_id);
} else {
// Create a new interface if this isn't a valid redeclaration.
interface_info.generic_id = BuildGenericDecl(context, decl_inst_id);
interface_decl.interface_id = context.interfaces().Add(interface_info);
if (interface_info.has_parameters()) {
interface_decl.type_id =
GetGenericInterfaceType(context, interface_decl.interface_id,
context.scope_stack().PeekSpecificId());
}
} else {
auto prev_decl_generic_id =
context.interfaces().Get(interface_decl.interface_id).generic_id;
FinishGenericRedecl(context, prev_decl_generic_id);
}
// Write the interface ID into the InterfaceDecl.
ReplaceInstBeforeConstantUse(context, interface_decl_id, interface_decl);
ReplaceInstBeforeConstantUse(context, decl_inst_id, interface_decl);
return {interface_decl.interface_id, interface_decl_id};
return {interface_decl.interface_id, decl_inst_id};
}
auto HandleParseNode(Context& context, Parse::InterfaceDeclId node_id) -> bool {
@@ -147,16 +109,16 @@ auto HandleParseNode(Context& context, Parse::InterfaceDeclId node_id) -> bool {
auto HandleParseNode(Context& context,
Parse::InterfaceDefinitionStartId node_id) -> bool {
auto [interface_id, interface_decl_id] =
auto [interface_id, decl_inst_id] =
BuildInterfaceDecl(context, node_id, /*is_definition=*/true);
auto& interface_info = context.interfaces().Get(interface_id);
// Track that this declaration is the definition.
CARBON_CHECK(!interface_info.has_definition_started(),
"Can't merge with defined interfaces.");
interface_info.definition_id = interface_decl_id;
interface_info.definition_id = decl_inst_id;
interface_info.scope_id = context.name_scopes().Add(
interface_decl_id, SemIR::NameId::None, interface_info.parent_scope_id);
decl_inst_id, SemIR::NameId::None, interface_info.parent_scope_id);
context.name_scopes()
.Get(interface_info.scope_id)
.set_is_interface_definition();
@@ -167,35 +129,21 @@ auto HandleParseNode(Context& context,
StartGenericDefinition(context, interface_info.generic_id);
context.inst_block_stack().Push();
context.node_stack().Push(node_id, interface_id);
// We use the arg stack to build the witness table type.
context.args_type_info_stack().Push();
// Declare and introduce `Self`.
// Declare and introduce `Self`. We model `Self` as a symbolic binding whose
// type is the interface, excluding any other interfaces mentioned by
// `require` declarations.
SemIR::TypeId self_type_id =
GetInterfaceType(context, interface_id, self_specific_id);
// We model `Self` as a symbolic binding whose type is the interface.
// Because there is no equivalent non-symbolic value, we use `None` as
// the `value_id` on the `BindSymbolicName`.
auto entity_name_id = context.entity_names().AddSymbolicBindingName(
SemIR::NameId::SelfType, interface_info.scope_id,
context.scope_stack().AddCompileTimeBinding(),
/*is_template=*/false);
interface_info.self_param_id =
AddInst(context, SemIR::LocIdAndInst::NoLoc<SemIR::BindSymbolicName>(
{.type_id = self_type_id,
.entity_name_id = entity_name_id,
.value_id = SemIR::InstId::None}));
context.scope_stack().PushCompileTimeBinding(interface_info.self_param_id);
context.name_scopes().AddRequiredName(interface_info.scope_id,
SemIR::NameId::SelfType,
interface_info.self_param_id);
interface_info.self_param_id = AddSelfGenericParameter(
context, self_type_id, interface_info.scope_id, /*is_template=*/false);
// Enter the interface scope.
context.scope_stack().PushForEntity(
interface_decl_id, interface_info.scope_id, self_specific_id);
context.scope_stack().PushForEntity(decl_inst_id, interface_info.scope_id,
self_specific_id);
// TODO: Handle the case where there's control flow in the interface body. For
// example:
@@ -207,6 +155,8 @@ auto HandleParseNode(Context& context,
// We may need to track a list of instruction blocks here, as we do for a
// function.
interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
context.node_stack().Push(node_id, interface_id);
return true;
}