Skip to content

Commit 6ae54fe

Browse files
authored
Merge pull request #74813 from swiftlang/gaborh/consuming-tests
[cxx-interop] Add new tests for consuming shared foreign reference types
2 parents ba585fd + bfc4e50 commit 6ae54fe

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

test/Interop/CxxToSwiftToCxx/consuming-cxx-struct-parameter-back-to-cxx-execution.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ __attribute__((swift_attr("release:releaseShared")));
6363
inline void retainShared(SharedFRT *r) { puts("retainShared"); }
6464
inline void releaseShared(SharedFRT *r) { puts("releaseShared"); }
6565

66+
inline SharedFRT* createSharedFRT() { return new SharedFRT(); }
67+
6668
//--- module.modulemap
6769
module CxxTest {
6870
header "header.h"
@@ -89,20 +91,38 @@ public func consumeImmortalFRT(_ x: consuming ImmortalFRT) {
8991
print("immortal frt x \(x.x)")
9092
}
9193

92-
public
93-
func consumeSharedFRT(_ x : consuming SharedFRT) {
94+
public func consumeSharedFRT(_ x : consuming SharedFRT) {
9495
print("consume shared frt x \(x.x)")
9596
}
9697

97-
public
98-
func takeSharedFRT(_ x : SharedFRT) { print("take shared frt x \(x.x)") }
98+
public func takeSharedFRT(_ x : SharedFRT) {
99+
print("take shared frt x \(x.x)")
100+
}
101+
102+
public func genericConsumingFunc<T>(_ p: consuming T) {
103+
print("generic consuming function")
104+
}
99105

100-
public
101-
func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
106+
public func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
102107
print("return shared frt x \(x.x)")
103108
return x
104109
}
105110

111+
public func returnSharedFRT2() -> SharedFRT {
112+
return createSharedFRT()
113+
}
114+
115+
public struct ValueWrapper {
116+
let sharedFRT: SharedFRT
117+
public init(_ x: SharedFRT) {
118+
self.sharedFRT = x
119+
}
120+
}
121+
122+
public func consumeValueWrapper(_ x: consuming ValueWrapper) {
123+
print("return shared frt x \(x.sharedFRT.x)")
124+
}
125+
106126
//--- use-swift-cxx-types.cpp
107127

108128
#include "header.h"
@@ -160,6 +180,31 @@ int main() {
160180
SharedFRT *sfrtptr = UseCxx::returnSharedFRT(&sfrt);
161181
// CHECK-NEXT: retainShared
162182
// CHECK-NEXT: return shared frt x 2
183+
SharedFRT *sfrtptr2 = UseCxx::returnSharedFRT2();
184+
// No retain or release here.
185+
}
186+
{
187+
SharedFRT sfrt;
188+
sfrt.x = 4;
189+
auto wrapper = UseCxx::ValueWrapper::init(&sfrt);
190+
// consumeValueWrapper creates a defensive copy in the thunk.
191+
UseCxx::consumeValueWrapper(wrapper);
192+
// CHECK-NEXT: retainShared
193+
// CHECK-NEXT: retainShared
194+
// CHECK-NEXT: releaseShared
195+
// CHECK-NEXT: return shared frt x 4
196+
// CHECK-NEXT: releaseShared
197+
}
198+
{
199+
SharedFRT sfrt;
200+
sfrt.x = 4;
201+
auto wrapper = UseCxx::ValueWrapper::init(&sfrt);
202+
UseCxx::genericConsumingFunc(wrapper);
203+
// CHECK-NEXT: retainShared
204+
// CHECK-NEXT: retainShared
205+
// CHECK-DAG: releaseShared
206+
// CHECK-DAG: generic consuming function
207+
// CHECK-NEXT: releaseShared
163208
}
164209
puts("EndOfTest");
165210
// CHECK-NEXT: EndOfTest
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o
5+
// 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
6+
7+
// RUN: cat %t/out | %FileCheck %s
8+
9+
//--- header.h
10+
11+
class SharedFRT {
12+
public:
13+
int x;
14+
} __attribute__((swift_attr("import_reference")))
15+
__attribute__((swift_attr("retain:retainShared")))
16+
__attribute__((swift_attr("release:releaseShared")));
17+
18+
inline void retainShared(SharedFRT *r) { }
19+
inline void releaseShared(SharedFRT *r) { }
20+
21+
//--- use-swift-cxx-types.cpp
22+
#include "header.h"
23+
24+
int main() {}
25+
26+
//--- module.modulemap
27+
module CxxTest {
28+
header "header.h"
29+
requires cplusplus
30+
}
31+
32+
//--- use-cxx-types.swift
33+
import CxxTest
34+
35+
public func consumeSharedFRT(_ x: consuming SharedFRT) {}
36+
public func takeSharedFRT(_ x: SharedFRT) {
37+
consumeSharedFRT(consume x)
38+
// 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
39+
x.x = 10
40+
}

0 commit comments

Comments
 (0)