diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index c9f48865aed2..b9ff0e404fe3 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -792,32 +792,28 @@ static auto GetConstantDeclaredFacetType(EvalContext& eval_context, &self_phase)}}); } - // Rewrite constraints are resolved first before replacing them with their - // canonical instruction, so that in a `WhereExpr` we can work with the - // `ImplWitnessAccess` references to `.Self` on the LHS of the constraints - // rather than the value of the associated constant they reference. - // - // This also implies that we may find `ImplWitnessAccessSubstituted` - // instructions in the LHS and RHS of these constraints, which are preserved - // to maintain them as an unresolved reference to an associated constant, but - // which must be handled gracefully during resolution. They will be replaced - // with the constant value of the `ImplWitnessAccess` below when they are - // substituted with a constant value. - declared_facet_type.rewrite_constraints = orig.rewrite_constraints; + // The rewrite constraints may not be fully resolved yet, so there may be + // `ImplWitnessAccessSubstituted` insts on the LHS that we need to look + // through in order to constant-evaluate correctly. + declared_facet_type.rewrite_constraints.reserve( + orig.rewrite_constraints.size()); + for (const auto& rewrite : orig.rewrite_constraints) { + declared_facet_type.rewrite_constraints.push_back( + {.lhs_id = RequireConstantValue( + eval_context, + GetImplWitnessAccessWithoutSubstitution( + eval_context.context().sem_ir(), rewrite.lhs_id), + &self_phase), + .rhs_id = + RequireConstantValue(eval_context, rewrite.rhs_id, &self_phase)}); + } + if (!ResolveFacetTypeRewriteConstraints( eval_context.context(), loc_id, declared_facet_type.rewrite_constraints)) { *phase = Phase::UnknownDueToError; } - for (auto& rewrite : declared_facet_type.rewrite_constraints) { - auto lhs_id = - RequireConstantValue(eval_context, rewrite.lhs_id, &self_phase); - auto rhs_id = - RequireConstantValue(eval_context, rewrite.rhs_id, &self_phase); - rewrite = {.lhs_id = lhs_id, .rhs_id = rhs_id}; - } - // Update phase, ignoring `.Self` dependence from constraints whose `.Self` is // scoped to this facet type. if (self_phase > Phase::PeriodSelfSymbolic) { diff --git a/toolchain/check/facet_type.cpp b/toolchain/check/facet_type.cpp index f1b6fb86fb0b..6e76f9ae730d 100644 --- a/toolchain/check/facet_type.cpp +++ b/toolchain/check/facet_type.cpp @@ -42,10 +42,10 @@ auto FacetTypeFromNamedConstraint(Context& context, .declared_facet_type_id = declared_facet_type_id}; } -auto GetImplWitnessAccessWithoutSubstitution(Context& context, +auto GetImplWitnessAccessWithoutSubstitution(const SemIR::File& sem_ir, SemIR::InstId inst_id) -> SemIR::InstId { - if (auto inst = context.insts().TryGetAs( + if (auto inst = sem_ir.insts().TryGetAs( inst_id)) { return inst->impl_witness_access_id; } @@ -340,7 +340,7 @@ static auto IsPeriodSelfAccess(Context& context, SemIR::InstId inst_id) static auto TryGetRewriteAccess(Context& context, SemIR::InstId inst_id) -> std::optional> { auto access_without_subst_id = - GetImplWitnessAccessWithoutSubstitution(context, inst_id); + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), inst_id); // When a facet type is first constructed, the LHS of every rewrite is an // access into `.Self`. But because we resolve rewrites on every evaluation, @@ -425,7 +425,7 @@ auto ResolveFacetTypeRewriteConstraints( // location along with them in the rewrite constraints. context.emitter().Emit(loc_id, AssociatedConstantWithDifferentValues, GetImplWitnessAccessWithoutSubstitution( - context, constraint.lhs_id), + context.sem_ir(), constraint.lhs_id), source_order1, source_order2); } return false; diff --git a/toolchain/check/facet_type.h b/toolchain/check/facet_type.h index 65037e3cc542..4723944aab86 100644 --- a/toolchain/check/facet_type.h +++ b/toolchain/check/facet_type.h @@ -29,7 +29,7 @@ auto FacetTypeFromNamedConstraint(Context& context, // This must be used when accessing the LHS of a rewrite constraint which has // not yet been resolved in order to preserve which associated constant is being // rewritten. -auto GetImplWitnessAccessWithoutSubstitution(Context& context, +auto GetImplWitnessAccessWithoutSubstitution(const SemIR::File& sem_ir, SemIR::InstId inst_id) -> SemIR::InstId; diff --git a/toolchain/check/handle_where.cpp b/toolchain/check/handle_where.cpp index 785d420cf561..0b776d34a713 100644 --- a/toolchain/check/handle_where.cpp +++ b/toolchain/check/handle_where.cpp @@ -143,7 +143,8 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool { for (const auto& rewrite : base_declared_facet_type.rewrite_constraints) { if (rewrite.lhs_id != SemIR::ErrorInst::InstId) { auto const_id = context.constant_values().Get( - GetImplWitnessAccessWithoutSubstitution(context, rewrite.lhs_id)); + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), + rewrite.lhs_id)); auto frozen_const_id = FreezePeriodSelf(context, const_id); context.where_stack().back().rewrites.Insert(frozen_const_id, rewrite.rhs_id); @@ -320,7 +321,7 @@ auto HandleParseNode(Context& context, Parse::RequirementEqualId node_id) // frozen. context.where_stack().back().rewrites.Insert( context.constant_values().Get( - GetImplWitnessAccessWithoutSubstitution(context, lhs_id)), + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), lhs_id)), rhs_id); } return true; @@ -360,7 +361,7 @@ static auto IsPeriodSelfAccess(Context& context, SemIR::InstId inst_id) } // Recurse through ImplWitnessAccess into the self type being accessed. auto access = context.insts().TryGetAs( - GetImplWitnessAccessWithoutSubstitution(context, inst_id)); + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), inst_id)); if (!access) { return false; } diff --git a/toolchain/check/testdata/facet/facet_assoc_const.carbon b/toolchain/check/testdata/facet/facet_assoc_const.carbon index 382bf43db86e..bbf00d54223f 100644 --- a/toolchain/check/testdata/facet/facet_assoc_const.carbon +++ b/toolchain/check/testdata/facet/facet_assoc_const.carbon @@ -227,8 +227,8 @@ interface M { let X: type; let Y: type; } // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━━ // CHECK:STDERR: · ┗━━┫ error: associated constant // CHECK:STDERR: · ┃ `.(M.Y)` given two -// CHECK:STDERR: · ┃ different values -// CHECK:STDERR: · ┃ `T.(M.X)` and `.(M.X)` +// CHECK:STDERR: · ┃ different values `()` +// CHECK:STDERR: · ┃ and `.(M.X)` // CHECK:STDERR: · ┃ [AssociatedConstantWithDifferentValues] // CHECK:STDERR: ╰──── // CHECK:STDERR: diff --git a/toolchain/check/testdata/facet/nested_facet_types.carbon b/toolchain/check/testdata/facet/nested_facet_types.carbon index aafdc6b2e5ef..8cb9e497451e 100644 --- a/toolchain/check/testdata/facet/nested_facet_types.carbon +++ b/toolchain/check/testdata/facet/nested_facet_types.carbon @@ -151,7 +151,7 @@ class C(T: type) {} // CHECK:STDERR: -> │ fn F(unused generic FF: (Z where .T = C(.U)) where .T = {}) {} // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ // CHECK:STDERR: · ┗━━┫ error: associated constant `.(Z.T)` given two -// CHECK:STDERR: · ┃ different values `C(.(Z.U))` and `{}` +// CHECK:STDERR: · ┃ different values `{}` and `C(.(Z.U))` // CHECK:STDERR: · ┃ [AssociatedConstantWithDifferentValues] // CHECK:STDERR: ╰──── // CHECK:STDERR: diff --git a/toolchain/check/testdata/impl/impl_assoc_const.carbon b/toolchain/check/testdata/impl/impl_assoc_const.carbon index ae11a50d5712..83f774912d34 100644 --- a/toolchain/check/testdata/impl/impl_assoc_const.carbon +++ b/toolchain/check/testdata/impl/impl_assoc_const.carbon @@ -141,7 +141,7 @@ interface L { let W: type; } // CHECK:STDERR: -> │ impl () as L where .W = {} and .W = () {} // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━ // CHECK:STDERR: · ┗━━┫ error: associated constant `.(L.W)` given two different values -// CHECK:STDERR: · ┃ `{}` and `()` +// CHECK:STDERR: · ┃ `()` and `{}` // CHECK:STDERR: · ┃ [AssociatedConstantWithDifferentValues] // CHECK:STDERR: ╰──── // CHECK:STDERR: @@ -276,7 +276,7 @@ interface M { let X: type; } // CHECK:STDERR: -> │ impl () as M where .X = {} and .X = () and .X = {} {} // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ // CHECK:STDERR: · ┗━━┫ error: associated constant `.(M.X)` given two different values -// CHECK:STDERR: · ┃ `{}` and `()` +// CHECK:STDERR: · ┃ `()` and `{}` // CHECK:STDERR: · ┃ [AssociatedConstantWithDifferentValues] // CHECK:STDERR: ╰──── // CHECK:STDERR: diff --git a/toolchain/check/type_completion.cpp b/toolchain/check/type_completion.cpp index 15535ab04757..4d5e5d8070a5 100644 --- a/toolchain/check/type_completion.cpp +++ b/toolchain/check/type_completion.cpp @@ -1061,7 +1061,7 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id, auto rewrite_as_constants = [&](auto rewrite) -> IdentifiedFacetType::Rewrite { return {context.constant_values().Get( - GetImplWitnessAccessWithoutSubstitution(context, + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), rewrite.lhs_id)), context.constant_values().Get(rewrite.rhs_id)}; }; @@ -1072,7 +1072,7 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id, auto rewrite_as_constants = [&](auto rewrite) -> IdentifiedFacetType::Equivalent { return {context.constant_values().Get( - GetImplWitnessAccessWithoutSubstitution(context, + GetImplWitnessAccessWithoutSubstitution(context.sem_ir(), rewrite.lhs_id)), context.constant_values().Get(rewrite.rhs_id)}; };