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 {