Verify non-extend require constraints are satisfied at impl as definition (#7937)

Rewrites in `extend requires` are used to set the witness table values
of the impl. Rewrites in non-extend `requires` are treated as same-type
constraints and must be true at the point of the impl definition.
This commit is contained in:
Dana Jansens authored and GitHub committed 2026-10-14 16:44:37 +00:00
1 parent 24766e213e
commit f0bae96373
10 files changed
+214 -19

No files matched your search

+29 -11
View File
@@ -883,12 +883,6 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
return;
}
// TODO: Check other kinds of constraints too: rewrites into targets other
// than `Self.(TargetInterface.__)` and same-type constraints. Consider maybe
// building a facet type that just excludes anything about the impl-as target
// interface, and then just perform lookup of Self as that facet type, so we
// don't have to re-implement all of the validation of impl lookup?
// The IdentifiedFacetType canonicalizes the self facets, so we do the same
// for comparing with it.
auto self_const_id =
@@ -919,13 +913,11 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
"implements `{2}`",
SemIR::DeclaredFacetTypeId, InstIdAsConstant,
SemIR::SpecificInterface);
auto facet_type = context.insts().GetAs<SemIR::FacetType>(
context.constant_values().GetConstantInstId(full_constraint_id));
context.emitter().Emit(
loc_id, IdentifiedRequireImplsNotImplemented,
context.insts()
.GetAs<SemIR::FacetType>(
context.constant_values().GetConstantInstId(
full_constraint_id))
.declared_facet_type_id,
facet_type.declared_facet_type_id,
context.constant_values().GetInstId(req.self_facet_value),
req.specific_interface);
}
@@ -935,6 +927,32 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
}
}
for (auto equiv : identified.equivalents()) {
// This replaces both IdentifiedWitness and ImplSelfWitness referring to
// this impl with the impl's ImplWitness. That causes any access through the
// witness to resolve to the value from the impl's witness table.
SubstWithImplWitness callbacks(&context, &impl);
auto subst_equiv =
SubstIdentifiedWitnesses(context, loc_id, equiv, callbacks);
if (subst_equiv.lhs != subst_equiv.rhs) {
CARBON_DIAGNOSTIC(IdentifiedRequireImplsNotEquivalent, Error,
"constraint {0} being implemented requires that {1} "
"equals {2}; found {3} and {4}",
SemIR::DeclaredFacetTypeId, InstIdAsConstant,
InstIdAsConstant, InstIdAsConstant, InstIdAsConstant);
auto facet_type = context.insts().GetAs<SemIR::FacetType>(
context.constant_values().GetConstantInstId(full_constraint_id));
context.emitter().Emit(
loc_id, IdentifiedRequireImplsNotEquivalent,
facet_type.declared_facet_type_id,
context.constant_values().GetInstId(equiv.lhs),
context.constant_values().GetInstId(equiv.rhs),
context.constant_values().GetInstId(subst_equiv.lhs),
context.constant_values().GetInstId(subst_equiv.rhs));
}
}
const auto& interface = context.interfaces().Get(impl.interface.interface_id);
auto require_ids =