Make identifying a facet type an operation on a (self+facet type) pair (#6592)

Identifying a facet type takes both a self and facet type as a pair, and
then encode the self into the IdentifiedFacetType. This makes a
constraint that requires some _other_ type implements an interface
visible in the IdentifiedFacetType. And it will help to enable facet
types with `where T impls Z` for `T` that is not `.Self` in the future.

IdentifiedFacetTypes are now stored in a CanonicalValueStore instead of
a RelationalValueStore as they key is the combination of self and
(declared) facet type together now.

When the self-type is a facet value (has type FacetType) this is most
straightforward. But when it's a type we need to construct a FacetValue
to construct a specific for a require decl, to replace the generic
binding of the symbolic `Self`, which has type FacetType. To do so, we
make a FacetValue with an empty FacetType (equivalent to TypeType). This
prevents any looking for witnesses through the FacetType, which matches
what you can get from a type directly, requiring witnesses to come from
finding an `impl` decl.

Add additional InstNamer logic for such empty facet types so they print
as `<typename>.type.facet` if possible instead of as just `facet_value`.
This commit is contained in:
Dana Jansens
2026-01-14 17:34:56 +00:00
committed by GitHub
parent 80639a02f0
commit 32aa7cb1fa
29 changed files with 617 additions and 267 deletions
+12 -4
View File
@@ -671,13 +671,19 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::Impl& impl) -> void {
const auto& interface = context.interfaces().Get(impl.interface.interface_id);
auto require_ids =
context.require_impls_blocks().Get(interface.require_impls_block_id);
if (require_ids.empty()) {
return;
}
// Make a facet value for the self type.
auto self_facet_value = GetConstantFacetValueForType(context, impl.self_id);
for (auto require_id : require_ids) {
const auto& require = context.require_impls().Get(require_id);
auto require_specific =
GetRequireImplsSpecificFromEnclosingSpecificWithSelfType(
context, require, impl.interface.specific_id, impl.self_id,
impl.witness_id);
GetRequireImplsSpecificFromEnclosingSpecificWithSelfFacetValue(
context, require, impl.interface.specific_id, self_facet_value);
auto self_const_id = GetConstantValueInRequireImplsSpecific(
context, require_specific, require.self_id);
auto facet_type_const_id = GetConstantValueInRequireImplsSpecific(
@@ -740,6 +746,7 @@ auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool {
}
auto CheckConstraintIsInterface(Context& context, SemIR::InstId impl_decl_id,
SemIR::InstId self_id,
SemIR::TypeInstId constraint_id)
-> SemIR::SpecificInterface {
auto facet_type_id = context.types().GetTypeIdForTypeInstId(constraint_id);
@@ -755,7 +762,8 @@ auto CheckConstraintIsInterface(Context& context, SemIR::InstId impl_decl_id,
}
auto identified_id = RequireIdentifiedFacetType(
context, SemIR::LocId(constraint_id), *facet_type, [&] {
context, SemIR::LocId(constraint_id),
context.constant_values().Get(self_id), *facet_type, [&] {
CARBON_DIAGNOSTIC(ImplOfUnidentifiedFacetType, Error,
"facet type {0} cannot be identified in `impl as`",
InstIdAsType);