Skip to content

Commit e355430

Browse files
authored
Merge pull request swiftlang#31619 from MForster/m/memberwise-initializer
Synthesize memberwise initializers despite AccessSpecDecl
2 parents ad3ea02 + d611b4c commit e355430

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "swift/Config.h"
4646
#include "clang/AST/ASTContext.h"
4747
#include "clang/AST/Attr.h"
48+
#include "clang/AST/DeclCXX.h"
4849
#include "clang/Basic/CharInfo.h"
4950
#include "swift/Basic/Statistic.h"
5051
#include "clang/Basic/TargetInfo.h"
@@ -3311,6 +3312,12 @@ namespace {
33113312
// it is nested in a struct.
33123313

33133314
for (auto m : decl->decls()) {
3315+
if (isa<clang::AccessSpecDecl>(m)) {
3316+
// The presence of AccessSpecDecls themselves does not influence
3317+
// whether we can generate a member-wise initializer.
3318+
continue;
3319+
}
3320+
33143321
auto nd = dyn_cast<clang::NamedDecl>(m);
33153322
if (!nd) {
33163323
// We couldn't import the member, so we can't reference it in Swift.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MEMBERWISE_INITIALIZER_H
2+
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBERWISE_INITIALIZER_H
3+
4+
struct StructPrivateOnly {
5+
private:
6+
int varPrivate;
7+
};
8+
9+
struct StructPublicOnly {
10+
int varPublic;
11+
};
12+
13+
struct StructEmptyPrivateSection {
14+
int varPublic;
15+
private:
16+
};
17+
18+
struct StructPublicAndPrivate {
19+
int varPublic;
20+
private:
21+
int varPrivate;
22+
};
23+
24+
class ClassPrivateOnly {
25+
int varPrivate;
26+
};
27+
28+
class ClassPublicOnly {
29+
public:
30+
int varPublic;
31+
};
32+
33+
class ClassEmptyPublicSection {
34+
int varPrivate;
35+
public:
36+
};
37+
38+
class ClassPrivateAndPublic {
39+
int varPrivate;
40+
public:
41+
int varPublic;
42+
};
43+
44+
#endif

test/Interop/Cxx/class/Inputs/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module AccessSpecifiers {
22
header "access-specifiers.h"
33
}
44

5+
module MemberwiseInitializer {
6+
header "memberwise-initializer.h"
7+
}
8+
59
module MemoryLayout {
610
header "memory-layout.h"
711
}

test/Interop/Cxx/class/member-variables-module-interface.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
// CHECK: struct MyClass {
44
// CHECK-NEXT: var const_member: Int32 { get }
55
// CHECK-NEXT: init()
6+
// CHECK-NEXT: init(const_member: Int32)
67
// CHECK-NEXT: }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=MemberwiseInitializer -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s
2+
3+
// CHECK: struct StructPrivateOnly {
4+
// CHECK-NEXT: init()
5+
// CHECK-NEXT: }
6+
// CHECK-NEXT: struct StructPublicOnly {
7+
// CHECK-NEXT: var varPublic: Int32
8+
// CHECK-NEXT: init()
9+
// CHECK-NEXT: init(varPublic: Int32)
10+
// CHECK-NEXT: }
11+
// CHECK-NEXT: struct StructEmptyPrivateSection {
12+
// CHECK-NEXT: var varPublic: Int32
13+
// CHECK-NEXT: init()
14+
// CHECK-NEXT: init(varPublic: Int32)
15+
// CHECK-NEXT: }
16+
// CHECK-NEXT: struct StructPublicAndPrivate {
17+
// CHECK-NEXT: var varPublic: Int32
18+
// CHECK-NEXT: init()
19+
// CHECK-NEXT: }
20+
// CHECK-NEXT: struct ClassPrivateOnly {
21+
// CHECK-NEXT: init()
22+
// CHECK-NEXT: }
23+
// CHECK-NEXT: struct ClassPublicOnly {
24+
// CHECK-NEXT: var varPublic: Int32
25+
// CHECK-NEXT: init()
26+
// CHECK-NEXT: init(varPublic: Int32)
27+
// CHECK-NEXT: }
28+
// CHECK-NEXT: struct ClassEmptyPublicSection {
29+
// CHECK-NEXT: init()
30+
// CHECK-NEXT: }
31+
// CHECK-NEXT: struct ClassPrivateAndPublic {
32+
// CHECK-NEXT: var varPublic: Int32
33+
// CHECK-NEXT: init()
34+
// CHECK-NEXT: }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop
2+
3+
import MemberwiseInitializer
4+
5+
let structPrivateOnly = StructPrivateOnly(varPrivate: 42) // expected-error {{argument passed to call that takes no arguments}}
6+
let structPublicOnly = StructPublicOnly(varPublic: 42)
7+
let structEmptyPrivateSetion = StructEmptyPrivateSection(varPublic: 42)
8+
let structPublicAndPrivate1 = StructPublicAndPrivate(varPublic: 42) // expected-error {{argument passed to call that takes no arguments}}
9+
let structPublicAndPrivate2 = StructPublicAndPrivate(varPublic: 42, varPrivate: 23) // expected-error {{argument passed to call that takes no arguments}}
10+
11+
let classPrivateOnly = ClassPrivateOnly(varPrivate: 42) // expected-error {{argument passed to call that takes no arguments}}
12+
let classPublicOnly = ClassPublicOnly(varPublic: 42)
13+
let classEmptyPublicSetion = ClassEmptyPublicSection(varPrivate: 42) // expected-error {{argument passed to call that takes no arguments}}
14+
let classPublicAndPrivate1 = ClassPrivateAndPublic(varPublic: 23) // expected-error {{argument passed to call that takes no arguments}}
15+
let classPublicAndPrivate2 = ClassPrivateAndPublic(varPrivate: 42, varPublic: 23) // expected-error {{argument passed to call that takes no arguments}}

0 commit comments

Comments
 (0)