Resolve cycles in .Self replacement in nested designators (#7183)

A nested designator like `.(X.X1).(Y.Y1)` results in nested
ImplWitnessAccess instructions, which can produce cycles in the
toolchain easily when replacing `.Self`.

First, when constructing a facet type like `V:! Z where .Z1 impls (Y
where .Y1 = U)` we substitute replace `.Self` in the nested facet type,
and in this case we replace `.Self` with `.Z1` which contains a `.Self`
of its own. This was coming from us being lazy about replacing `.Self`
in an `impl as` declaration, such as `impl C as Z where .Z1 = .Self`.
The self type is known there, so we can more eagerly replace `.Self` as
we do in a `require impls` declaration. Then the replacement for `.Self`
never comes with a `.Self` that needs to also be replaced. Any resulting
`.Self` would always be the top-level one.

Second, when evaluating ImplWitnessAccess, we were replacing .Self in
the LHS of rewrite constraints, but the `.Self` may itself have a type
that contains rewrite constraints. If one of those rewrite constraints
has nested ImplWitnessAccess instructions, we evaluate the new
ImplWitnessAccess, which again finds rewrite constraints to replace
`.Self` in, and we repeat forever. For this one we just stop replacing
.Self in the LHS of rewrite constraints. Since they are always against
.Self, we can always look in the access facet's type for a value.

While fixing ImplWitness access, also correct the lookup to search
through the types of nested ImplWitnessAccess instructions to find a
rewrite value, since it may find it at any level up to the eventual
`.Self`.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Dana Jansens
2026-05-13 16:11:20 +00:00
committed by GitHub
co-authored by Richard Smith
parent 798b177fc0
commit 6326bbdbe1
23 changed files with 820 additions and 534 deletions
+20 -13
View File
@@ -2642,32 +2642,37 @@ static auto AddRequirementImpls(Context& context, SemIR::LocId loc_id,
// references are to the same top-level value for `.Self` and can all be
// replaced together later.
SubstPeriodSelfCallbacks callbacks(
&context, loc_id, context.constant_values().Get(lhs_facet_or_type),
SubstPeriodSelfCallbacks::Behaviour::ImplicitOnly);
auto period_self_replacement_id =
context.constant_values().Get(lhs_facet_or_type);
auto self_impls_interface = [&](SemIR::SpecificInterface si) {
return SubstPeriodSelf(context, callbacks, si);
return SubstPeriodSelf(context, loc_id, si, period_self_replacement_id,
SubstPeriodSelfBehaviour::ImplicitOnly);
};
auto self_impls_constraint = [&](SemIR::SpecificNamedConstraint sc) {
return SubstPeriodSelf(context, callbacks, sc);
return SubstPeriodSelf(context, loc_id, sc, period_self_replacement_id,
SubstPeriodSelfBehaviour::ImplicitOnly);
};
auto type_impls_interface =
[&](SemIR::FacetTypeInfo::TypeImplsInterface impls)
-> SemIR::FacetTypeInfo::TypeImplsInterface {
auto self = SubstPeriodSelf(
context, callbacks, context.constant_values().Get(impls.self_type));
auto interface =
SubstPeriodSelf(context, callbacks, impls.specific_interface);
context, loc_id, context.constant_values().Get(impls.self_type),
period_self_replacement_id, SubstPeriodSelfBehaviour::ImplicitOnly);
auto interface = SubstPeriodSelf(
context, loc_id, impls.specific_interface, period_self_replacement_id,
SubstPeriodSelfBehaviour::ImplicitOnly);
return {context.constant_values().GetInstId(self), interface};
};
auto type_impls_constraint =
[&](SemIR::FacetTypeInfo::TypeImplsNamedConstraint impls)
-> SemIR::FacetTypeInfo::TypeImplsNamedConstraint {
auto self = SubstPeriodSelf(
context, callbacks, context.constant_values().Get(impls.self_type));
auto constraint =
SubstPeriodSelf(context, callbacks, impls.specific_named_constraint);
context, loc_id, context.constant_values().Get(impls.self_type),
period_self_replacement_id, SubstPeriodSelfBehaviour::ImplicitOnly);
auto constraint = SubstPeriodSelf(
context, loc_id, impls.specific_named_constraint,
period_self_replacement_id, SubstPeriodSelfBehaviour::ImplicitOnly);
return {context.constant_values().GetInstId(self), constraint};
};
@@ -2688,9 +2693,11 @@ static auto AddRequirementImpls(Context& context, SemIR::LocId loc_id,
[&](SemIR::FacetTypeInfo::RewriteConstraint rewrite)
-> SemIR::FacetTypeInfo::RewriteConstraint {
auto lhs_id = SubstPeriodSelf(
context, callbacks, context.constant_values().Get(rewrite.lhs_id));
context, loc_id, context.constant_values().Get(rewrite.lhs_id),
period_self_replacement_id, SubstPeriodSelfBehaviour::ImplicitOnly);
auto rhs_id = SubstPeriodSelf(
context, callbacks, context.constant_values().Get(rewrite.rhs_id));
context, loc_id, context.constant_values().Get(rewrite.rhs_id),
period_self_replacement_id, SubstPeriodSelfBehaviour::ImplicitOnly);
return {context.constant_values().GetInstId(lhs_id),
context.constant_values().GetInstId(rhs_id)};
};