-
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
Conversation
afaec26
to
20005bb
Compare
@swift-ci please smoke test |
include/swift/Basic/Features.def
Outdated
/// without being explicitly annotated with either SWIFT_RETURNS_RETAINED | ||
/// or SWIFT_RETURNS_UNRETAINED. | ||
EXPERIMENTAL_FEATURE(WarnUnannotatedReturnOfCxxFrt, true) | ||
EXPERIMENTAL_FEATURE(SuppressWarnUnannotatedReturnOfCxxFrt, true) |
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.
Instead of inverting the feature flag, should we enable the experimental feature by default with a way to opt out? That might be more inline with what happens to other experimental features when they become language features.
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 am now making WarnUnannotatedReturnOfCxxFrt
from an EXPERIMENTAL_FEATURE
to a BASELINE_LANGUAGE_FEATURE
. Users can opt-out of these warnings by using the CxxForeignReferenceType
diagnostic group via the -Werror/-Wwarning
flags. Does this approach seem reasonable?
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 think that would make sense to me, yes!
…d add it to a diagnostic group This change makes the warning for unannotated C++ functions returning foreign reference types (FRT) enabled by default, improving memory safety for Swift/C++ interop users. Also added CxxForeignReferenceType diagnostic group for better control
20005bb
to
31d7816
Compare
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 comment
The 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 #if hasFeature(WarnUnannotatedReturnOfCxxFrt)
or opt in to it
GROUP(AvailabilityUnrecognizedName, "availability-unrecognized-name") | ||
GROUP(ClangDeclarationImport, "clang-declaration-import") | ||
GROUP(ConformanceIsolation, "conformance-isolation") | ||
GROUP(CxxForeignReferenceType, "cxx-foreign-reference-type") |
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.
@@ -0,0 +1,44 @@ | |||
# C++ Foreign Reference Type warnings (CxxForeignReferenceType) |
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.
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.
|
||
## 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 comment
The 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
typedef struct SWIFT_SHARED_REFERENCE(MyFRT_retain, MyFRT_release) MyFRT;
@@ -0,0 +1,44 @@ | |||
# C++ Foreign Reference Type warnings (CxxForeignReferenceType) | |||
|
|||
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 comment
The 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.
Enabling the warning on unannotated C++ functions returning foreign reference types (
SWIFT_SHARED_REFERENCE
) on by default to improve memory safety when calling such APIs from SwiftChanges
WarnUnannotatedReturnOfCxxFrt
from anEXPERIMENTAL_FEATURE
to aBASELINE_LANGUAGE_FEATURE
CxxForeignReferenceType
for granular control via-Werror
/-Wwarning
userdocs/diagnostics/cxx-foreign-reference-type.md