diff --git a/toolchain/check/interface.cpp b/toolchain/check/interface.cpp index 14f8739beff8..c6efe442e231 100644 --- a/toolchain/check/interface.cpp +++ b/toolchain/check/interface.cpp @@ -18,6 +18,7 @@ #include "toolchain/check/inst.h" #include "toolchain/check/merge.h" #include "toolchain/check/name_lookup.h" +#include "toolchain/check/subst.h" #include "toolchain/check/type.h" #include "toolchain/sem_ir/entity_with_params_base.h" #include "toolchain/sem_ir/ids.h" @@ -79,16 +80,34 @@ auto GetSelfSpecificForInterfaceMemberWithSelfType( // numbering at the point where the self specific is used. int index_delta = num_enclosing_specific_args - arg_ids.size(); + // Trailing arguments may mention outer parameters of `generic` in their + // types (for example `U: A(T)` where `T` comes from the enclosing interface). + // Substitute those outer arguments before adjusting bind indices, so later + // thunk cloning does not evaluate a binding against the wrong generic. + llvm::SmallVector substitutions; + substitutions.reserve(arg_ids.size()); + auto generic_bindings = context.inst_blocks().Get(generic.bindings_id); + CARBON_CHECK(arg_ids.size() <= generic_bindings.size()); + for (auto [i, outer_arg_id] : llvm::enumerate(arg_ids)) { + auto binding = context.insts().GetAs( + context.constant_values().GetConstantInstId(generic_bindings[i])); + auto entity_name = context.entity_names().Get(binding.entity_name_id); + CARBON_CHECK(entity_name.bind_index().has_value()); + substitutions.push_back( + {.bind_id = entity_name.bind_index(), + .replacement_id = context.constant_values().Get(outer_arg_id)}); + } + // Take any trailing argument values from the self specific. - // TODO: If these refer to outer arguments, for example in their types, we may - // need to perform extra substitutions here. for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) { - auto new_arg_id = context.constant_values().GetConstantInstId(arg_id); + auto subst_const_id = SubstConstant( + context, loc_id, context.constant_values().Get(arg_id), substitutions); + auto new_arg_id = context.constant_values().GetInstId(subst_const_id); if (index_delta) { // If this parameter would have a new index in the context described by // `enclosing_specific_id`, form a new binding with an adjusted index. auto bind_name = context.insts().GetAs( - context.constant_values().GetConstantInstId(arg_id)); + context.constant_values().GetConstantInstId(new_arg_id)); auto entity_name = context.entity_names().Get(bind_name.entity_name_id); entity_name.bind_index_value += index_delta; CARBON_CHECK(entity_name.bind_index_value >= 0); diff --git a/toolchain/check/testdata/impl/generic_method_constraint.carbon b/toolchain/check/testdata/impl/generic_method_constraint.carbon new file mode 100644 index 000000000000..e6beca222161 --- /dev/null +++ b/toolchain/check/testdata/impl/generic_method_constraint.carbon @@ -0,0 +1,69 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/generic_method_constraint.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/generic_method_constraint.carbon + +// Regression tests for https://github.com/carbon-language/carbon-lang/issues/7919: +// building an impl witness thunk must not crash when an associated function's +// generic parameter constraint depends on an enclosing interface parameter. + +// --- dependent_on_interface_param.carbon + +library "[[@TEST_NAME]]"; + +interface A(T: type) { + fn F(self); +} + +interface B(T: type) { + fn G[U: A(T)](u: U); +} + +class C {} + +impl C as B(()) { + fn G[U: A(())](unused u: U) {} +} + +// --- dependent_on_self.carbon + +library "[[@TEST_NAME]]"; + +interface A(T: type) { + fn F(self); +} + +interface B(T: type) { + fn G[U: A(Self)](u: U); +} + +class C {} + +impl C as B(()) { + fn G[U: A(C)](unused u: U) {} +} + +// --- independent_constraint_still_ok.carbon + +library "[[@TEST_NAME]]"; + +interface A { + fn F(self); +} + +interface B(T: type) { + fn G[U: A](u: U, t: T); +} + +class C {} + +impl C as B(()) { + fn G[U: A](unused u: U, unused t: ()) {} +}