From 69f0a7d75604801e0e655ca8d2d5db5b8f19f7a6 Mon Sep 17 00:00:00 2001 From: Gabor Horvath Date: Fri, 17 Oct 2025 13:08:26 +0100 Subject: [PATCH] [cxx-interop] Do not treat SWIFT_RETURNS_INDEPENDENT_VALUE unsafe The explicit safety analysis inadvertently considered every instance method with this annotation unsafe. We trust these annotations, so let's not consider the annotated methods unsafe. rdar://162602614 --- lib/ClangImporter/ClangImporter.cpp | 2 +- test/Interop/Cxx/class/safe-interop-mode.swift | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 43ea6cce93991..068c5c58b4b8f 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8782,7 +8782,7 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator, // Explicitly unsafe. auto decl = desc.decl; - if (hasUnsafeAPIAttr(decl) || hasSwiftAttribute(decl, "unsafe")) + if (hasSwiftAttribute(decl, "unsafe")) return ExplicitSafety::Unsafe; // Explicitly safe. diff --git a/test/Interop/Cxx/class/safe-interop-mode.swift b/test/Interop/Cxx/class/safe-interop-mode.swift index 7f36ca1c22c30..ad5d2c9f249b7 100644 --- a/test/Interop/Cxx/class/safe-interop-mode.swift +++ b/test/Interop/Cxx/class/safe-interop-mode.swift @@ -93,6 +93,12 @@ using TTakePtr = TTake; using TTakeSafeTuple = TTake; using TTakeUnsafeTuple = TTake; +struct HoldsShared { + SharedObject* obj; + + SharedObject* getObj() const SWIFT_RETURNS_INDEPENDENT_VALUE; +}; + //--- test.swift import Test @@ -183,8 +189,9 @@ func useSharedReference(frt: SharedObject, x: OwnedData) { } @available(SwiftStdlib 5.8, *) -func useSharedReference(frt: DerivedFromSharedObject) { +func useSharedReference(frt: DerivedFromSharedObject, h: HoldsShared) { let _ = frt + let _ = h.getObj() } func useTTakeInt(x: TTakeInt) {