File tree 1 file changed +79
-0
lines changed 1 file changed +79
-0
lines changed Original file line number Diff line number Diff line change
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 ( )
You can’t perform that action at this time.
0 commit comments