Skip to content

[cxx-interop] Add new tests for consuming shared foreign reference types #74813

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
Sep 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ __attribute__((swift_attr("release:releaseShared")));
inline void retainShared(SharedFRT *r) { puts("retainShared"); }
inline void releaseShared(SharedFRT *r) { puts("releaseShared"); }

inline SharedFRT* createSharedFRT() { return new SharedFRT(); }

//--- module.modulemap
module CxxTest {
header "header.h"
Expand All @@ -89,20 +91,38 @@ public func consumeImmortalFRT(_ x: consuming ImmortalFRT) {
print("immortal frt x \(x.x)")
}

public
func consumeSharedFRT(_ x : consuming SharedFRT) {
public func consumeSharedFRT(_ x : consuming SharedFRT) {
print("consume shared frt x \(x.x)")
}

public
func takeSharedFRT(_ x : SharedFRT) { print("take shared frt x \(x.x)") }
public func takeSharedFRT(_ x : SharedFRT) {
print("take shared frt x \(x.x)")
}

public func genericConsumingFunc<T>(_ p: consuming T) {
print("generic consuming function")
}

public
func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
public func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
print("return shared frt x \(x.x)")
return x
}

public func returnSharedFRT2() -> SharedFRT {
return createSharedFRT()
}

public struct ValueWrapper {
let sharedFRT: SharedFRT
public init(_ x: SharedFRT) {
self.sharedFRT = x
}
}

public func consumeValueWrapper(_ x: consuming ValueWrapper) {
print("return shared frt x \(x.sharedFRT.x)")
}

//--- use-swift-cxx-types.cpp

#include "header.h"
Expand Down Expand Up @@ -160,6 +180,31 @@ int main() {
SharedFRT *sfrtptr = UseCxx::returnSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: return shared frt x 2
SharedFRT *sfrtptr2 = UseCxx::returnSharedFRT2();
// No retain or release here.
}
{
SharedFRT sfrt;
sfrt.x = 4;
auto wrapper = UseCxx::ValueWrapper::init(&sfrt);
// consumeValueWrapper creates a defensive copy in the thunk.
UseCxx::consumeValueWrapper(wrapper);
// CHECK-NEXT: retainShared
// CHECK-NEXT: retainShared
// CHECK-NEXT: releaseShared
// CHECK-NEXT: return shared frt x 4
// CHECK-NEXT: releaseShared
}
{
SharedFRT sfrt;
sfrt.x = 4;
auto wrapper = UseCxx::ValueWrapper::init(&sfrt);
UseCxx::genericConsumingFunc(wrapper);
// CHECK-NEXT: retainShared
// CHECK-NEXT: retainShared
// CHECK-DAG: releaseShared
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines are swapped on linux and windows, this is why I use CHECK-DAG.

// CHECK-DAG: generic consuming function
// CHECK-NEXT: releaseShared
}
puts("EndOfTest");
// CHECK-NEXT: EndOfTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o
// RUN: not %target-interop-build-swift %t/use-cxx-types.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseCxx -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O -Xfrontend -disable-availability-checking 2> %t/out

// RUN: cat %t/out | %FileCheck %s

//--- header.h

class SharedFRT {
public:
int x;
} __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:retainShared")))
__attribute__((swift_attr("release:releaseShared")));

inline void retainShared(SharedFRT *r) { }
inline void releaseShared(SharedFRT *r) { }

//--- use-swift-cxx-types.cpp
#include "header.h"

int main() {}

//--- module.modulemap
module CxxTest {
header "header.h"
requires cplusplus
}

//--- use-cxx-types.swift
import CxxTest

public func consumeSharedFRT(_ x: consuming SharedFRT) {}
public func takeSharedFRT(_ x: SharedFRT) {
consumeSharedFRT(consume x)
// CHECK: error: 'consume' applied to value that the compiler does not support. This is a compiler bug. Please file a bug with a small example of the bug
x.x = 10
}