Skip to content

Commit 340c855

Browse files
authored
Merge pull request #38121 from egorzhdan/cxx-init-static
C++ Interop: avoid adding static members to the value constructor
2 parents e059d5c + 0aebf79 commit 340c855

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,9 @@ createValueConstructor(ClangImporter::Implementation &Impl,
13991399
// Construct the set of parameters from the list of members.
14001400
SmallVector<ParamDecl *, 8> valueParameters;
14011401
for (auto var : members) {
1402+
if (var->isStatic())
1403+
continue;
1404+
14021405
bool generateParamName = wantCtorParamNames;
14031406

14041407
if (var->hasClangNode()) {

test/Interop/Cxx/static/Inputs/static-member-var.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class WithConstexprStaticMember {
3737
constexpr static float definedInlineFromMethod = getFloatValue();
3838
};
3939

40+
class WithStaticAndInstanceMember {
41+
public:
42+
int myInstance;
43+
static int myStatic;
44+
};
45+
4046
class ClassA {
4147
public:
4248
static int notUniqueName;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=StaticMemberVar -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s
2+
3+
// CHECK: struct WithStaticAndInstanceMember {
4+
// CHECK-NEXT: static var myStatic: Int32
5+
// CHECK-NEXT: var myInstance: Int32
6+
// CHECK-NEXT: init()
7+
// CHECK-NEXT: init(myInstance: Int32)
8+
// CHECK-NEXT: }

test/Interop/Cxx/static/static-member-var.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ StaticMemberVarTestSuite.test("no-collisions") {
9090
expectEqual(169, ClassB.notUniqueName)
9191
}
9292

93+
StaticMemberVarTestSuite.test("init-struct-with-static-member") {
94+
let obj = WithStaticAndInstanceMember(myInstance: 123)
95+
expectEqual(123, obj.myInstance)
96+
}
97+
9398
runAllTests()

0 commit comments

Comments
 (0)