Skip to content

Commit ed1c605

Browse files
committed
[cxx-interop] add a testcase to verify that 'borrowing' works with copyable C++ types
This lets us verify that our suggested pattern for calling unsafe functions is working as intended in Swift This also lets us verify that we can still pass borrowed C++ types to C++ functions that take in a const ref parameter Tests now fixed swiftlang#61454 (cherry picked from commit da8c52c)
1 parent 2cf1917 commit ed1c605

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: %target-build-swift %t/test.swift -I %t -o %t/out -Xfrontend -enable-experimental-cxx-interop -O
4+
// RUN: %target-codesign %t/out
5+
// RUN: %target-run %t/out
6+
7+
// Verify that a non-const ref value parameter can't implicitly receive
8+
// aborrowed value.
9+
// RUN: %target-swift-frontend -DBORROW_PASS_TO_VALUE_PARAM -emit-ir -o /dev/null -I %t %t/test.swift -enable-experimental-cxx-interop -verify
10+
11+
//--- Inputs/module.modulemap
12+
module CxxTest {
13+
header "test.h"
14+
requires cplusplus
15+
}
16+
17+
//--- Inputs/test.h
18+
19+
inline int &getCopyCounter() {
20+
static int value = 0;
21+
return value;
22+
}
23+
24+
class BorrowMe {
25+
public:
26+
BorrowMe(): x_(11) {}
27+
BorrowMe(const BorrowMe &) {
28+
++getCopyCounter();
29+
}
30+
31+
const int &x() const { return x_; }
32+
int &x() { return x_; }
33+
private:
34+
int x_;
35+
};
36+
37+
inline int takeBorrowConstRef(const BorrowMe &value) {
38+
return value.x();
39+
}
40+
41+
inline int takeBorrowByVal(BorrowMe value) {
42+
return value.x();
43+
}
44+
45+
//--- test.swift
46+
47+
import CxxTest
48+
49+
extension BorrowMe {
50+
borrowing func getX() -> CInt {
51+
__xUnsafe().pointee
52+
}
53+
54+
var x: CInt {
55+
getX()
56+
}
57+
}
58+
59+
func testBorrowingParam(_ value: borrowing BorrowMe) {
60+
let x = takeBorrowConstRef(value)
61+
assert(x == 11)
62+
#if BORROW_PASS_TO_VALUE_PARAM
63+
takeBorrowByVal(value) // expected-error@-4 {{'value' is borrowed and cannot be consumed}} expected-note {{consumed here}}
64+
takeBorrowByVal(copy value) // ok
65+
#endif
66+
}
67+
68+
public func testBorrowingSafeReferenceUse() {
69+
let x: CInt
70+
do {
71+
let p = BorrowMe()
72+
x = p.x
73+
testBorrowingParam(p)
74+
}
75+
if x != 11 { fatalError("wrong value") }
76+
assert(getCopyCounter().pointee == 0)
77+
}
78+
79+
testBorrowingSafeReferenceUse()

0 commit comments

Comments
 (0)