[cxx-interop] Add RETURNS_RETAINED annotations to C++ thunks#88597
Open
patrykstefanski wants to merge 1 commit intoswiftlang:mainfrom
Open
[cxx-interop] Add RETURNS_RETAINED annotations to C++ thunks#88597patrykstefanski wants to merge 1 commit intoswiftlang:mainfrom
patrykstefanski wants to merge 1 commit intoswiftlang:mainfrom
Conversation
Swift functions exposed to C++ return +1 (retained) values, but the generated C++ thunks in the -Swift.h header lacked ownership annotations. This prevented Clang's static analyzer from verifying reference counts when C++ code calls into Swift. Add NS_RETURNS_RETAINED, CF_RETURNS_RETAINED, and SWIFT_RETURNS_RETAINED annotations to thunk signatures based on the return type: - NS_RETURNS_RETAINED for ObjC classes (NSString, etc.) and existentials (id) - CF_RETURNS_RETAINED for CF types (CFString, etc.) - SWIFT_RETURNS_RETAINED for foreign reference types rdar://165231653
j-hui
approved these changes
Apr 21, 2026
Contributor
j-hui
left a comment
There was a problem hiding this comment.
This LGTM!
Just wondering, is there ever a need to print a RETURNS_UNRETAINED annotation?
hnrklssn
reviewed
Apr 22, 2026
| // CHECK: #if !defined(CF_RETURNS_RETAINED) | ||
| // CHECK: #if !defined(SWIFT_RETURNS_RETAINED) | ||
|
|
||
| // CHECK: SWIFT_INLINE_THUNK NSString *_Nonnull getName() |
Member
There was a problem hiding this comment.
It looks like these are emitted in alphabetical order. WDYT about flipping the naming scheme so it's swiftClassReturn, swiftClassPassthrough, methodGetNSString etc? That way you can keep them grouped by category, but sort the decls in alphabetical order and place the check comments right next to the decl being checked.
Contributor
Author
There was a problem hiding this comment.
I think that's a great idea! I'll do it
Xazax-hun
approved these changes
Apr 22, 2026
|
|
||
| // A function returning NSString? generates a thunk returning `NSString | ||
| // *_Nullable`. | ||
| Type unwrapped = resultTy->getOptionalObjectType(); |
Contributor
There was a problem hiding this comment.
Nit: could this be simplified via lookThroughSingleOptionalType?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Swift functions exposed to C++ return +1 (retained) values, but the generated C++ thunks in the -Swift.h header lacked ownership annotations. This prevented Clang's static analyzer from verifying reference counts when C++ code calls into Swift.
Add NS_RETURNS_RETAINED, CF_RETURNS_RETAINED, and SWIFT_RETURNS_RETAINED annotations to thunk signatures based on the return type:
rdar://165231653