Give the facet type as a TypeId to IdentifyFacetType functions (#7926)

This statically ensures that identify always works with a constant
value, in the most restrictive way possible (ensuring it's a type not
just a constant).
This commit is contained in:
Dana Jansens authored and GitHub committed 2026-10-13 15:02:58 +00:00
1 parent f396762e22
commit b1de0e037d
7 files changed
+40 -35

No files matched your search

+8 -3
View File
@@ -204,13 +204,18 @@ static auto PopImplIntroducerAndParamsAsNameComponent(
.pattern_block_id = pattern_block_id};
}
struct BuildImplDeclResult {
SemIR::ImplId impl_id;
SemIR::InstId impl_decl_id;
SemIR::TypeInstId full_constraint_type_inst_id;
SemIR::IdentifiedFacetTypeId identified_id;
};
// Build an ImplDecl describing the signature of an impl. This handles the
// common logic shared by impl forward declarations and impl definitions. It
// also sets the `definition_id` on the Impl structure.
static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
bool has_definition)
-> std::tuple<SemIR::ImplId, SemIR::InstId, SemIR::TypeInstId,
SemIR::IdentifiedFacetTypeId> {
bool has_definition) -> BuildImplDeclResult {
auto [constraint_node, constraint_id] =
context.node_stack().PopExprWithNodeId();
auto [self_type_node, self_type_inst_id] =
+1 -1
View File
@@ -245,7 +245,7 @@ static auto ValidateRequire(Context& context, SemIR::LocId full_require_loc_id,
auto identified_facet_type_id = RequireIdentifiedFacetType(
context, constraint_loc_id, self_type_id.AsConstantId(),
context.types().GetTypeInstId(constraint_type_id), [&](auto& builder) {
constraint_type_id, [&](auto& builder) {
CARBON_DIAGNOSTIC(
RequireImplsUnidentifiedFacetType, Context,
"facet type {0} cannot be identified in `require` declaration",
+1 -2
View File
@@ -95,8 +95,7 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
// lookups into that interface with `.Self` so we identify it with `.Self`.
auto identified_id = TryToIdentifyFacetType(
context, node_id, context.constant_values().Get(period_self),
context.types().GetTypeInstId(period_self_type_id),
/*allow_partially_identified=*/false);
period_self_type_id, /*allow_partially_identified=*/false);
if (identified_id.has_value()) {
const auto& identified =
context.identified_facet_types().Get(identified_id);
+1 -1
View File
@@ -995,7 +995,7 @@ auto CheckConstraintIsInterface(Context& context, SemIR::LocId loc_id,
auto identified_id = RequireIdentifiedFacetType(
context, SemIR::LocId(full_constraint_id),
context.constant_values().Get(self_id),
context.constant_values().GetConstantTypeInstId(full_constraint_id),
context.types().GetTypeIdForTypeInstId(full_constraint_id),
[&](auto& builder) {
CARBON_DIAGNOSTIC(ImplOfUnidentifiedFacetType, Context,
"facet type {0} cannot be identified in `impl as`",
+14 -16
View File
@@ -228,15 +228,16 @@ static auto GetRequiredImplsFromConstraint(
SemIR::ConstantId query_self_const_id,
SemIR::ConstantId query_facet_type_const_id, bool diagnose)
-> std::optional<llvm::ArrayRef<SemIR::IdentifiedFacetType::RequiredImpl>> {
auto facet_type_inst_id =
context.types().GetTypeInstIdForTypeConstantId(query_facet_type_const_id);
auto identified_id = RequireIdentifiedFacetType(
context, loc_id, query_self_const_id, facet_type_inst_id,
context, loc_id, query_self_const_id,
context.types().GetTypeIdForTypeConstantId(query_facet_type_const_id),
[&](auto& builder) {
CARBON_DIAGNOSTIC(ImplLookupInUnidentifiedFacetType, Context,
"facet type {0} can not be identified", InstIdAsType);
"facet type {0} can not be identified",
SemIR::TypeId);
builder.Context(loc_id, ImplLookupInUnidentifiedFacetType,
facet_type_inst_id);
context.types().GetTypeIdForTypeConstantId(
query_facet_type_const_id));
},
diagnose);
if (!identified_id.has_value()) {
@@ -372,8 +373,7 @@ static auto CollectFacetWitnessSources(
context.impl_lookup_no_symbolic_final_lookups();
++no_symbolic_final_lookups;
auto identified_id = TryToIdentifyFacetType(
context, loc_id, facet_const_id,
context.types().GetTypeInstId(type_id), allow_partially_identified);
context, loc_id, facet_const_id, type_id, allow_partially_identified);
--no_symbolic_final_lookups;
if (identified_id.has_value()) {
@@ -452,7 +452,7 @@ static auto CollectFacetWitnessSources(
// always have a FacetType in `facet_type_const_id`.
auto identified_id = TryToIdentifyFacetType(
context, loc_id, canon_self_const_id,
context.types().GetTypeInstIdForTypeConstantId(facet_type_const_id),
context.types().GetTypeIdForTypeConstantId(facet_type_const_id),
/*allow_partially_identified=*/true);
if (identified_id.has_value()) {
witnesses.push_back({.facet_const_id = canon_self_const_id,
@@ -1353,14 +1353,12 @@ auto MakeWitnessesForPeriodSelfTypeWithoutLookup(Context& context,
context.constant_values().GetInst(period_self).type_id();
auto identified_period_self_type_id = RequireIdentifiedFacetType(
context, loc_id, facet_value,
context.types().GetTypeInstId(period_self_type_id),
[&](auto& /*builder*/) {
// The facet type of `.Self` may refer to generic interfaces that use
// `.Self` in their arguments. And when `.Self` is replaced by
// `facet_value`, we may fail with a monomorphization error. We pass it
// along without adding additional context.
});
context, loc_id, facet_value, period_self_type_id,
// The facet type of `.Self` may refer to generic interfaces that use
// `.Self` in their arguments. And when `.Self` is replaced by
// `facet_value`, we may fail with a monomorphization error. We pass it
// along without adding additional context.
[&](auto& /*builder*/) {});
if (!identified_period_self_type_id.has_value()) {
return SemIR::InstBlockIdOrError::MakeError();
}
+13 -10
View File
@@ -962,16 +962,17 @@ auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
// error is encountered or diagnosed.
static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId initial_self_const_id,
SemIR::TypeInstId facet_type_inst_id,
SemIR::ConstantId facet_type_const_id,
bool allow_partially_identified, bool diagnose)
-> SemIR::IdentifiedFacetTypeId {
if (facet_type_inst_id == SemIR::ErrorInst::InstId) {
if (facet_type_const_id == SemIR::ErrorInst::ConstantId) {
return SemIR::IdentifiedFacetTypeId::None;
}
auto declared_facet_type_id = context.insts()
.GetAs<SemIR::FacetType>(facet_type_inst_id)
.declared_facet_type_id;
auto declared_facet_type_id =
context.constant_values()
.GetInstAs<SemIR::FacetType>(facet_type_const_id)
.declared_facet_type_id;
// While partially identified facet types end up in the store of
// IdentifiedFacetTypes, we don't try to construct a key to look for them
@@ -992,7 +993,7 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
auto subst_id = SubstPeriodSelfInFacetType(
context, loc_id,
context.constant_values().GetInstId(initial_self_const_id),
context.types().GetAsTypeInstId(facet_type_inst_id));
context.types().GetTypeInstIdForTypeConstantId(facet_type_const_id));
declared_facet_type_id = context.insts()
.GetAs<SemIR::FacetType>(subst_id)
.declared_facet_type_id;
@@ -1251,22 +1252,24 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId self_const_id,
SemIR::TypeInstId facet_type_inst_id,
SemIR::TypeId facet_type_type_id,
bool allow_partially_identified)
-> SemIR::IdentifiedFacetTypeId {
return IdentifyFacetType(context, loc_id, self_const_id, facet_type_inst_id,
return IdentifyFacetType(context, loc_id, self_const_id,
facet_type_type_id.AsConstantId(),
allow_partially_identified,
/*diagnose=*/false);
}
auto RequireIdentifiedFacetType(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId self_const_id,
SemIR::TypeInstId facet_type_inst_id,
SemIR::TypeId facet_type_type_id,
DiagnosticContextFn diagnostic_context,
bool diagnose) -> SemIR::IdentifiedFacetTypeId {
CARBON_CHECK(diagnostic_context);
Diagnostics::ContextScope scope(&context.emitter(), diagnostic_context);
return IdentifyFacetType(context, loc_id, self_const_id, facet_type_inst_id,
return IdentifyFacetType(context, loc_id, self_const_id,
facet_type_type_id.AsConstantId(),
/*allow_partially_identified=*/false, diagnose);
}
+2 -2
View File
@@ -77,7 +77,7 @@ auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
// the middle of being defined.
auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId self_const_id,
SemIR::TypeInstId facet_type_inst_id,
SemIR::TypeId facet_type_type_id,
bool allow_partially_identified)
-> SemIR::IdentifiedFacetTypeId;
@@ -96,7 +96,7 @@ auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id,
// TODO: Remove `diagnose` and split into `TryIdentifyFacetType`.
auto RequireIdentifiedFacetType(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId self_const_id,
SemIR::TypeInstId facet_type_inst_id,
SemIR::TypeId facet_type_type_id,
DiagnosticContextFn diagnostic_context,
bool diagnose = true)
-> SemIR::IdentifiedFacetTypeId;