Skip to content

Commit 7698cd0

Browse files
author
Gabor Horvath
committed
[cxx-interop] Add new tests for consuming shared foreign reference types
Check semantics for functions creating FRTs, and wrapping FRTs.
1 parent 1c1b6f7 commit 7698cd0

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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

Lines changed: 36 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,34 @@ 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+
}
99101

100-
public
101-
func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
102+
public func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
102103
print("return shared frt x \(x.x)")
103104
return x
104105
}
105106

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

108124
#include "header.h"
@@ -160,6 +176,20 @@ int main() {
160176
SharedFRT *sfrtptr = UseCxx::returnSharedFRT(&sfrt);
161177
// CHECK-NEXT: retainShared
162178
// CHECK-NEXT: return shared frt x 2
179+
SharedFRT *sfrtptr2 = UseCxx::returnSharedFRT2();
180+
// No retain or release here.
181+
}
182+
{
183+
SharedFRT sfrt;
184+
sfrt.x = 4;
185+
auto wrapper = UseCxx::ValueWrapper::init(&sfrt);
186+
// consumeValueWrapper creates a defensive copy in the thunk.
187+
UseCxx::consumeValueWrapper(wrapper);
188+
// CHECK-NEXT: retainShared
189+
// CHECK-NEXT: retainShared
190+
// CHECK-NEXT: releaseShared
191+
// CHECK-NEXT: return shared frt x 4
192+
// CHECK-NEXT: releaseShared
163193
}
164194
puts("EndOfTest");
165195
// CHECK-NEXT: EndOfTest

0 commit comments

Comments
 (0)