From 77975524be1927008b87bbb15a5a09fd091cb85b Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Tue, 19 Aug 2025 17:24:04 +0100 Subject: [PATCH] [cxx-interop] Do not import partial specializations of variables They do not get imported correctly and trigger linker errors. rdar://149232900 / resolves https://github.com/swiftlang/swift/issues/80802 --- lib/ClangImporter/ImportDecl.cpp | 5 +++++ test/ClangImporter/const_values_cxx.swift | 3 --- test/Interop/Cxx/templates/Inputs/module.modulemap | 5 +++++ test/Interop/Cxx/templates/Inputs/variable-template.h | 5 +++++ .../Cxx/templates/variable-template-typechecker.swift | 5 +++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/Interop/Cxx/templates/Inputs/variable-template.h create mode 100644 test/Interop/Cxx/templates/variable-template-typechecker.swift diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 096c8d7315876..40f44f97cf2d8 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -4638,6 +4638,11 @@ namespace { return result; } + Decl *VisitVarTemplatePartialSpecializationDecl( + const clang::VarTemplatePartialSpecializationDecl *decl) { + return nullptr; + } + Decl *VisitImplicitParamDecl(const clang::ImplicitParamDecl *decl) { // Parameters are never directly imported. return nullptr; diff --git a/test/ClangImporter/const_values_cxx.swift b/test/ClangImporter/const_values_cxx.swift index 3283d68e19e97..e1a0c75502444 100644 --- a/test/ClangImporter/const_values_cxx.swift +++ b/test/ClangImporter/const_values_cxx.swift @@ -55,9 +55,6 @@ func foo() { print(MyClass().class_const_int) print(MyClass.class_static_const_int) - - // TODO: This seems to be incorrectly imported, this test here is just to check that the compiler doesn't crash. - print(template_gcd) } // Only imported as external declarations: diff --git a/test/Interop/Cxx/templates/Inputs/module.modulemap b/test/Interop/Cxx/templates/Inputs/module.modulemap index b2764d9204b53..88d4bdd22085f 100644 --- a/test/Interop/Cxx/templates/Inputs/module.modulemap +++ b/test/Interop/Cxx/templates/Inputs/module.modulemap @@ -177,3 +177,8 @@ module UninstantiatableSpecialMembers { header "uninstantiatable-special-members.h" requires cplusplus } + +module VariableTemplate { + header "variable-template.h" + requires cplusplus +} diff --git a/test/Interop/Cxx/templates/Inputs/variable-template.h b/test/Interop/Cxx/templates/Inputs/variable-template.h new file mode 100644 index 0000000000000..ffe1432a24199 --- /dev/null +++ b/test/Interop/Cxx/templates/Inputs/variable-template.h @@ -0,0 +1,5 @@ +template +inline const int template_gcd = template_gcd; + +template +inline const int template_gcd = N; diff --git a/test/Interop/Cxx/templates/variable-template-typechecker.swift b/test/Interop/Cxx/templates/variable-template-typechecker.swift new file mode 100644 index 0000000000000..3e628d34b1873 --- /dev/null +++ b/test/Interop/Cxx/templates/variable-template-typechecker.swift @@ -0,0 +1,5 @@ +// RUN: %target-typecheck-verify-swift -cxx-interoperability-mode=default -I %S/Inputs + +import VariableTemplate + +let _: CInt = template_gcd // expected-error {{cannot find 'template_gcd' in scope}}