Skip to content

Commit facef0e

Browse files
fahadnayyarj-hui
authored andcommitted
[cxx-interop] Enabling WarnUnannotatedReturnOfCxxFrt on by default and 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
1 parent 75f8041 commit facef0e

File tree

11 files changed

+62
-24
lines changed

11 files changed

+62
-24
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ GROUP(AlwaysAvailableDomain, "always-available-domain")
4545
GROUP(AvailabilityUnrecognizedName, "availability-unrecognized-name")
4646
GROUP(ClangDeclarationImport, "clang-declaration-import")
4747
GROUP(ConformanceIsolation, "conformance-isolation")
48+
GROUP(CxxForeignReferenceType, "cxx-foreign-reference-type")
4849
GROUP(DeprecatedDeclaration, "deprecated-declaration")
4950
GROUP(DynamicCallable, "dynamic-callable-requirements")
5051
GROUP(EmbeddedRestrictions, "embedded-restrictions")

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ ERROR(expose_nested_type_to_cxx,none,
22112211
"nested %kind0 can not yet be represented in C++", (ValueDecl *))
22122212
ERROR(expose_macro_to_cxx,none,
22132213
"Swift macro can not yet be represented in C++", (ValueDecl *))
2214-
WARNING(warn_unannotated_cxx_func_returning_frt, none,
2214+
GROUPED_WARNING(warn_unannotated_cxx_func_returning_frt, CxxForeignReferenceType, none,
22152215
"cannot infer the ownership of the returned value, annotate %0 with "
22162216
"either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED",
22172217
(const ValueDecl *))

include/swift/Basic/Features.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ BASELINE_LANGUAGE_FEATURE(BuiltinUnprotectedStackAlloc, 0, "Builtin.unprotectedS
213213
BASELINE_LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline")
214214
BASELINE_LANGUAGE_FEATURE(BuiltinUnprotectedAddressOf, 0, "Builtin.unprotectedAddressOf")
215215
BASELINE_LANGUAGE_FEATURE(NewCxxMethodSafetyHeuristics, 0, "Only import C++ methods that return pointers (projections) on owned types as unsafe")
216+
BASELINE_LANGUAGE_FEATURE(WarnUnannotatedReturnOfCxxFrt, 0, "Warn about unannotated C++ functions returning foreign reference types")
216217
BASELINE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability")
217218
BASELINE_LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment")
218219
BASELINE_LANGUAGE_FEATURE(BuiltinCreateTaskGroupWithFlags, 0, "Builtin.createTaskGroupWithFlags")
@@ -498,10 +499,9 @@ EXPERIMENTAL_FEATURE(AssumeResilientCxxTypes, true)
498499
/// Import inherited non-public members when importing C++ classes.
499500
EXPERIMENTAL_FEATURE(ImportNonPublicCxxMembers, true)
500501

501-
/// Emit a warning when a C++ API returns a SWIFT_SHARED_REFERENCE type
502-
/// without being explicitly annotated with either SWIFT_RETURNS_RETAINED
503-
/// or SWIFT_RETURNS_UNRETAINED.
504-
EXPERIMENTAL_FEATURE(WarnUnannotatedReturnOfCxxFrt, true)
502+
/// Suppress the synthesis of static factory methods for C++ foreign reference
503+
/// types and importing them as Swift initializers.
504+
EXPERIMENTAL_FEATURE(SuppressCXXForeignReferenceTypeInitializers, true)
505505

506506
/// modify/read single-yield coroutines
507507
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CoroutineAccessors, true)

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ UNINTERESTING_FEATURE(LibraryEvolution)
332332
UNINTERESTING_FEATURE(SafeInteropWrappers)
333333
UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
334334
UNINTERESTING_FEATURE(ImportNonPublicCxxMembers)
335-
UNINTERESTING_FEATURE(WarnUnannotatedReturnOfCxxFrt)
335+
UNINTERESTING_FEATURE(SuppressCXXForeignReferenceTypeInitializers)
336336
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)
337337
UNINTERESTING_FEATURE(AllowRuntimeSymbolDeclarations)
338338

