From afefaa82c1444225a81d3c369318fe4bd18fe716 Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Thu, 18 Dec 2025 15:39:55 +0000 Subject: [PATCH] [cxx-interop] Avoid miscompiling calls to function templates with added metatype parameters C++ allows function templates that do not use all of their template parameters in the signature, such as: ``` template bool foo(); ``` This is not compatible with Swift's generic model, so to let clients call such functions from Swift, ClangImporter generates an extra metatype parameter: ``` func foo(T: T.Type) -> Bool // Usage: foo(Int.self) foo(Bool.self) ``` That logic had a flaw that surfaced when the C++ function's behavior differs depending on the templated parameter. Instead of instantiating the function template with all of the required template parameters, and using the corresponding instantiation for each call, Swift was (correctly) instantiating the template for the necessary types, but then silently dropping all of the instantiations except for one, and using the remaining one for all callsites. This happened because all of the instantiations were getting the same name in Swift, and therefore had the exact same mangled name, which caused SILGen to pick one and discard the others. This makes sure that if the extra metatype parameter was added to a function template, all of the function instantiations get unique mangled names. rdar://166184513 --- lib/ClangImporter/ClangImporter.cpp | 14 +++++++++ ...etained-unretained-attributes-silgen.swift | 28 ++++++++--------- ...template-type-parameter-not-in-signature.h | 14 ++++++++- .../templates/function-template-silgen.swift | 8 ++--- .../templates/member-templates-silgen.swift | 30 +++++++++---------- ...pe-parameter-not-in-signature-silgen.swift | 17 +++++++++++ ...late-type-parameter-not-in-signature.swift | 8 +++-- 7 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 test/Interop/Cxx/templates/template-type-parameter-not-in-signature-silgen.swift diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 4e7a0c999b06a..1d211fa28436b 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -7818,6 +7818,20 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst, if (!inserted) return ConcreteDeclRef(fnIt->second); + // Before we proceed to import the newly instantiated C++ function, make sure + // it gets a unique name in Swift. In particular, it must have a different + // name from all the other instantiations of the same function template, to + // prevent Swift from incorrectly deduplicating the multiple instantiations, + // which would cause a silent miscompile. The naming itself is not important, + // since this will only get called from synthesized code. Let's use the + // mangled Clang name of this instantiation as the swift_name. + std::string mangledNewFn; + llvm::raw_string_ostream mangleRawStream(mangledNewFn); + mangleRawStream << "__swift_specializedThunk_"; + getMangledName(mangleRawStream, newFn); + newFn->addAttr(clang::SwiftNameAttr::CreateImplicit(newFn->getASTContext(), + mangledNewFn)); + auto *newDecl = cast_or_null( decl->getASTContext().getClangModuleLoader()->importDeclDirectly(newFn)); if (!newDecl) diff --git a/test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-silgen.swift b/test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-silgen.swift index 536853614b19b..e4d85f48d841b 100644 --- a/test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-silgen.swift +++ b/test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-silgen.swift @@ -189,46 +189,46 @@ func testtFreeFunctionsTemplated(frt : FRTStruct, nonFrt: NonFRTStruct) { let frtLocalVar1 : Int = 1; let frtLocalVar2 = global_templated_function_returning_FRT(frtLocalVar1) - // CHECK: function_ref @$sSo39global_templated_function_returning_FRTySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo84__swift_specializedThunk__Z39global_templated_function_returning_FRTIlEP9FRTStructT_ySo0J0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar3 = global_templated_function_returning_FRT_copy(frtLocalVar1) - // CHECK: function_ref @$sSo44global_templated_function_returning_FRT_copyySo9FRTStructVSiFTo : $@convention(c) (Int) -> @owned FRTStruct + // CHECK: function_ref @$sSo89__swift_specializedThunk__Z44global_templated_function_returning_FRT_copyIlEP9FRTStructT_ySo0L0VSiFTo : $@convention(c) (Int) -> @owned FRTStruct let frtLocalVar4 = global_templated_function_returning_FRT_create(frtLocalVar1) - // CHECK: function_ref @$sSo46global_templated_function_returning_FRT_createySo9FRTStructVSiFTo : $@convention(c) (Int) -> @owned FRTStruct + // CHECK: function_ref @$sSo91__swift_specializedThunk__Z46global_templated_function_returning_FRT_createIlEP9FRTStructT_ySo0L0VSiFTo : $@convention(c) (Int) -> @owned FRTStruct let frtLocalVar5 = global_templated_function_returning_FRT_init(frtLocalVar1) - // CHECK: function_ref @$sSo44global_templated_function_returning_FRT_initySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo89__swift_specializedThunk__Z44global_templated_function_returning_FRT_initIlEP9FRTStructT_ySo0L0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar6 = global_templated_function_returning_FRT_clone(frtLocalVar1) - // CHECK: function_ref @$sSo45global_templated_function_returning_FRT_cloneySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo90__swift_specializedThunk__Z45global_templated_function_returning_FRT_cloneIlEP9FRTStructT_ySo0L0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar7 = global_templated_function_returning_FRT_with_attr_returns_retained(frtLocalVar1) - // CHECK: function_ref @$sSo66global_templated_function_returning_FRT_with_attr_returns_retainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> @owned FRTStruct + // CHECK: function_ref @$sSo111__swift_specializedThunk__Z66global_templated_function_returning_FRT_with_attr_returns_retainedIlEP9FRTStructT_ySo0O0VSiFTo : $@convention(c) (Int) -> @owned FRTStruct let frtLocalVar8 = global_templated_function_returning_FRT_copy_with_attr_returns_retained(frtLocalVar1) - // CHECK: function_ref @$sSo71global_templated_function_returning_FRT_copy_with_attr_returns_retainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> @owned FRTStruct + // CHECK: function_ref @$sSo116__swift_specializedThunk__Z71global_templated_function_returning_FRT_copy_with_attr_returns_retainedIlEP9FRTStructT_ySo0P0VSiFTo : $@convention(c) (Int) -> @owned FRTStruct let frtLocalVar9 = global_templated_function_returning_FRT_create_with_attr_returns_retained(frtLocalVar1) - // CHECK: function_ref @$sSo73global_templated_function_returning_FRT_create_with_attr_returns_retainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> @owned FRTStruct + // CHECK: function_ref @$sSo118__swift_specializedThunk__Z73global_templated_function_returning_FRT_create_with_attr_returns_retainedIlEP9FRTStructT_ySo0P0VSiFTo : $@convention(c) (Int) -> @owned FRTStruct let frtLocalVar10 = global_templated_function_returning_FRT_with_attr_returns_unretained(frtLocalVar1) - // CHECK: function_ref @$sSo68global_templated_function_returning_FRT_with_attr_returns_unretainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo113__swift_specializedThunk__Z68global_templated_function_returning_FRT_with_attr_returns_unretainedIlEP9FRTStructT_ySo0O0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar11 = global_templated_function_returning_FRT_copy_with_attr_returns_unretained(frtLocalVar1) - // CHECK: function_ref @$sSo73global_templated_function_returning_FRT_copy_with_attr_returns_unretainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo118__swift_specializedThunk__Z73global_templated_function_returning_FRT_copy_with_attr_returns_unretainedIlEP9FRTStructT_ySo0P0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar12 = global_templated_function_returning_FRT_create_with_attr_returns_unretained(frtLocalVar1) - // CHECK: function_ref @$sSo75global_templated_function_returning_FRT_create_with_attr_returns_unretainedySo9FRTStructVSiFTo : $@convention(c) (Int) -> FRTStruct + // CHECK: function_ref @$sSo120__swift_specializedThunk__Z75global_templated_function_returning_FRT_create_with_attr_returns_unretainedIlEP9FRTStructT_ySo0P0VSiFTo : $@convention(c) (Int) -> FRTStruct let frtLocalVar13 = global_function_returning_templated_retrun_frt(frt) - // CHECK: function_ref @$sSo46global_function_returning_templated_retrun_frtySo9FRTStructVACFTo : $@convention(c) (FRTStruct) -> FRTStruct + // CHECK: function_ref @$sSo93__swift_specializedThunk__Z46global_function_returning_templated_retrun_frtIP9FRTStructET_S2_ySo0K0VACFTo : $@convention(c) (FRTStruct) -> FRTStruct let frtLocalVar14 = global_function_returning_templated_retrun_frt_owned(frt) - // CHECK: function_ref @$sSo52global_function_returning_templated_retrun_frt_ownedySo9FRTStructVACFTo : $@convention(c) (FRTStruct) -> @owned FRTStruct + // CHECK: function_ref @$sSo99__swift_specializedThunk__Z52global_function_returning_templated_retrun_frt_ownedIP9FRTStructET_S2_ySo0L0VACFTo : $@convention(c) (FRTStruct) -> @owned FRTStruct let nonFrtLocalVar1 = global_function_returning_templated_retrun_frt_owned(nonFrt) - // CHECK: function_ref @$sSo52global_function_returning_templated_retrun_frt_ownedySo12NonFRTStructVACFTo : $@convention(c) (NonFRTStruct) -> NonFRTStruct + // CHECK: function_ref @$sSo102__swift_specializedThunk__Z52global_function_returning_templated_retrun_frt_ownedI12NonFRTStructET_S1_ySo0lM0VACFTo : $@convention(c) (NonFRTStruct) -> NonFRTStruct } func testVirtualMethods(base: Base, derived: Derived) { diff --git a/test/Interop/Cxx/templates/Inputs/template-type-parameter-not-in-signature.h b/test/Interop/Cxx/templates/Inputs/template-type-parameter-not-in-signature.h index 22987ead23288..97bca2669705f 100644 --- a/test/Interop/Cxx/templates/Inputs/template-type-parameter-not-in-signature.h +++ b/test/Interop/Cxx/templates/Inputs/template-type-parameter-not-in-signature.h @@ -16,7 +16,19 @@ struct Struct { }; template -void templateTypeParamNotUsedInSignature() {} +struct is_bool { + constexpr static bool value = false; +}; + +template <> +struct is_bool { + constexpr static bool value = true; +}; + +template +bool templateTypeParamNotUsedInSignature() { + return is_bool::value; +} template void multiTemplateTypeParamNotUsedInSignature() {} diff --git a/test/Interop/Cxx/templates/function-template-silgen.swift b/test/Interop/Cxx/templates/function-template-silgen.swift index b4e1f330505ca..947f31693d5c5 100644 --- a/test/Interop/Cxx/templates/function-template-silgen.swift +++ b/test/Interop/Cxx/templates/function-template-silgen.swift @@ -7,16 +7,16 @@ import FunctionTemplates // CHECK: bb0(%0 : $Int32): // CHECK: [[IL_ZERO:%.*]] = integer_literal $Builtin.Int32, 0 // CHECK: [[ZERO:%.*]] = struct $Int32 ([[IL_ZERO]] : $Builtin.Int32) -// CHECK: [[PASS_THROUGH_CONST_FN:%.*]] = function_ref @$sSo16passThroughConstys5Int32VACFTo : $@convention(c) (Int32) -> Int32 +// CHECK: [[PASS_THROUGH_CONST_FN:%.*]] = function_ref @$sSo54__swift_specializedThunk__Z16passThroughConstIiEKT_S0_ys5Int32VACFTo : $@convention(c) (Int32) -> Int32 // CHECK: [[A:%.*]] = apply [[PASS_THROUGH_CONST_FN]]([[ZERO]]) : $@convention(c) (Int32) -> Int32 -// CHECK: [[PASS_THROUGH_FN:%.*]] = function_ref @$sSo11passThroughys5Int32VACFTo : $@convention(c) (Int32) -> Int32 +// CHECK: [[PASS_THROUGH_FN:%.*]] = function_ref @$sSo48__swift_specializedThunk__Z11passThroughIiET_S0_ys5Int32VACFTo : $@convention(c) (Int32) -> Int32 // CHECK: [[B:%.*]] = apply [[PASS_THROUGH_FN]](%0) : $@convention(c) (Int32) -> Int32 -// CHECK: [[ADD_TWO_FN:%.*]] = function_ref @$sSo18addMixedTypeParamsys5Int32VAC_ACtFTo : $@convention(c) (Int32, Int32) -> Int32 +// CHECK: [[ADD_TWO_FN:%.*]] = function_ref @$sSo59__swift_specializedThunk__Z18addMixedTypeParamsIiiET_S0_T0_ys5Int32VAC_ACtFTo : $@convention(c) (Int32, Int32) -> Int32 // CHECK: [[C:%.*]] = apply [[ADD_TWO_FN]]([[A]], [[B]]) : $@convention(c) (Int32, Int32) -> Int32 -// CHECK: [[ADD_FN:%.*]] = function_ref @$sSo17addSameTypeParamsys5Int32VAC_ACtFTo : $@convention(c) (Int32, Int32) -> Int32 +// CHECK: [[ADD_FN:%.*]] = function_ref @$sSo054__swift_specializedThunk__Z17addSameTypeParamsIiET_S0_J1_ys5Int32VAC_ACtFTo : $@convention(c) (Int32, Int32) -> Int32 // CHECK: [[OUT:%.*]] = apply [[ADD_FN]]([[B]], [[C_32:%.*]]) : $@convention(c) (Int32, Int32) -> Int32 // CHECK: return [[OUT]] : $Int32 diff --git a/test/Interop/Cxx/templates/member-templates-silgen.swift b/test/Interop/Cxx/templates/member-templates-silgen.swift index 3e4f941bfcc64..c0b3d143a5790 100644 --- a/test/Interop/Cxx/templates/member-templates-silgen.swift +++ b/test/Interop/Cxx/templates/member-templates-silgen.swift @@ -4,16 +4,16 @@ import MemberTemplates // CHECK-LABEL: sil hidden @$s4main9basicTestyyF : $@convention(thin) () -> () -// CHECK: [[ADD:%.*]] = function_ref @$sSo18HasMemberTemplatesV17addSameTypeParamsys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK: [[ADD:%.*]] = function_ref @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18ab37Templates17addSameTypeParamsIiEET_S1_N1_ys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 // CHECK: apply [[ADD]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK: [[ADD_TWO_TEMPLATES:%.*]] = function_ref @$sSo18HasMemberTemplatesV18addMixedTypeParamsys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK: [[ADD_TWO_TEMPLATES:%.*]] = function_ref @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB42Templates18addMixedTypeParamsIiiEET_S1_T0_ys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 // CHECK: apply [[ADD_TWO_TEMPLATES]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK: [[ADD_ALL:%.*]] = function_ref @$sSo18HasMemberTemplatesV6addAllys5Int32VAE_A2EtFTo : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK: [[ADD_ALL:%.*]] = function_ref @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB28Templates6addAllIiiEEiiT_T0_ys5Int32VAE_A2EtFTo : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 // CHECK: apply [[ADD_ALL]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK: [[DO_NOTHING:%.*]] = function_ref @$sSo18HasMemberTemplatesV17doNothingConstRefyys5Int32VFTo : $@convention(cxx_method) (@in_guaranteed Int32, @inout HasMemberTemplates) -> () +// CHECK: [[DO_NOTHING:%.*]] = function_ref @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB37Templates17doNothingConstRefIiEEvRKT_yys5Int32VFTo : $@convention(cxx_method) (@in_guaranteed Int32, @inout HasMemberTemplates) -> () // CHECK: apply [[DO_NOTHING]]({{.*}}) : $@convention(cxx_method) (@in_guaranteed Int32, @inout HasMemberTemplates) -> () // CHECK-LABEL: end sil function '$s4main9basicTestyyF' @@ -26,17 +26,17 @@ func basicTest() { obj.doNothingConstRef(i) } -// CHECK-LABEL: sil [asmname "{{.*}}addSameTypeParams{{.*}}"] [clang HasMemberTemplates.addSameTypeParams] @$sSo18HasMemberTemplatesV17addSameTypeParamsys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK-LABEL: sil [asmname "{{.*}}addSameTypeParams{{.*}}"] [clang HasMemberTemplates.__swift_specializedThunk__ZN18HasMemberTemplates17addSameTypeParamsIiEET_S1_S1_] @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18ab37Templates17addSameTypeParamsIiEET_S1_N1_ys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK-LABEL: sil [asmname "{{.*}}addMixedTypeParams{{.*}}"] [clang HasMemberTemplates.addMixedTypeParams] @$sSo18HasMemberTemplatesV18addMixedTypeParamsys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK-LABEL: sil [asmname "{{.*}}addMixedTypeParams{{.*}}"] [clang HasMemberTemplates.__swift_specializedThunk__ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_] @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB42Templates18addMixedTypeParamsIiiEET_S1_T0_ys5Int32VAE_AEtFTo : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK-LABEL: sil [asmname "{{.*}}addAll{{.*}}"] [clang HasMemberTemplates.addAll] @$sSo18HasMemberTemplatesV6addAllys5Int32VAE_A2EtFTo : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 +// CHECK-LABEL: sil [asmname "{{.*}}addAll{{.*}}"] [clang HasMemberTemplates.__swift_specializedThunk__ZN18HasMemberTemplates6addAllIiiEEiiT_T0_] @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB28Templates6addAllIiiEEiiT_T0_ys5Int32VAE_A2EtFTo : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 -// CHECK-LABEL: sil [asmname "{{.*}}doNothingConstRef{{.*}}"] [clang HasMemberTemplates.doNothingConstRef] @$sSo18HasMemberTemplatesV17doNothingConstRefyys5Int32VFTo : $@convention(cxx_method) (@in_guaranteed Int32, @inout HasMemberTemplates) -> () +// CHECK-LABEL: sil [asmname "{{.*}}doNothingConstRef{{.*}}"] [clang HasMemberTemplates.__swift_specializedThunk__ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_] @$sSo18HasMemberTemplatesV030__swift_specializedThunk__ZN18aB37Templates17doNothingConstRefIiEEvRKT_yys5Int32VFTo : $@convention(cxx_method) (@in_guaranteed Int32, @inout HasMemberTemplates) -> () // CHECK-LABEL: sil hidden @$s4main12testSetValueyyF : $@convention(thin) () -> () -// CHECK: [[SET_VALUE:%.*]] = function_ref @$sSo0044TemplateClassWithMemberTemplatesCInt_CeCInkcV8setValueyySiFTo : $@convention(cxx_method) (Int, @inout TemplateClassWithMemberTemplates) -> () +// CHECK: [[SET_VALUE:%.*]] = function_ref @$sSo0044TemplateClassWithMemberTemplatesCInt_CeCInkcV81__swift_specializedThunk__ZN32TemplateClassWithMemberTemplatesIiE8setValueIlEEvT_yySiFTo : $@convention(cxx_method) (Int, @inout TemplateClassWithMemberTemplates) -> () // CHECK: apply [[SET_VALUE]]({{.*}}) : $@convention(cxx_method) (Int, @inout TemplateClassWithMemberTemplates) -> () // CHECK-LABEL: end sil function '$s4main12testSetValueyyF' @@ -47,13 +47,13 @@ func testSetValue() { // CHECK-LABEL: sil hidden @$s4main17testStaticMembersyyF : $@convention(thin) () -> () -// CHECK: [[ADD_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV3addyS2i_SitFZTo : $@convention(c) (Int, Int) -> Int +// CHECK: [[ADD_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abc22Templates3addIlEET_S1_L1_yS2i_SitFZTo : $@convention(c) (Int, Int) -> Int // CHECK: apply [[ADD_FN]]({{.*}}) : $@convention(c) (Int, Int) -> Int -// CHECK: [[ADD_TWO_TEMPLATES_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV06addTwoD0yS2i_s4Int8VtFZTo : $@convention(c) (Int, Int8) -> Int +// CHECK: [[ADD_TWO_TEMPLATES_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abc17Templates15addTwoD13IlcEET_S1_T0_yS2i_s4Int8VtFZTo : $@convention(c) (Int, Int8) -> Int // CHECK: apply [[ADD_TWO_TEMPLATES_FN]]({{.*}}) : $@convention(c) (Int, Int8) -> Int -// CHECK: [[REMOVE_REFERENCE_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV15removeReferenceyS2izFZTo : $@convention(c) (@inout Int) -> Int +// CHECK: [[REMOVE_REFERENCE_FN:%.*]] = function_ref @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abC36Templates15removeReferenceIlEET_RS1_yS2izFZTo : $@convention(c) (@inout Int) -> Int // CHECK: apply [[REMOVE_REFERENCE_FN]]({{.*}}) : $@convention(c) (@inout Int) -> Int // CHECK-LABEL: end sil function '$s4main17testStaticMembersyyF' @@ -65,8 +65,8 @@ func testStaticMembers() { HasStaticMemberTemplates.removeReference(&x) } -// CHECK: sil hidden_external [asmname "{{.*}}add{{.*}}"] [clang HasStaticMemberTemplates.add] @$sSo24HasStaticMemberTemplatesV3addyS2i_SitFZTo : $@convention(c) (Int, Int) -> Int +// CHECK: sil hidden_external [asmname "{{.*}}add{{.*}}"] [clang HasStaticMemberTemplates.__swift_specializedThunk__ZN24HasStaticMemberTemplates3addIlEET_S1_S1_] @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abc22Templates3addIlEET_S1_L1_yS2i_SitFZTo : $@convention(c) (Int, Int) -> Int -// CHECK: sil hidden_external [asmname "{{.*}}addTwoTemplates{{.*}}"] [clang HasStaticMemberTemplates.addTwoTemplates] @$sSo24HasStaticMemberTemplatesV06addTwoD0yS2i_s4Int8VtFZTo : $@convention(c) (Int, Int8) -> Int +// CHECK: sil hidden_external [asmname "{{.*}}addTwoTemplates{{.*}}"] [clang HasStaticMemberTemplates.__swift_specializedThunk__ZN24HasStaticMemberTemplates15addTwoTemplatesIlcEET_S1_T0_] @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abc17Templates15addTwoD13IlcEET_S1_T0_yS2i_s4Int8VtFZTo : $@convention(c) (Int, Int8) -> Int -// CHECK: sil hidden_external [asmname "{{.*}}removeReference{{.*}}"] [clang HasStaticMemberTemplates.removeReference] @$sSo24HasStaticMemberTemplatesV15removeReferenceyS2izFZTo : $@convention(c) (@inout Int) -> Int +// CHECK: sil hidden_external [asmname "{{.*}}removeReference{{.*}}"] [clang HasStaticMemberTemplates.__swift_specializedThunk__ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_] @$sSo24HasStaticMemberTemplatesV030__swift_specializedThunk__ZN24abC36Templates15removeReferenceIlEET_RS1_yS2izFZTo : $@convention(c) (@inout Int) -> Int diff --git a/test/Interop/Cxx/templates/template-type-parameter-not-in-signature-silgen.swift b/test/Interop/Cxx/templates/template-type-parameter-not-in-signature-silgen.swift new file mode 100644 index 0000000000000..696c7806ee2cd --- /dev/null +++ b/test/Interop/Cxx/templates/template-type-parameter-not-in-signature-silgen.swift @@ -0,0 +1,17 @@ +// RUN: %target-swift-emit-silgen %s -cxx-interoperability-mode=default -I %S/Inputs | %FileCheck %s + +import TemplateTypeParameterNotInSignature + +_ = templateTypeParamNotUsedInSignature(T: Int.self) +_ = templateTypeParamNotUsedInSignature(T: Bool.self) + + +// CHECK: sil [transparent] [serialized] [ossa] @$sSC35templateTypeParamNotUsedInSignatureySbSimF : $@convention(thin) (@thin Int.Type) -> Bool { +// CHECK: bb0(%0 : $@thin Int.Type): +// CHECK: {{.*}} = function_ref @$sSo69__swift_specializedThunk__Z35templateTypeParamNotUsedInSignatureIlEbvSbyFTo : $@convention(c) () -> Bool +// CHECK: } // end sil function '$sSC35templateTypeParamNotUsedInSignatureySbSimF' + +// CHECK: sil [transparent] [serialized] [ossa] @$sSC35templateTypeParamNotUsedInSignatureyS2bmF : $@convention(thin) (@thin Bool.Type) -> Bool { +// CHECK: bb0(%0 : $@thin Bool.Type): +// CHECK: {{.*}} = function_ref @$sSo69__swift_specializedThunk__Z35templateTypeParamNotUsedInSignatureIbEbvSbyFTo : $@convention(c) () -> Bool +// CHECK: } // end sil function '$sSC35templateTypeParamNotUsedInSignatureyS2bmF' diff --git a/test/Interop/Cxx/templates/template-type-parameter-not-in-signature.swift b/test/Interop/Cxx/templates/template-type-parameter-not-in-signature.swift index 7e01f818edb21..b5d28f4515a5f 100644 --- a/test/Interop/Cxx/templates/template-type-parameter-not-in-signature.swift +++ b/test/Interop/Cxx/templates/template-type-parameter-not-in-signature.swift @@ -19,8 +19,12 @@ TemplateNotInSignatureTestSuite.test("Function with defaulted template type para TemplateNotInSignatureTestSuite.test("Instantiate the same function template twice.") { // Intentionally test the same thing twice. - templateTypeParamNotUsedInSignature(T: Int.self) - templateTypeParamNotUsedInSignature(T: Int.self) + let res1 = templateTypeParamNotUsedInSignature(T: Int.self) + expectFalse(res1) + let res2 = templateTypeParamNotUsedInSignature(T: Int.self) + expectFalse(res2) + let res3 = templateTypeParamNotUsedInSignature(T: Bool.self) + expectTrue(res3) } TemplateNotInSignatureTestSuite.test("Pointer types") {