-
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?
[cxx-interop] Add SWIFT_RETUNRS_(UN)RETAINED discussions to C++ interop docs #1077
Conversation
3d5f9dd
to
08c62b4
Compare
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.
I had some comments inline. cc @egorzhdan just in case he also wants to take a look.
@@ -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: |
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.)
|
||
let unowned = makeUnownedObject() | ||
unowned.doSomething() | ||
// make sure `unowned` remains valid while in use |
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.
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.
// make sure `unowned` remains valid while in use | ||
``` | ||
|
||
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 comment
The 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++?
|
||
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 comment
The 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.
Documenting the usage of
SWIFT_RETUNRS_(UN)RETAINED
annotations to specify the ownership convention of the returnedSWIFT_SHARED_REFERENCE
types from C++ functions and methodsrdar://116817243