Skip to content

Commit 9e29267

Browse files
committed
[cxx-interop] Document guarantees and assumptions for non-const C++ shared references passed to a C++ API from Swift
1 parent 4305ebc commit 9e29267

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

documentation/cxx-interop/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,20 @@ owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is
12711271
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
12721272
The C++ caller of this function is responsible for releasing the object.
12731273

1274+
If a C++ Shared Reference Type is passed as an argument to a C++ API from Swift, the Swift compiler *guarantees* that the passed value would be alive. Also Swift *assumes* that the C++ API is not consuming i.e., it returns with a valid object in the passed reference at the end of the C++ API call.
1275+
1276+
```swift
1277+
var obj = SharedObject.create()
1278+
receiveSharedObject(obj) // Swift gaurantees that obj is alive
1279+
```
1280+
1281+
```c++
1282+
void receiveSharedObject(SharedObject *sobj) {
1283+
...
1284+
// Swift assumes that sobj is valid, non-null object at the end of this function
1285+
}
1286+
```
1287+
12741288
### Unsafe Reference Types
12751289
12761290
The `SWIFT_UNSAFE_REFERENCE` annotation macro has the same effect as `SWIFT_IMMORTAL_REFERENCE`

0 commit comments

Comments
 (0)