Skip to content

[cxx-interop] Test static factory backed initializers with value types #79909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions test/Interop/Cxx/class/Inputs/constructors.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,58 @@ struct MoveConstructorWithOneParameterWithDefaultArg {
: value(other.value + 1) {}
};

struct __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:retain")))
__attribute__((swift_attr("release:release"))) ImportWithCtor {
int value = 0;
int param1 = 0;
int param2 = 0;

__attribute__((swift_name("init()")))
__attribute__((swift_attr("returns_retained")))
static ImportWithCtor * _Nonnull create() {
return new ImportWithCtor{1};
}

__attribute__((swift_name("init(_:)")))
__attribute__((swift_attr("returns_retained")))
static ImportWithCtor * _Nonnull create(int x) {
return new ImportWithCtor{1, x};
}

__attribute__((swift_name("init(_:_:)")))
__attribute__((swift_attr("returns_retained")))
static ImportWithCtor * _Nonnull create(int x, int y) {
return new ImportWithCtor{1, x, y};
}

__attribute__((swift_name("init(_:_:_:)")))
__attribute__((swift_attr("returns_unretained")))
static ImportWithCtor * _Nonnull create(int x, int y, int z) {
return new ImportWithCtor{0, x, y};
}
};

inline void retain(ImportWithCtor * _Nonnull x) {
x->value++;
}

inline void release(ImportWithCtor * _Nonnull x) {
if (!--x->value)
delete x;
}

class Value {
public:
__attribute__((swift_name("init(x:)")))
static Value forFoo(int x) {
Value ret;
ret.x = x;
return ret;
}
int getX() const { return x; }
private:
int x;
};

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_CONSTRUCTORS_H
20 changes: 19 additions & 1 deletion test/Interop/Cxx/class/constructors-executable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test

Expand Down Expand Up @@ -70,4 +70,22 @@ CxxConstructorTestSuite.test("MoveConstructorWithOneParamWithDefaultArg") {
expectTrue(instance2.value + instance3.value >= 10)
}

CxxConstructorTestSuite.test("ImportStaticFactoryAsInitializer") {
let x = ImportWithCtor()
expectEqual(x.param1, 0)
expectEqual(x.param2, 0)
let y = x
let z = ImportWithCtor(1)
expectEqual(z.param1, 1)
expectEqual(z.param2, 0)
let z2 = ImportWithCtor(2, 3)
expectEqual(z2.param1, 2)
expectEqual(z2.param2, 3)
let z3 = ImportWithCtor(2, 3, 4)
expectEqual(z3.param1, 2)
expectEqual(z3.param2, 3)
let v = Value(x: 2)
expectEqual(v.getX(), 2)
}

runAllTests()
42 changes: 0 additions & 42 deletions test/Interop/Cxx/foreign-reference/Inputs/constructor.h

This file was deleted.

25 changes: 0 additions & 25 deletions test/Interop/Cxx/foreign-reference/constructor-execution.swift

This file was deleted.