Skip to content

Commit f85d90a

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

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
@@ -1280,6 +1280,20 @@ owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is
12801280
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
12811281
The C++ caller of this function is responsible for releasing the object.
12821282

1283+
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.
1284+
1285+
```swift
1286+
var obj = SharedObject.create()
1287+
receiveSharedObject(obj) // Swift gaurantees that obj is alive
1288+
```
1289+
1290+
```c++
1291+
void receiveSharedObject(SharedObject *sobj) {
1292+
...
1293+
// Swift assumes that sobj is valid, non-null object at the end of this function
1294+
}
1295+
```
1296+
12831297
### Unsafe Reference Types
12841298
12851299
The `SWIFT_UNSAFE_REFERENCE` annotation macro has the same effect as `SWIFT_IMMORTAL_REFERENCE`

0 commit comments

Comments
 (0)