Skip to content

[cxx-interop] Re-enable warnings for unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types under an experimental feature flag #82488

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 3 commits into
base: main
Choose a base branch
from

Conversation

fahadnayyar
Copy link
Contributor

@fahadnayyar fahadnayyar commented Jun 25, 2025

This patch re-enables diagnostics for unannotated C++ functions or methods returning SWIFT_SHARED_REFERENCE types. These warnings now fire only once per source location, even in the presence of multiple template instantiations. This avoids diagnostic duplication that was a key source of noise in the compilation of larger codebases.

These warnings were previously disabled starting in Swift 6.2 via PR-#81411 due to concerns around false positives and excessive duplication in projects adopting C++ interop. This patch addresses the duplication issue by adding source-location-based caching, which ensures that a warning is emitted only once per source location, even across template instantiations with different types.

However, the false positive issue remains to be investigated and will be addressed in a follow-up patch. Until that happens, the warnings are gated behind a new experimental feature flag: WarnUnannotatedReturnOfCxxFrt. This feature will be enabled by default only after thorough qualification and testing on large C++ codebases.

rdar://154261051

@fahadnayyar
Copy link
Contributor Author

@swift-ci please smoke test

@fahadnayyar fahadnayyar changed the title Re-enable warnings for unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types under an experimental feature flag [cxx-interop] Re-enable warnings for unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types under an experimental feature flag Jun 25, 2025
@fahadnayyar fahadnayyar self-assigned this Jun 25, 2025
@fahadnayyar fahadnayyar added the c++ interop Feature: Interoperability with C++ label Jun 25, 2025
/// 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, false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the second argument false here? Do we want this flag to be available for production compilers?

clang::SourceLocation spellingLoc;
if (loc.isMacroID()) {
clang::SourceLocation expansionLoc =
sourceMgr.getImmediateExpansionRange(loc).getBegin();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the immediate expansion range work as intended for nested macros?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case do we need this trick? And why wouldn't always using the spelling location work?

@@ -449,4 +449,34 @@ void bRelease(DefaultOwnershipInheritance::BaseTypeNonDefault *v) {};
void dRetain(DefaultOwnershipInheritance::DerivedTypeNonDefault *v) {};
void dRelease(DefaultOwnershipInheritance::DerivedTypeNonDefault *v) {};

namespace SourceLocationCaching {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see testing for the macro case.

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.

2 participants