From 71af207401f6aa9cc515a0a3be0e01cb4bb97442 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 13 Oct 2026 13:13:57 +0000 Subject: [PATCH] Disable global state that modifies eval while resolving a specific (#7923) We set the `declaring_impl_decls` state to modify the evaluation of LookupImplWitness instructions formed inside an `impl as` decl's constraint. However while checking that constraint, we may form other specifics through calls to generic classes, etc. Specifics are canonical and are keyed by their arguments and the generic ID. This means every time we see the same arguments for a specific, we must produce the same instructions. The `declaring_impl_decls` state can break that, causing instructions inside the specific to be formed in a way they wouldn't at another time. To fix this, remove the effect of `declaring_impl_decls` while resolving a specific. This keeps the specific's evaluation consistent so that the key truly represents the canonical specific for its arguments. This doesn't break anything. Existing LookupImplWitnesses (such as in a class decl) are not guaranteed to point to an `impl as` when they are modified by a specific. It is designators in the `impl as` constraint that are guaranteed as such. Designators in an `extend require impls` statement in a named constraint will be pointed to the `impl as` through another mechanism in a followup, which will make the `impl_as_named_constraint.carbon` test pass again. --- toolchain/check/eval.cpp | 11 +++++ .../impl/impl_as_named_constraint.carbon | 44 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index 0de1e80ebc94..df243a764233 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -3588,6 +3588,14 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, builder.Context(loc_id, ResolvingSpecificHere, specific_id); }); + // The `declaring_impl_decls` is a global state that modifies the evaluation + // of LookupImplWitness. As a global state, it should only affect new + // instructions introduced during a given state. When we are evaluating for a + // specific, we are resolving existing instructions in the eval block for a + // generic. The global state should not apply to anything done while resolving + // a specific. + auto declaring = std::exchange(context.declaring_impl_decls(), {}); + for (auto [i, inst_id, result_id] : llvm::enumerate(eval_block, value_block)) { auto const_id = TryEvalInstInContext(eval_context, inst_id, @@ -3599,6 +3607,9 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, } result_id = context.constant_values().GetInstId(const_id); } + + CARBON_CHECK(context.declaring_impl_decls().empty()); + context.declaring_impl_decls() = std::move(declaring); } // Information about the function call we are currently executing. Unlike diff --git a/toolchain/check/testdata/impl/impl_as_named_constraint.carbon b/toolchain/check/testdata/impl/impl_as_named_constraint.carbon index 287d29c9611e..9d6591bc6d5d 100644 --- a/toolchain/check/testdata/impl/impl_as_named_constraint.carbon +++ b/toolchain/check/testdata/impl/impl_as_named_constraint.carbon @@ -285,7 +285,7 @@ constraint Y { // CHECK:STDERR: impl () as Y {} -// --- member_access_in_extend_require.carbon +// --- fail_todo_member_access_in_extend_require.carbon library "[[@TEST_NAME]]"; interface I { let X: type; } @@ -304,8 +304,50 @@ constraint GivesI { // - Deduces for Self which tries to convert Self to type // - Requires the type of Self to be complete, and its type is GivesI // - Forms the self-specfic for GivesI's extend require....we have a loop. +// CHECK:STDERR: fail_todo_member_access_in_extend_require.carbon:[[@LINE+4]]:1: error: constraint `GivesI where .(I.X) = ()` being implemented requires that `T.(I.X)` implements `J` [IdentifiedRequireImplsNotImplemented] +// CHECK:STDERR: impl forall [T: type] T as GivesI where .X = () {} +// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// CHECK:STDERR: impl forall [T: type] T as GivesI where .X = () {} +// --- require_specific_used_in_impl_as_then_outside.carbon +library "[[@TEST_NAME]]"; + +interface I { let X: type; } +interface J {} +interface K {} + +class C {} +impl C as J {} +impl C as K {} + +class D {} +impl D as J {} + +constraint GivesI { + extend require impls I where .X impls J; +} + +match_first { + impl () as I where .X = C {} + // Never selected for `() as I`. Declaring this impl forms the specific of + // the require in `GivesI` with `Self = ()`, where `.X` refers to this + // impl. The require's specific must not permanently encode that decision + // into future uses though. + impl () as GivesI where .X = D {} +} + +fn F(generic _: GivesI where .X impls K) {} + +fn G() { + // Reuses the specific of the require in `GivesI` with `Self = ()`. The + // `.X` should perform a fresh impl lookup here and should resolve to `C`. If + // the specific encoded the attachment to the second impl instead, it may + // resolve to `D` (which doesn't impl `K`) or it may not resolve beyond the + // symbolic `.X` at all. + F(()); +} + // CHECK:STDOUT: --- fail_error_self_in_require.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants {