Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fahadnayyar
Copy link

Documenting the usage of SWIFT_RETUNRS_(UN)RETAINED annotations to specify the ownership convention of the returned SWIFT_SHARED_REFERENCE types from C++ functions and methods

rdar://116817243

@fahadnayyar fahadnayyar marked this pull request as ready for review June 10, 2025 13:58
@fahadnayyar fahadnayyar force-pushed the cxx-frt-returns-unretained-docs branch from 3d5f9dd to 08c62b4 Compare June 11, 2025 04:28
Copy link
Contributor

@Xazax-hun Xazax-hun left a 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:
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

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.


Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants