Skip to content

Commit 9c22a7a

Browse files
committed
[cxx-interop] Memberwise init is not synthesized for C++ type with templated using decl
When Swift fails to import a member of a struct, it checks to see if this member could affect the memory layout of the struct, and if it can, Swift doesn't synthesize the memberwise initializer for this struct. This logic was overly restrictive and treated templated using-decls as potentially affecting the memory layout of the struct. rdar://113044949
1 parent b3cd553 commit 9c22a7a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,9 @@ namespace {
22362236
Decl *member = Impl.importDecl(nd, getActiveSwiftVersion());
22372237

22382238
if (!member) {
2239-
if (!isa<clang::TypeDecl>(nd) && !isa<clang::FunctionDecl>(nd)) {
2239+
if (!isa<clang::TypeDecl>(nd) && !isa<clang::FunctionDecl>(nd) &&
2240+
!isa<clang::TypeAliasTemplateDecl>(nd) &&
2241+
!isa<clang::FunctionTemplateDecl>(nd)) {
22402242
// We don't know what this member is.
22412243
// Assume it may be important in C.
22422244
hasUnreferenceableStorage = true;

test/Interop/Cxx/class/Inputs/memberwise-initializer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MEMBERWISE_INITIALIZER_H
22
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBERWISE_INITIALIZER_H
33

4+
template <typename T>
5+
struct TemplatedType {};
6+
7+
48
struct StructPrivateOnly {
59
private:
610
int varPrivate;
@@ -52,4 +56,20 @@ struct ClassWithUnimportedMemberFunction {
5256
int ClassWithUnimportedMemberFunction::* unimportedMemberFunction();
5357
};
5458

59+
struct ClassWithTemplatedFunction {
60+
public:
61+
int varPublic;
62+
63+
template <int I>
64+
void foo();
65+
};
66+
67+
struct ClassWithTemplatedUsingDecl {
68+
public:
69+
int varPublic;
70+
71+
template <typename T>
72+
using MyUsing = TemplatedType<T>;
73+
};
74+
5575
#endif

test/Interop/Cxx/class/memberwise-initializer-module-interface.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@
4242
// CHECK-NEXT: init(varPublic: Int32)
4343
// CHECK-NEXT: var varPublic: Int32
4444
// CHECK-NEXT: }
45+
// CHECK-NEXT: struct ClassWithTemplatedFunction {
46+
// CHECK-NEXT: init()
47+
// CHECK-NEXT: init(varPublic: Int32)
48+
// CHECK-NEXT: var varPublic: Int32
49+
// CHECK-NEXT: }
50+
// CHECK-NEXT: struct ClassWithTemplatedUsingDecl {
51+
// CHECK-NEXT: init()
52+
// CHECK-NEXT: init(varPublic: Int32)
53+
// CHECK-NEXT: var varPublic: Int32
54+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)