Fix mangling collision for C++ class template specializations (#7269)

Carbon-side thunks (for example the `Copy`/`Destroy` witness thunks
generated for imported C++ types) are mangled by Carbon, and their names
incorporate a fingerprint of the involved types. The instruction
fingerprinter identifies a class only by its name and parent scope,
which is sufficient for Carbon classes but not for imported C++ classes:
different specializations of one class template (and other cases such as
types in anonymous namespaces) share a Carbon name and parent scope. As
a result, the thunks for two distinct specializations could mangle to
the same name, producing a single LLVM function with two definitions and
failing `verifyModule` during lowering.

When fingerprinting a class imported from C++, also include the Clang
mangled name of its type.

Test: toolchain/lower/testdata/interop/cpp/thunks.carbon gains a split
with two specializations of one class template, each requiring a thunk;
their thunks now get distinct mangled names instead of colliding.

Assisted-by: Claude Code

---------

Co-authored-by: Christopher Di Bella <cjdb.ns@gmail.com>
This commit is contained in:
Chandler Carruth
2026-05-30 08:13:00 +00:00
committed by GitHub
co-authored by Christopher Di Bella
parent 3ef128ac91
commit 8a59f2a76b
64 changed files with 4762 additions and 4330 deletions
+12 -12
View File
@@ -35,18 +35,18 @@ fn F() {
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: %Enum: type = class_type @Enum [concrete]
// CHECK:STDOUT: %pattern_type.9f7: type = pattern_type %Enum [concrete]
// CHECK:STDOUT: %pattern_type.361: type = pattern_type %Enum [concrete]
// CHECK:STDOUT: %int_0: %Enum = int_value 0 [concrete]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %Enum.Op.type: type = fn_type @Enum.Op [concrete]
// CHECK:STDOUT: %Enum.Op: %Enum.Op.type = struct_value () [concrete]
// CHECK:STDOUT: %custom_witness.b8d: <witness> = custom_witness (%Enum.Op), @Copy [concrete]
// CHECK:STDOUT: %Copy.facet.ae7: %Copy.type = facet_value %Enum, (%custom_witness.b8d) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.100: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet.ae7) [concrete]
// CHECK:STDOUT: %.e06: type = fn_type_with_self_type %Copy.WithSelf.Op.type.100, %Copy.facet.ae7 [concrete]
// CHECK:STDOUT: %Enum.Op.bound.7be: <bound method> = bound_method %int_0, %Enum.Op [concrete]
// CHECK:STDOUT: %Copy.facet.4ef: %Copy.type = facet_value %Enum, (%custom_witness.b8d) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.bb4: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet.4ef) [concrete]
// CHECK:STDOUT: %.a09: type = fn_type_with_self_type %Copy.WithSelf.Op.type.bb4, %Copy.facet.4ef [concrete]
// CHECK:STDOUT: %Enum.Op.bound.c3d: <bound method> = bound_method %int_0, %Enum.Op [concrete]
// CHECK:STDOUT: %int_1: %Enum = int_value 1 [concrete]
// CHECK:STDOUT: %Enum.Op.bound.c35: <bound method> = bound_method %int_1, %Enum.Op [concrete]
// CHECK:STDOUT: %Enum.Op.bound.6f8: <bound method> = bound_method %int_1, %Enum.Op [concrete]
// CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
// CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete]
// CHECK:STDOUT: }
@@ -68,15 +68,15 @@ fn F() {
// CHECK:STDOUT: fn @F() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.9f7 = ref_binding_pattern a [concrete]
// CHECK:STDOUT: %a.var_patt: %pattern_type.9f7 = var_pattern %a.patt [concrete]
// CHECK:STDOUT: %a.patt: %pattern_type.361 = ref_binding_pattern a [concrete]
// CHECK:STDOUT: %a.var_patt: %pattern_type.361 = var_pattern %a.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a.var: ref %Enum = var %a.var_patt
// CHECK:STDOUT: %Cpp.ref.loc8_21: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Enum.ref.loc8_24: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
// CHECK:STDOUT: %a.ref.loc8: %Enum = name_ref a, imports.%int_0 [concrete = constants.%int_0]
// CHECK:STDOUT: %impl.elem0.loc8: %.e06 = impl_witness_access constants.%custom_witness.b8d, element0 [concrete = constants.%Enum.Op]
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %a.ref.loc8, %impl.elem0.loc8 [concrete = constants.%Enum.Op.bound.7be]
// CHECK:STDOUT: %impl.elem0.loc8: %.a09 = impl_witness_access constants.%custom_witness.b8d, element0 [concrete = constants.%Enum.Op]
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %a.ref.loc8, %impl.elem0.loc8 [concrete = constants.%Enum.Op.bound.c3d]
// CHECK:STDOUT: %Enum.Op.call.loc8: init %Enum = call %bound_method.loc8(%a.ref.loc8) [concrete = constants.%int_0]
// CHECK:STDOUT: assign %a.var, %Enum.Op.call.loc8
// CHECK:STDOUT: %.loc8: type = splice_block %Enum.ref.loc8_13 [concrete = constants.%Enum] {
@@ -88,8 +88,8 @@ fn F() {
// CHECK:STDOUT: %Cpp.ref.loc10: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Enum.ref.loc10: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
// CHECK:STDOUT: %b.ref: %Enum = name_ref b, imports.%int_1 [concrete = constants.%int_1]
// CHECK:STDOUT: %impl.elem0.loc10: %.e06 = impl_witness_access constants.%custom_witness.b8d, element0 [concrete = constants.%Enum.Op]
// CHECK:STDOUT: %bound_method.loc10: <bound method> = bound_method %b.ref, %impl.elem0.loc10 [concrete = constants.%Enum.Op.bound.c35]
// CHECK:STDOUT: %impl.elem0.loc10: %.a09 = impl_witness_access constants.%custom_witness.b8d, element0 [concrete = constants.%Enum.Op]
// CHECK:STDOUT: %bound_method.loc10: <bound method> = bound_method %b.ref, %impl.elem0.loc10 [concrete = constants.%Enum.Op.bound.6f8]
// CHECK:STDOUT: %Enum.Op.call.loc10: init %Enum = call %bound_method.loc10(%b.ref) [concrete = constants.%int_1]
// CHECK:STDOUT: assign %a.ref.loc10, %Enum.Op.call.loc10
// CHECK:STDOUT: %Destroy.Op.bound: <bound method> = bound_method %a.var, constants.%Destroy.Op