lib/Sema/MiscDiagnostics.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6375,8 +6375,6 @@ static bool isReturningFRT(const clang::NamedDecl *ND,
63756375
static bool shouldDiagnoseMissingReturnsRetained(const clang::NamedDecl *ND,
63766376
clang::QualType retType,
63776377
ASTContext &Ctx) {
6378-
if (!Ctx.LangOpts.hasFeature(Feature::WarnUnannotatedReturnOfCxxFrt))
6379-
return false;
63806378

63816379
auto attrInfo = importer::ReturnOwnershipInfo(ND);
63826380
if (attrInfo.hasRetainAttr())

test/Interop/Cxx/foreign-reference/frt-reference-returns-diagnostics.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: rm -rf %t
2-
// RUN: %target-typecheck-verify-swift -I %S%{fs-sep}Inputs -cxx-interoperability-mode=default -enable-experimental-feature WarnUnannotatedReturnOfCxxFrt -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}frt-reference-returns.h
3-
4-
// REQUIRES: swift_feature_WarnUnannotatedReturnOfCxxFrt
2+
// RUN: %target-typecheck-verify-swift -I %S%{fs-sep}Inputs -cxx-interoperability-mode=default -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}frt-reference-returns.h
53

64
import FRTReferenceReturns
75

test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-error.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// RUN: rm -rf %t
2-
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature WarnUnannotatedReturnOfCxxFrt -verify-additional-file %S/Inputs/cxx-functions-and-methods-returning-frt.h -Xcc -Wno-return-type -Xcc -Wno-nullability-completeness
2+
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=upcoming-swift -verify-additional-file %S/Inputs/cxx-functions-and-methods-returning-frt.h -Xcc -Wno-return-type -Xcc -Wno-nullability-completeness
33

44
// XFAIL: OS=windows-msvc
55
// TODO: Enable this on windows when -verify-additional-file issue on Windows Swift CI is resolved
66

7-
// REQUIRES: swift_feature_WarnUnannotatedReturnOfCxxFrt
87

98
import FunctionsAndMethodsReturningFRT
109
import CxxStdlib

test/Interop/Cxx/foreign-reference/inheritance-diagnostics.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: rm -rf %t
2-
// RUN: %target-swift-frontend -typecheck -verify -I %S%{fs-sep}Inputs %s -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature WarnUnannotatedReturnOfCxxFrt -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}inheritance.h -Xcc -Wno-return-type -Xcc -Wno-inaccessible-base
2+
// RUN: %target-swift-frontend -typecheck -verify -I %S%{fs-sep}Inputs %s -cxx-interoperability-mode=upcoming-swift -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}inheritance.h -Xcc -Wno-return-type -Xcc -Wno-inaccessible-base
33

4-
// REQUIRES: swift_feature_WarnUnannotatedReturnOfCxxFrt
54

65
import Inheritance
76

test/Interop/Cxx/objc-correctness/objc-returning-cxx-frt-diagnostics.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: rm -rf %t
2-
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature WarnUnannotatedReturnOfCxxFrt -verify-additional-file %S/Inputs/cxx-frt.h -Xcc -Wno-return-type
2+
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=upcoming-swift -verify-additional-file %S/Inputs/cxx-frt.h -Xcc -Wno-return-type
33

4-
// REQUIRES: swift_feature_WarnUnannotatedReturnOfCxxFrt
54

65
import CxxForeignRef
76

test/Interop/CxxToSwiftToCxx/consuming-cxx-struct-parameter-back-to-cxx-execution.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
33

4-
// RUN: %target-swift-frontend %t/use-cxx-types.swift -module-name UseCxx -typecheck -verify -emit-clang-header-path %t/UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public -disable-availability-checking
4+
// RUN: %target-swift-frontend %t%{fs-sep}use-cxx-types.swift -module-name UseCxx -typecheck -verify -verify-additional-file %t%{fs-sep}header.h -emit-clang-header-path %t%{fs-sep}UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public -disable-availability-checking
55

6-
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o
7-
// RUN: %target-interop-build-swift %t/use-cxx-types.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseCxx -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O -Xfrontend -disable-availability-checking
6+
// RUN: %target-interop-build-clangxx -std=c++20 -c %t%{fs-sep}use-swift-cxx-types.cpp -I %t -o %t%{fs-sep}swift-cxx-execution.o
7+
// RUN: %target-interop-build-swift %t%{fs-sep}use-cxx-types.swift -o %t%{fs-sep}swift-cxx-execution -Xlinker %t%{fs-sep}swift-cxx-execution.o -module-name UseCxx -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O -Xfrontend -disable-availability-checking
88

9-
// RUN: %target-codesign %t/swift-cxx-execution
10-
// RUN: %target-run %t/swift-cxx-execution | %FileCheck %s
9+
// RUN: %target-codesign %t%{fs-sep}swift-cxx-execution
10+
// RUN: %target-run %t%{fs-sep}swift-cxx-execution | %FileCheck %s
1111

1212
// REQUIRES: executable_test
1313

@@ -63,7 +63,7 @@ __attribute__((swift_attr("release:releaseShared")));
6363
inline void retainShared(SharedFRT *r) { puts("retainShared"); }
6464
inline void releaseShared(SharedFRT *r) { puts("releaseShared"); }
6565

66-
inline SharedFRT* createSharedFRT() { return new SharedFRT(); }
66+
inline SharedFRT* createSharedFRT() { return new SharedFRT(); } // expected-note {{'createSharedFRT()' is defined here}}
6767

6868
//--- module.modulemap
6969
module CxxTest {
@@ -109,7 +109,7 @@ public func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
109109
}
110110

111111
public func returnSharedFRT2() -> SharedFRT {
112-
return createSharedFRT()
112+
return createSharedFRT() // expected-warning {{cannot infer the ownership of the returned value, annotate 'createSharedFRT()' with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED}}
113113
}
114114

115115
public struct ValueWrapper {

0 commit comments

Comments
 (0)