-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[cxx-interop] Enabling WarnUnannotatedReturnOfCxxFrt on by default and add it to a diagnostic group #84759
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] Enabling WarnUnannotatedReturnOfCxxFrt on by default and add it to a diagnostic group #84759
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 |
---|---|---|
|
@@ -213,6 +213,7 @@ BASELINE_LANGUAGE_FEATURE(BuiltinUnprotectedStackAlloc, 0, "Builtin.unprotectedS | |
BASELINE_LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline") | ||
BASELINE_LANGUAGE_FEATURE(BuiltinUnprotectedAddressOf, 0, "Builtin.unprotectedAddressOf") | ||
BASELINE_LANGUAGE_FEATURE(NewCxxMethodSafetyHeuristics, 0, "Only import C++ methods that return pointers (projections) on owned types as unsafe") | ||
BASELINE_LANGUAGE_FEATURE(WarnUnannotatedReturnOfCxxFrt, 0, "Warn about unannotated C++ functions returning foreign 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. I don't think we need a feature for this at all, because nobody needs to do |
||
BASELINE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability") | ||
BASELINE_LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment") | ||
BASELINE_LANGUAGE_FEATURE(BuiltinCreateTaskGroupWithFlags, 0, "Builtin.createTaskGroupWithFlags") | ||
|
@@ -502,10 +503,6 @@ EXPERIMENTAL_FEATURE(ImportNonPublicCxxMembers, true) | |
/// types and importing them as Swift initializers. | ||
EXPERIMENTAL_FEATURE(SuppressCXXForeignReferenceTypeInitializers, true) | ||
|
||
/// Emit a warning when a C++ API returns a SWIFT_SHARED_REFERENCE type | ||
/// without being explicitly annotated with either SWIFT_RETURNS_RETAINED | ||
/// or SWIFT_RETURNS_UNRETAINED. | ||
EXPERIMENTAL_FEATURE(WarnUnannotatedReturnOfCxxFrt, true) | ||
|
||
/// modify/read single-yield coroutines | ||
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CoroutineAccessors, true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# C++ Foreign Reference Type warnings (CxxForeignReferenceType) | ||
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. As I noted earlier, SWIFT_SHARED_REFERENCE isn't a C++-specific feature. There are C types that get imported as shared references, too. Please remove the C++-specific bits; generally speaking, replacing C++ with C in this markdown file looks sufficient. |
||
|
||
Warnings related to C++ APIs returning foreign reference types that lack proper ownership annotations. | ||
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. Thank you for the descriptive documentation group. It's very helpful to our users. |
||
|
||
## Overview | ||
|
||
When importing C++ types marked with `SWIFT_SHARED_REFERENCE` into Swift, the compiler needs to understand the ownership semantics of functions returning these types. Without explicit annotations, the compiler cannot determine whether the returned object should be retained or not, which can lead to memory management issues. | ||
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 worth saying that these types are imported as classes in Swift, and showing an example declaration like
|
||
|
||
## The Warning | ||
|
||
The compiler emits a warning when it encounters a C++ function or Objective-C method that: | ||
1. Returns a type marked with `SWIFT_SHARED_REFERENCE` (foreign reference type) | ||
2. Lacks either `SWIFT_RETURNS_RETAINED` or `SWIFT_RETURNS_UNRETAINED` annotation | ||
|
||
Example warning: | ||
``` | ||
warning: cannot infer the ownership of the returned value, annotate 'functionName()' with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED | ||
``` | ||
|
||
## How to Fix | ||
|
||
### For C++ Functions | ||
|
||
Add the appropriate annotation to your C++ function declaration: | ||
|
||
```cpp | ||
// If the function returns a retained value | ||
SWIFT_RETURNS_RETAINED MyFRT* createObject(); | ||
|
||
// If the function returns an unretained value | ||
SWIFT_RETURNS_UNRETAINED MyFRT* getExistingObject(); | ||
``` | ||
|
||
### For Objective-C Methods | ||
|
||
Similarly, annotate your Objective-C methods: | ||
|
||
```objc | ||
// Retained value | ||
- (MyFRT *)createObject SWIFT_RETURNS_RETAINED; | ||
|
||
// Unretained value | ||
- (MyFRT *)getExistingObject SWIFT_RETURNS_UNRETAINED; | ||
``` |
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.
Please remove the "cxx" from these. It's not a C++-specific feature.