Skip to content

Conversation

fahadnayyar
Copy link
Contributor

@fahadnayyar fahadnayyar commented Oct 8, 2025

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 Swift

Changes

  • Made WarnUnannotatedReturnOfCxxFrt from an EXPERIMENTAL_FEATURE to a BASELINE_LANGUAGE_FEATURE
  • Added diagnostic group: CxxForeignReferenceType for granular control via -Werror/-Wwarning
  • Added documentation: userdocs/diagnostics/cxx-foreign-reference-type.md

@fahadnayyar fahadnayyar force-pushed the fnayyar/misl-diag-group-default-on branch 3 times, most recently from afaec26 to 20005bb Compare October 8, 2025 21:03
@fahadnayyar fahadnayyar self-assigned this Oct 9, 2025
@fahadnayyar fahadnayyar added the c++ interop Feature: Interoperability with C++ label Oct 9, 2025
@fahadnayyar fahadnayyar marked this pull request as ready for review October 9, 2025 03:10
@fahadnayyar
Copy link
Contributor Author

@swift-ci please smoke test

/// without being explicitly annotated with either SWIFT_RETURNS_RETAINED
/// or SWIFT_RETURNS_UNRETAINED.
EXPERIMENTAL_FEATURE(WarnUnannotatedReturnOfCxxFrt, true)
EXPERIMENTAL_FEATURE(SuppressWarnUnannotatedReturnOfCxxFrt, true)
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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
@fahadnayyar fahadnayyar force-pushed the fnayyar/misl-diag-group-default-on branch from 20005bb to 31d7816 Compare October 10, 2025 04:21
@fahadnayyar fahadnayyar changed the title [cxx-interop] Convert WarnUnannotatedReturnOfCxxFrt from opt-in to opt-out and add diagnostic group [cxx-interop] Enabling WarnUnannotatedReturnOfCxxFrt on by default and add it to a diagnostic group Oct 10, 2025
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")
Copy link
Member

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")
Copy link
Member

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)
Copy link
Member

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

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants