-
Notifications
You must be signed in to change notification settings - Fork 235
[cxx-interop] Add SWIFT_RETUNRS_(UN)RETAINED discussions to C++ interop docs #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1255,6 +1255,30 @@ object.doSomething() | |
// `object` will be released here. | ||
``` | ||
|
||
To specify the ownership of returned `SWIFT_SHARED_REFERENCE` types, use the `SWIFT_RETURNS_RETAINED` and `SWIFT_RETURNS_UNRETAINED` annotations on C++ functions and methods. These annotations tell the Swift compiler whether the type is returned as `+1` (retained) or `+0` (unretained). This is necessary to ensure that appropriate `retain`/`release` operations are inserted at the boundary: | ||
|
||
```c++ | ||
// Returns +1 ownership; Swift will take responsibility for releasing it. | ||
SharedObject* makeOwnedObject() SWIFT_RETURNS_RETAINED; | ||
|
||
// Returns +0 ownership; the caller must ensure the object stays alive. | ||
SharedObject* makeUnownedObject() SWIFT_RETURNS_UNRETAINED; | ||
``` | ||
|
||
These ownership conventions are reflected naturally in Swift: | ||
```swift | ||
let owned = makeOwnedObject() | ||
owned.doSomething() | ||
// `owned` is automatically released when it goes out of scope | ||
|
||
let unowned = makeUnownedObject() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: looks like the syntax highlighter treats |
||
unowned.doSomething() | ||
// make sure `unowned` remains valid while in use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't Swift automatically increase reference counting operations to guarantee that unowned is valid? I think we should check with someone if we are unsure before adding this to the documentation. |
||
``` | ||
|
||
These ownership annotations are also supported in Objective-C or Objective-C++ functions that return C++ `SWIFT_SHARED_REFERENCE` types. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for an Objective-C function to return a C++ type? Isn't that already Objective-C++? |
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the default ownership convention documented somewhere? If not, this might be a good place to add. |
||
### Inheritance and Virtual Member Functions | ||
|
||
Similar to value types, casting an instance of a derived reference type to a | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would these annotations work on C, and Objective-C code? If yes, maybe we should not emphasize C++ here (also this is the C++ interop documentation so this might be redundant information anyway.)