Skip to content

Commit 2386508

Browse files
authored
Merge pull request #79909 from swiftlang/gaborh/ctor-value-type
[cxx-interop] Test static factory backed initializers with value types
2 parents c0a4056 + 14d92ff commit 2386508

File tree

4 files changed

+73
-68
lines changed

4 files changed

+73
-68
lines changed

test/Interop/Cxx/class/Inputs/constructors.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,58 @@ struct MoveConstructorWithOneParameterWithDefaultArg {
122122
: value(other.value + 1) {}
123123
};
124124

125+
struct __attribute__((swift_attr("import_reference")))
126+
__attribute__((swift_attr("retain:retain")))
127+
__attribute__((swift_attr("release:release"))) ImportWithCtor {
128+
int value = 0;
129+
int param1 = 0;
130+
int param2 = 0;
131+
132+
__attribute__((swift_name("init()")))
133+
__attribute__((swift_attr("returns_retained")))
134+
static ImportWithCtor * _Nonnull create() {
135+
return new ImportWithCtor{1};
136+
}
137+
138+
__attribute__((swift_name("init(_:)")))
139+
__attribute__((swift_attr("returns_retained")))
140+
static ImportWithCtor * _Nonnull create(int x) {
141+
return new ImportWithCtor{1, x};
142+
}
143+
144+
__attribute__((swift_name("init(_:_:)")))
145+
__attribute__((swift_attr("returns_retained")))
146+
static ImportWithCtor * _Nonnull create(int x, int y) {
147+
return new ImportWithCtor{1, x, y};
148+
}
149+
150+
__attribute__((swift_name("init(_:_:_:)")))
151+
__attribute__((swift_attr("returns_unretained")))
152+
static ImportWithCtor * _Nonnull create(int x, int y, int z) {
153+
return new ImportWithCtor{0, x, y};
154+
}
155+
};
156+
157+
inline void retain(ImportWithCtor * _Nonnull x) {
158+
x->value++;
159+
}
160+
161+
inline void release(ImportWithCtor * _Nonnull x) {
162+
if (!--x->value)
163+
delete x;
164+
}
165+
166+
class Value {
167+
public:
168+
__attribute__((swift_name("init(x:)")))
169+
static Value forFoo(int x) {
170+
Value ret;
171+
ret.x = x;
172+
return ret;
173+
}
174+
int getX() const { return x; }
175+
private:
176+
int x;
177+
};
178+
125179
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_CONSTRUCTORS_H

test/Interop/Cxx/class/constructors-executable.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
22
//
33
// REQUIRES: executable_test
44

@@ -70,4 +70,22 @@ CxxConstructorTestSuite.test("MoveConstructorWithOneParamWithDefaultArg") {
7070
expectTrue(instance2.value + instance3.value >= 10)
7171
}
7272

73+
CxxConstructorTestSuite.test("ImportStaticFactoryAsInitializer") {
74+
let x = ImportWithCtor()
75+
expectEqual(x.param1, 0)
76+
expectEqual(x.param2, 0)
77+
let y = x
78+
let z = ImportWithCtor(1)
79+
expectEqual(z.param1, 1)
80+
expectEqual(z.param2, 0)
81+
let z2 = ImportWithCtor(2, 3)
82+
expectEqual(z2.param1, 2)
83+
expectEqual(z2.param2, 3)
84+
let z3 = ImportWithCtor(2, 3, 4)
85+
expectEqual(z3.param1, 2)
86+
expectEqual(z3.param2, 3)
87+
let v = Value(x: 2)
88+
expectEqual(v.getX(), 2)
89+
}
90+
7391
runAllTests()

test/Interop/Cxx/foreign-reference/Inputs/constructor.h

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/Interop/Cxx/foreign-reference/constructor-execution.swift

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)