From e9943bc4119631a4aa82cd61e1469850759af1da Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 14:36:42 -0800 Subject: [PATCH 1/8] Reapply "[Test][C++ Interop] XFAIL some tests." This reverts commit c00544503d744df14086f9e52213fca9065290e7. --- test/Interop/Cxx/class/access/swiftify-private-fileid.swift | 3 +++ test/Interop/Cxx/class/noncopyable-typechecker.swift | 3 +++ test/Interop/Cxx/class/safe-interop-mode-darwin.swift | 5 ++++- test/Interop/Cxx/class/safe-interop-mode.swift | 3 +++ test/Interop/Cxx/operators/member-inline.swift | 3 +++ test/Interop/Cxx/stdlib/foundation-and-std-module.swift | 3 +++ test/Interop/Cxx/stdlib/std-span-transformed-execution.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-any.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-chrono.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-function.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-optional.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-pair.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-set.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-span-typechecker.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-span.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-string-view.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-string-with-opts.swift | 3 +++ test/Interop/Cxx/stdlib/use-std-string.swift | 3 +++ .../Cxx/swiftify-import/import-as-instance-method.swift | 3 +++ test/Interop/Cxx/swiftify-import/span-in-ctor.swift | 2 ++ 20 files changed, 60 insertions(+), 1 deletion(-) diff --git a/test/Interop/Cxx/class/access/swiftify-private-fileid.swift b/test/Interop/Cxx/class/access/swiftify-private-fileid.swift index 8944779e6fc96..955392b021ee0 100644 --- a/test/Interop/Cxx/class/access/swiftify-private-fileid.swift +++ b/test/Interop/Cxx/class/access/swiftify-private-fileid.swift @@ -6,6 +6,9 @@ // REQUIRES: swift_feature_SafeInteropWrappers // REQUIRES: std_span +// rdar://163511959 error: circular reference +// XFAIL: * + //--- Inputs/swiftify-non-public.h #pragma once diff --git a/test/Interop/Cxx/class/noncopyable-typechecker.swift b/test/Interop/Cxx/class/noncopyable-typechecker.swift index 175c86d124d95..dd7a359c52432 100644 --- a/test/Interop/Cxx/class/noncopyable-typechecker.swift +++ b/test/Interop/Cxx/class/noncopyable-typechecker.swift @@ -3,6 +3,9 @@ // RUN: %target-swift-frontend -cxx-interoperability-mode=default -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-ignore-unrelated // RUN: %target-swift-frontend -cxx-interoperability-mode=default -Xcc -std=c++20 -verify-additional-prefix cpp20- -D CPP20 -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-ignore-unrelated +// rdar://163511959 error: circular reference +// XFAIL: * + //--- Inputs/module.modulemap module Test { header "noncopyable.h" diff --git a/test/Interop/Cxx/class/safe-interop-mode-darwin.swift b/test/Interop/Cxx/class/safe-interop-mode-darwin.swift index 6f7de581e863c..5b844e6e715b4 100644 --- a/test/Interop/Cxx/class/safe-interop-mode-darwin.swift +++ b/test/Interop/Cxx/class/safe-interop-mode-darwin.swift @@ -6,6 +6,9 @@ // REQUIRES: swift_feature_LifetimeDependence // REQUIRES: OS=macosx +// rdar://163511959 error: circular reference +// XFAIL: * + //--- Inputs/module.modulemap module Test { header "nonescapable.h" @@ -31,4 +34,4 @@ func simdConsideredSafe(x : MySimd) { func simdVecConsideredSafe(x : VecOfSimd) { let _ = x -} \ No newline at end of file +} diff --git a/test/Interop/Cxx/class/safe-interop-mode.swift b/test/Interop/Cxx/class/safe-interop-mode.swift index f9eb61a94c449..71b79f9ba3c06 100644 --- a/test/Interop/Cxx/class/safe-interop-mode.swift +++ b/test/Interop/Cxx/class/safe-interop-mode.swift @@ -7,6 +7,9 @@ // REQUIRES: swift_feature_LifetimeDependence // REQUIRES: std_span +// rdar://163511959 error: circular reference +// XFAIL: * + //--- Inputs/module.modulemap module Test { header "nonescapable.h" diff --git a/test/Interop/Cxx/operators/member-inline.swift b/test/Interop/Cxx/operators/member-inline.swift index 3212114e22218..37298322621c0 100644 --- a/test/Interop/Cxx/operators/member-inline.swift +++ b/test/Interop/Cxx/operators/member-inline.swift @@ -4,6 +4,9 @@ // // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import MemberInline import StdlibUnittest diff --git a/test/Interop/Cxx/stdlib/foundation-and-std-module.swift b/test/Interop/Cxx/stdlib/foundation-and-std-module.swift index b371dd885623c..00da488f176d4 100644 --- a/test/Interop/Cxx/stdlib/foundation-and-std-module.swift +++ b/test/Interop/Cxx/stdlib/foundation-and-std-module.swift @@ -9,6 +9,9 @@ // RUN: ls -R %/t | %FileCheck %s +// rdar://163511959 error: circular reference +// XFAIL: * + #if canImport(Foundation) import Foundation #endif diff --git a/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift b/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift index 7c04a7293e671..797d6b8703e16 100644 --- a/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift +++ b/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift @@ -9,6 +9,9 @@ // REQUIRES: executable_test // REQUIRES: std_span +// rdar://163511959 error: circular reference +// XFAIL: * + #if !BRIDGING_HEADER import StdSpan #endif diff --git a/test/Interop/Cxx/stdlib/use-std-any.swift b/test/Interop/Cxx/stdlib/use-std-any.swift index 513ee6ccf9b81..18ffedf469491 100644 --- a/test/Interop/Cxx/stdlib/use-std-any.swift +++ b/test/Interop/Cxx/stdlib/use-std-any.swift @@ -8,6 +8,9 @@ // type, which Swift isn't able to instantiate. // UNSUPPORTED: OS=windows-msvc +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import StdAny import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-chrono.swift b/test/Interop/Cxx/stdlib/use-std-chrono.swift index fa6958f949f4b..6ad923bd30919 100644 --- a/test/Interop/Cxx/stdlib/use-std-chrono.swift +++ b/test/Interop/Cxx/stdlib/use-std-chrono.swift @@ -8,6 +8,9 @@ // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-function.swift b/test/Interop/Cxx/stdlib/use-std-function.swift index 4509bc8031d33..10d7e4dd91141 100644 --- a/test/Interop/Cxx/stdlib/use-std-function.swift +++ b/test/Interop/Cxx/stdlib/use-std-function.swift @@ -7,6 +7,9 @@ // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import StdFunction import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-optional.swift b/test/Interop/Cxx/stdlib/use-std-optional.swift index 7f75ae5bf302f..afc0486576757 100644 --- a/test/Interop/Cxx/stdlib/use-std-optional.swift +++ b/test/Interop/Cxx/stdlib/use-std-optional.swift @@ -11,6 +11,9 @@ // rdar://121551667 // XFAIL: OS=freebsd +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import StdOptional import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-pair.swift b/test/Interop/Cxx/stdlib/use-std-pair.swift index 4d6c5ff50e177..fb27f45419788 100644 --- a/test/Interop/Cxx/stdlib/use-std-pair.swift +++ b/test/Interop/Cxx/stdlib/use-std-pair.swift @@ -7,6 +7,9 @@ // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import StdPair import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-set.swift b/test/Interop/Cxx/stdlib/use-std-set.swift index af873659a33fc..6b90e0168c37c 100644 --- a/test/Interop/Cxx/stdlib/use-std-set.swift +++ b/test/Interop/Cxx/stdlib/use-std-set.swift @@ -18,6 +18,9 @@ // REQUIRES: OS=macosx || OS=linux-gnu // UNSUPPORTED: LinuxDistribution=fedora-41 +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest #if !BRIDGING_HEADER import StdSet diff --git a/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift b/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift index 0a77811ba0905..71edf6f471e3d 100644 --- a/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift +++ b/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift @@ -1,6 +1,9 @@ // RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop -Xcc -std=c++20 2>&1 // REQUIRES: std_span +// rdar://163511959 error: circular reference +// XFAIL: * + import StdSpan let arr: [Int32] = [1, 2, 3] diff --git a/test/Interop/Cxx/stdlib/use-std-span.swift b/test/Interop/Cxx/stdlib/use-std-span.swift index c86d3602f236c..1cb5bbcf4094f 100644 --- a/test/Interop/Cxx/stdlib/use-std-span.swift +++ b/test/Interop/Cxx/stdlib/use-std-span.swift @@ -10,6 +10,9 @@ // REQUIRES: executable_test // REQUIRES: std_span +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest #if !BRIDGING_HEADER import StdSpan diff --git a/test/Interop/Cxx/stdlib/use-std-string-view.swift b/test/Interop/Cxx/stdlib/use-std-string-view.swift index 4ae1c18a4c5b8..406602db0d3b6 100644 --- a/test/Interop/Cxx/stdlib/use-std-string-view.swift +++ b/test/Interop/Cxx/stdlib/use-std-string-view.swift @@ -5,6 +5,9 @@ // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import CxxStdlib import StdStringView diff --git a/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift b/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift index 5b06bd3cf1f81..0424d0bfcb6d0 100644 --- a/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift +++ b/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift @@ -4,6 +4,9 @@ // Tests optimizations related to CxxStdlib. +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import CxxStdlib import StdStringAndVector diff --git a/test/Interop/Cxx/stdlib/use-std-string.swift b/test/Interop/Cxx/stdlib/use-std-string.swift index e1b23d3adfb2f..a8c9e5c0092f8 100644 --- a/test/Interop/Cxx/stdlib/use-std-string.swift +++ b/test/Interop/Cxx/stdlib/use-std-string.swift @@ -8,6 +8,9 @@ // // REQUIRES: executable_test +// rdar://163511959 error: circular reference +// XFAIL: * + import StdlibUnittest import CxxStdlib #if USE_CUSTOM_STRING_API diff --git a/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift b/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift index c1779a087ce18..d3d57267b3354 100644 --- a/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift +++ b/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift @@ -7,6 +7,9 @@ // RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -cxx-interoperability-mode=default -Xcc -std=c++20 -verify -verify-additional-file %t/Inputs/instance.h -DVERIFY // RUN: env SWIFT_BACKTRACE="" %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -cxx-interoperability-mode=default -Xcc -std=c++20 -dump-macro-expansions 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace --implicit-check-not __swiftmacro +// rdar://163511959 error: circular reference +// XFAIL: * + //--- test.swift import Instance diff --git a/test/Interop/Cxx/swiftify-import/span-in-ctor.swift b/test/Interop/Cxx/swiftify-import/span-in-ctor.swift index 348258acdfd0f..8fea3776a875f 100644 --- a/test/Interop/Cxx/swiftify-import/span-in-ctor.swift +++ b/test/Interop/Cxx/swiftify-import/span-in-ctor.swift @@ -10,6 +10,8 @@ // CHECK: unsafe self.init(IntSpan(sp)) // CHECK: } +// rdar://163511959 error: circular reference +// XFAIL: * //--- Inputs/module.modulemap module Method { From 80fa8c18ef653775ef2135b8a25a04f99d947f33 Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 14:36:47 -0800 Subject: [PATCH 2/8] Revert "[cxx-interop] Avoid cycles in ClangTypeExplicitSafety requests" This reverts commit d0bad4cebd40cca1395d952b183ee295605aeac5. --- lib/ClangImporter/ClangImporter.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 2a19f3735f554..299b73e8e0a71 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8844,13 +8844,11 @@ CustomRefCountingOperationResult CustomRefCountingOperation::evaluate( /// Check whether the given Clang type involves an unsafe type. static bool hasUnsafeType(Evaluator &evaluator, clang::QualType clangType) { - auto req = ClangTypeExplicitSafety({clangType}); - if (evaluator.hasActiveRequest(req)) - // If there is a cycle in a type, assume ExplicitSafety is Unspecified, - // i.e., not unsafe: - return false; - return evaluateOrDefault(evaluator, req, ExplicitSafety::Unspecified) == - ExplicitSafety::Unsafe; + + auto safety = + evaluateOrDefault(evaluator, ClangTypeExplicitSafety({clangType}), + ExplicitSafety::Unspecified); + return safety == ExplicitSafety::Unsafe; } ExplicitSafety From f98480f1b1f094aea9f0917b0d0bd6b2ec452fbf Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 14:36:59 -0800 Subject: [PATCH 3/8] Revert "[Test][C++ Interop] XFAIL some tests." This reverts commit c672e27bca1f12e44253846e55e0eba448f6f2a5. --- test/Interop/Cxx/class/access/swiftify-private-fileid.swift | 3 --- test/Interop/Cxx/class/noncopyable-typechecker.swift | 3 --- test/Interop/Cxx/class/safe-interop-mode-darwin.swift | 5 +---- test/Interop/Cxx/class/safe-interop-mode.swift | 3 --- test/Interop/Cxx/operators/member-inline.swift | 3 --- test/Interop/Cxx/stdlib/foundation-and-std-module.swift | 3 --- test/Interop/Cxx/stdlib/std-span-transformed-execution.swift | 3 --- test/Interop/Cxx/stdlib/use-std-any.swift | 3 --- test/Interop/Cxx/stdlib/use-std-chrono.swift | 3 --- test/Interop/Cxx/stdlib/use-std-function.swift | 3 --- test/Interop/Cxx/stdlib/use-std-optional.swift | 3 --- test/Interop/Cxx/stdlib/use-std-pair.swift | 3 --- test/Interop/Cxx/stdlib/use-std-set.swift | 3 --- test/Interop/Cxx/stdlib/use-std-span-typechecker.swift | 3 --- test/Interop/Cxx/stdlib/use-std-span.swift | 3 --- test/Interop/Cxx/stdlib/use-std-string-view.swift | 3 --- test/Interop/Cxx/stdlib/use-std-string-with-opts.swift | 3 --- test/Interop/Cxx/stdlib/use-std-string.swift | 3 --- .../Cxx/swiftify-import/import-as-instance-method.swift | 3 --- test/Interop/Cxx/swiftify-import/span-in-ctor.swift | 2 -- 20 files changed, 1 insertion(+), 60 deletions(-) diff --git a/test/Interop/Cxx/class/access/swiftify-private-fileid.swift b/test/Interop/Cxx/class/access/swiftify-private-fileid.swift index 955392b021ee0..8944779e6fc96 100644 --- a/test/Interop/Cxx/class/access/swiftify-private-fileid.swift +++ b/test/Interop/Cxx/class/access/swiftify-private-fileid.swift @@ -6,9 +6,6 @@ // REQUIRES: swift_feature_SafeInteropWrappers // REQUIRES: std_span -// rdar://163511959 error: circular reference -// XFAIL: * - //--- Inputs/swiftify-non-public.h #pragma once diff --git a/test/Interop/Cxx/class/noncopyable-typechecker.swift b/test/Interop/Cxx/class/noncopyable-typechecker.swift index dd7a359c52432..175c86d124d95 100644 --- a/test/Interop/Cxx/class/noncopyable-typechecker.swift +++ b/test/Interop/Cxx/class/noncopyable-typechecker.swift @@ -3,9 +3,6 @@ // RUN: %target-swift-frontend -cxx-interoperability-mode=default -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-ignore-unrelated // RUN: %target-swift-frontend -cxx-interoperability-mode=default -Xcc -std=c++20 -verify-additional-prefix cpp20- -D CPP20 -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-ignore-unrelated -// rdar://163511959 error: circular reference -// XFAIL: * - //--- Inputs/module.modulemap module Test { header "noncopyable.h" diff --git a/test/Interop/Cxx/class/safe-interop-mode-darwin.swift b/test/Interop/Cxx/class/safe-interop-mode-darwin.swift index 5b844e6e715b4..6f7de581e863c 100644 --- a/test/Interop/Cxx/class/safe-interop-mode-darwin.swift +++ b/test/Interop/Cxx/class/safe-interop-mode-darwin.swift @@ -6,9 +6,6 @@ // REQUIRES: swift_feature_LifetimeDependence // REQUIRES: OS=macosx -// rdar://163511959 error: circular reference -// XFAIL: * - //--- Inputs/module.modulemap module Test { header "nonescapable.h" @@ -34,4 +31,4 @@ func simdConsideredSafe(x : MySimd) { func simdVecConsideredSafe(x : VecOfSimd) { let _ = x -} +} \ No newline at end of file diff --git a/test/Interop/Cxx/class/safe-interop-mode.swift b/test/Interop/Cxx/class/safe-interop-mode.swift index 71b79f9ba3c06..f9eb61a94c449 100644 --- a/test/Interop/Cxx/class/safe-interop-mode.swift +++ b/test/Interop/Cxx/class/safe-interop-mode.swift @@ -7,9 +7,6 @@ // REQUIRES: swift_feature_LifetimeDependence // REQUIRES: std_span -// rdar://163511959 error: circular reference -// XFAIL: * - //--- Inputs/module.modulemap module Test { header "nonescapable.h" diff --git a/test/Interop/Cxx/operators/member-inline.swift b/test/Interop/Cxx/operators/member-inline.swift index 37298322621c0..3212114e22218 100644 --- a/test/Interop/Cxx/operators/member-inline.swift +++ b/test/Interop/Cxx/operators/member-inline.swift @@ -4,9 +4,6 @@ // // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import MemberInline import StdlibUnittest diff --git a/test/Interop/Cxx/stdlib/foundation-and-std-module.swift b/test/Interop/Cxx/stdlib/foundation-and-std-module.swift index 00da488f176d4..b371dd885623c 100644 --- a/test/Interop/Cxx/stdlib/foundation-and-std-module.swift +++ b/test/Interop/Cxx/stdlib/foundation-and-std-module.swift @@ -9,9 +9,6 @@ // RUN: ls -R %/t | %FileCheck %s -// rdar://163511959 error: circular reference -// XFAIL: * - #if canImport(Foundation) import Foundation #endif diff --git a/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift b/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift index 797d6b8703e16..7c04a7293e671 100644 --- a/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift +++ b/test/Interop/Cxx/stdlib/std-span-transformed-execution.swift @@ -9,9 +9,6 @@ // REQUIRES: executable_test // REQUIRES: std_span -// rdar://163511959 error: circular reference -// XFAIL: * - #if !BRIDGING_HEADER import StdSpan #endif diff --git a/test/Interop/Cxx/stdlib/use-std-any.swift b/test/Interop/Cxx/stdlib/use-std-any.swift index 18ffedf469491..513ee6ccf9b81 100644 --- a/test/Interop/Cxx/stdlib/use-std-any.swift +++ b/test/Interop/Cxx/stdlib/use-std-any.swift @@ -8,9 +8,6 @@ // type, which Swift isn't able to instantiate. // UNSUPPORTED: OS=windows-msvc -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import StdAny import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-chrono.swift b/test/Interop/Cxx/stdlib/use-std-chrono.swift index 6ad923bd30919..fa6958f949f4b 100644 --- a/test/Interop/Cxx/stdlib/use-std-chrono.swift +++ b/test/Interop/Cxx/stdlib/use-std-chrono.swift @@ -8,9 +8,6 @@ // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-function.swift b/test/Interop/Cxx/stdlib/use-std-function.swift index 10d7e4dd91141..4509bc8031d33 100644 --- a/test/Interop/Cxx/stdlib/use-std-function.swift +++ b/test/Interop/Cxx/stdlib/use-std-function.swift @@ -7,9 +7,6 @@ // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import StdFunction import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-optional.swift b/test/Interop/Cxx/stdlib/use-std-optional.swift index afc0486576757..7f75ae5bf302f 100644 --- a/test/Interop/Cxx/stdlib/use-std-optional.swift +++ b/test/Interop/Cxx/stdlib/use-std-optional.swift @@ -11,9 +11,6 @@ // rdar://121551667 // XFAIL: OS=freebsd -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import StdOptional import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-pair.swift b/test/Interop/Cxx/stdlib/use-std-pair.swift index fb27f45419788..4d6c5ff50e177 100644 --- a/test/Interop/Cxx/stdlib/use-std-pair.swift +++ b/test/Interop/Cxx/stdlib/use-std-pair.swift @@ -7,9 +7,6 @@ // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import StdPair import CxxStdlib diff --git a/test/Interop/Cxx/stdlib/use-std-set.swift b/test/Interop/Cxx/stdlib/use-std-set.swift index 6b90e0168c37c..af873659a33fc 100644 --- a/test/Interop/Cxx/stdlib/use-std-set.swift +++ b/test/Interop/Cxx/stdlib/use-std-set.swift @@ -18,9 +18,6 @@ // REQUIRES: OS=macosx || OS=linux-gnu // UNSUPPORTED: LinuxDistribution=fedora-41 -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest #if !BRIDGING_HEADER import StdSet diff --git a/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift b/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift index 71edf6f471e3d..0a77811ba0905 100644 --- a/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift +++ b/test/Interop/Cxx/stdlib/use-std-span-typechecker.swift @@ -1,9 +1,6 @@ // RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop -Xcc -std=c++20 2>&1 // REQUIRES: std_span -// rdar://163511959 error: circular reference -// XFAIL: * - import StdSpan let arr: [Int32] = [1, 2, 3] diff --git a/test/Interop/Cxx/stdlib/use-std-span.swift b/test/Interop/Cxx/stdlib/use-std-span.swift index 1cb5bbcf4094f..c86d3602f236c 100644 --- a/test/Interop/Cxx/stdlib/use-std-span.swift +++ b/test/Interop/Cxx/stdlib/use-std-span.swift @@ -10,9 +10,6 @@ // REQUIRES: executable_test // REQUIRES: std_span -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest #if !BRIDGING_HEADER import StdSpan diff --git a/test/Interop/Cxx/stdlib/use-std-string-view.swift b/test/Interop/Cxx/stdlib/use-std-string-view.swift index 406602db0d3b6..4ae1c18a4c5b8 100644 --- a/test/Interop/Cxx/stdlib/use-std-string-view.swift +++ b/test/Interop/Cxx/stdlib/use-std-string-view.swift @@ -5,9 +5,6 @@ // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import CxxStdlib import StdStringView diff --git a/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift b/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift index 0424d0bfcb6d0..5b06bd3cf1f81 100644 --- a/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift +++ b/test/Interop/Cxx/stdlib/use-std-string-with-opts.swift @@ -4,9 +4,6 @@ // Tests optimizations related to CxxStdlib. -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import CxxStdlib import StdStringAndVector diff --git a/test/Interop/Cxx/stdlib/use-std-string.swift b/test/Interop/Cxx/stdlib/use-std-string.swift index a8c9e5c0092f8..e1b23d3adfb2f 100644 --- a/test/Interop/Cxx/stdlib/use-std-string.swift +++ b/test/Interop/Cxx/stdlib/use-std-string.swift @@ -8,9 +8,6 @@ // // REQUIRES: executable_test -// rdar://163511959 error: circular reference -// XFAIL: * - import StdlibUnittest import CxxStdlib #if USE_CUSTOM_STRING_API diff --git a/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift b/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift index d3d57267b3354..c1779a087ce18 100644 --- a/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift +++ b/test/Interop/Cxx/swiftify-import/import-as-instance-method.swift @@ -7,9 +7,6 @@ // RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -cxx-interoperability-mode=default -Xcc -std=c++20 -verify -verify-additional-file %t/Inputs/instance.h -DVERIFY // RUN: env SWIFT_BACKTRACE="" %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -cxx-interoperability-mode=default -Xcc -std=c++20 -dump-macro-expansions 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace --implicit-check-not __swiftmacro -// rdar://163511959 error: circular reference -// XFAIL: * - //--- test.swift import Instance diff --git a/test/Interop/Cxx/swiftify-import/span-in-ctor.swift b/test/Interop/Cxx/swiftify-import/span-in-ctor.swift index 8fea3776a875f..348258acdfd0f 100644 --- a/test/Interop/Cxx/swiftify-import/span-in-ctor.swift +++ b/test/Interop/Cxx/swiftify-import/span-in-ctor.swift @@ -10,8 +10,6 @@ // CHECK: unsafe self.init(IntSpan(sp)) // CHECK: } -// rdar://163511959 error: circular reference -// XFAIL: * //--- Inputs/module.modulemap module Method { From a13c95ded63a65101614b28b6dd0106d5b15c43a Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 14:37:03 -0800 Subject: [PATCH 4/8] Revert "[cxx-interop] Avoid cycles in ClangDeclExplicitSafety requests" This reverts commit 929d0d8c2695e89041e4642ee0c5d784c8002039. --- lib/ClangImporter/ClangImporter.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 299b73e8e0a71..ca816cdab36e8 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8759,14 +8759,9 @@ ExplicitSafety ClangTypeExplicitSafety::evaluate( if (auto recordDecl = clangType->getAsTagDecl()) { // If we reached this point the types is not imported as a shared reference, // so we don't need to check the bases whether they are shared references. - auto req = ClangDeclExplicitSafety({recordDecl, false}); - if (evaluator.hasActiveRequest(req)) - // Cycles are allowed in templates, e.g.: - // template class Foo { ... }; // throws away template arg - // template class Bar : Foo> { ... }; - // We need to avoid them here. - return ExplicitSafety::Unspecified; - return evaluateOrDefault(evaluator, req, ExplicitSafety::Unspecified); + return evaluateOrDefault(evaluator, + ClangDeclExplicitSafety({recordDecl, false}), + ExplicitSafety::Unspecified); } // Everything else is safe. From 6607ae9c5647530a7ca5cb9f01de16d0eee45bae Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 14:37:05 -0800 Subject: [PATCH 5/8] Revert "[cxx-interop] Check template argument safety in ClangDeclExplicitSafety" This reverts commit d2632d21afaa60d6df2e29734fe2cda3fbcccf7d. --- lib/ClangImporter/ClangImporter.cpp | 25 +------------- lib/ClangImporter/ImportDecl.cpp | 34 +++++++++++++++++++ .../Interop/Cxx/class/safe-interop-mode.swift | 15 -------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index ca816cdab36e8..a143752419cde 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8881,29 +8881,6 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator, ClangTypeEscapability({recordDecl->getTypeForDecl(), nullptr}), CxxEscapability::Unknown) != CxxEscapability::Unknown) return ExplicitSafety::Safe; - - // A template class is unsafe if any of its type arguments are unsafe. - // Note that this does not rely on the record being defined. - if (const auto *ctsd = - dyn_cast(recordDecl)) { - for (auto arg : ctsd->getTemplateArgs().asArray()) { - switch (arg.getKind()) { - case clang::TemplateArgument::Type: - if (hasUnsafeType(evaluator, arg.getAsType())) - return ExplicitSafety::Unsafe; - break; - case clang::TemplateArgument::Pack: - for (auto pkArg : arg.getPackAsArray()) { - if (pkArg.getKind() == clang::TemplateArgument::Type && - hasUnsafeType(evaluator, pkArg.getAsType())) - return ExplicitSafety::Unsafe; - } - break; - default: - continue; - } - } - } // If we don't have a definition, leave it unspecified. recordDecl = recordDecl->getDefinition(); @@ -8917,7 +8894,7 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator, return ExplicitSafety::Unsafe; } } - + // Check the fields. for (auto field : recordDecl->fields()) { if (hasUnsafeType(evaluator, field->getType())) diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 343a799bd27f5..dcd4916ee1357 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -2302,6 +2302,40 @@ namespace { } } + if (const auto *ctsd = + dyn_cast(decl)) { + for (auto arg : ctsd->getTemplateArgs().asArray()) { + auto done = false; + auto checkUnsafe = [&](clang::TemplateArgument tyArg) { + if (tyArg.getKind() != clang::TemplateArgument::Type) + return; + + auto safety = + evaluateOrDefault(Impl.SwiftContext.evaluator, + ClangTypeExplicitSafety({tyArg.getAsType()}), + ExplicitSafety::Unspecified); + + if (safety == ExplicitSafety::Unsafe) { + result->addAttribute(new (Impl.SwiftContext) + UnsafeAttr(/*implicit=*/true)); + done = true; + } + }; + + if (arg.getKind() == clang::TemplateArgument::Pack) { + for (auto pkArg : arg.getPackAsArray()) { + checkUnsafe(pkArg); + if (done) + break; + } + } else { + checkUnsafe(arg); + } + if (done) + break; + } + } + bool isNonEscapable = false; if (evaluateOrDefault( Impl.SwiftContext.evaluator, diff --git a/test/Interop/Cxx/class/safe-interop-mode.swift b/test/Interop/Cxx/class/safe-interop-mode.swift index f9eb61a94c449..11eac1ce830f4 100644 --- a/test/Interop/Cxx/class/safe-interop-mode.swift +++ b/test/Interop/Cxx/class/safe-interop-mode.swift @@ -94,16 +94,6 @@ using TTakePtr = TTake; using TTakeSafeTuple = TTake; using TTakeUnsafeTuple = TTake; -// An escapability or explicit safety annotation means a type is considered safe -// even if it would otherwise be considered unsafe. -template struct SWIFT_ESCAPABLE TEscape {}; -template struct __attribute__((swift_attr("safe"))) TSafe { void *ptr; }; - -using TEscapePtr = TEscape; -using TEscapeUnsafeTuple = TEscape; -using TSafePtr = TSafe; -using TSafeTuple = TSafe; - struct HoldsShared { SharedObject* obj; @@ -223,8 +213,3 @@ func useTTakeUnsafeTuple(x: TTakeUnsafeTuple) { // expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}} _ = x // expected-note{{reference to parameter 'x' involves unsafe type}} } - -func useTEscapePtr(x: TEscapePtr) { _ = x } -func useTEscapeUnsafeTuple(x: TEscapeUnsafeTuple) { _ = x } -func useTSafePtr(x: TSafePtr) { _ = x } -func useTSafeTuple(x: TSafeTuple) { _ = x } From ad8b5a520467a1cddf651fd7ddaa3c0b0453a13c Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 17:58:34 -0800 Subject: [PATCH 6/8] Revert "[cxx-interop] Use clang::CanQualType in ClangTypeExplicitSafetyDescriptor" This reverts commit 5328572fbe8a8886aecf1e9ea42fb06b129eb776. --- include/swift/ClangImporter/ClangImporterRequests.h | 6 ++---- lib/ClangImporter/ClangImporter.cpp | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/include/swift/ClangImporter/ClangImporterRequests.h b/include/swift/ClangImporter/ClangImporterRequests.h index ec626cb0e9164..0d8b642af3702 100644 --- a/include/swift/ClangImporter/ClangImporterRequests.h +++ b/include/swift/ClangImporter/ClangImporterRequests.h @@ -625,11 +625,9 @@ void simple_display(llvm::raw_ostream &out, CxxValueSemanticsDescriptor desc); SourceLoc extractNearestSourceLoc(CxxValueSemanticsDescriptor desc); struct ClangTypeExplicitSafetyDescriptor final { - clang::CanQualType type; + clang::QualType type; - ClangTypeExplicitSafetyDescriptor(clang::CanQualType type) : type(type) {} - ClangTypeExplicitSafetyDescriptor(clang::QualType type) - : type(type->getCanonicalTypeUnqualified()) {} + ClangTypeExplicitSafetyDescriptor(clang::QualType type) : type(type) {} friend llvm::hash_code hash_value(const ClangTypeExplicitSafetyDescriptor &desc) { diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index a143752419cde..2a1c28b1947ff 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8725,8 +8725,8 @@ SourceLoc swift::extractNearestSourceLoc(SafeUseOfCxxDeclDescriptor desc) { void swift::simple_display(llvm::raw_ostream &out, ClangTypeExplicitSafetyDescriptor desc) { - auto qt = static_cast(desc.type); - out << "Checking if type '" << qt.getAsString() << "' is explicitly safe.\n"; + out << "Checking if type '" << desc.type.getAsString() + << "' is explicitly safe.\n"; } SourceLoc swift::extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc) { @@ -8735,7 +8735,7 @@ SourceLoc swift::extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc) ExplicitSafety ClangTypeExplicitSafety::evaluate( Evaluator &evaluator, ClangTypeExplicitSafetyDescriptor desc) const { - auto clangType = static_cast(desc.type); + auto clangType = desc.type; // Handle pointers. auto pointeeType = clangType->getPointeeType(); From 4389cc9bea3510df810857d194ac38e61dedc83a Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 18:03:16 -0800 Subject: [PATCH 7/8] Revert "[cxx-interop] Mark class templates with unsafe type arguments as unsafe" This reverts commit afaa499e3a6f5e55b7c9b75155f4bd1710876f12. --- .../ClangImporter/ClangImporterRequests.h | 45 ---------- .../ClangImporter/ClangImporterTypeIDZone.def | 3 - lib/ClangImporter/ClangImporter.cpp | 85 ++++++++----------- lib/ClangImporter/ImportDecl.cpp | 35 -------- .../Interop/Cxx/class/safe-interop-mode.swift | 29 ------- 5 files changed, 36 insertions(+), 161 deletions(-) diff --git a/include/swift/ClangImporter/ClangImporterRequests.h b/include/swift/ClangImporter/ClangImporterRequests.h index 0d8b642af3702..fe83d0fae0405 100644 --- a/include/swift/ClangImporter/ClangImporterRequests.h +++ b/include/swift/ClangImporter/ClangImporterRequests.h @@ -624,51 +624,6 @@ class CxxValueSemantics void simple_display(llvm::raw_ostream &out, CxxValueSemanticsDescriptor desc); SourceLoc extractNearestSourceLoc(CxxValueSemanticsDescriptor desc); -struct ClangTypeExplicitSafetyDescriptor final { - clang::QualType type; - - ClangTypeExplicitSafetyDescriptor(clang::QualType type) : type(type) {} - - friend llvm::hash_code - hash_value(const ClangTypeExplicitSafetyDescriptor &desc) { - return llvm::hash_combine(desc.type.getTypePtrOrNull()); - } - - friend bool operator==(const ClangTypeExplicitSafetyDescriptor &lhs, - const ClangTypeExplicitSafetyDescriptor &rhs) { - return lhs.type == rhs.type; - } - - friend bool operator!=(const ClangTypeExplicitSafetyDescriptor &lhs, - const ClangTypeExplicitSafetyDescriptor &rhs) { - return !(lhs == rhs); - } -}; - -void simple_display(llvm::raw_ostream &out, - ClangTypeExplicitSafetyDescriptor desc); -SourceLoc extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc); - -/// Determine the safety of the given Clang declaration. -class ClangTypeExplicitSafety - : public SimpleRequest { -public: - using SimpleRequest::SimpleRequest; - - // Source location - SourceLoc getNearestLoc() const { return SourceLoc(); }; - bool isCached() const { return true; } - -private: - friend SimpleRequest; - - // Evaluation. - ExplicitSafety evaluate(Evaluator &evaluator, - ClangTypeExplicitSafetyDescriptor desc) const; -}; - struct CxxDeclExplicitSafetyDescriptor final { const clang::Decl *decl; bool isClass; diff --git a/include/swift/ClangImporter/ClangImporterTypeIDZone.def b/include/swift/ClangImporter/ClangImporterTypeIDZone.def index 231c4efc0d2c1..0d562311a9c72 100644 --- a/include/swift/ClangImporter/ClangImporterTypeIDZone.def +++ b/include/swift/ClangImporter/ClangImporterTypeIDZone.def @@ -48,9 +48,6 @@ SWIFT_REQUEST(ClangImporter, ClangTypeEscapability, SWIFT_REQUEST(ClangImporter, CxxValueSemantics, CxxValueSemanticsKind(CxxValueSemanticsDescriptor), Cached, NoLocationInfo) -SWIFT_REQUEST(ClangImporter, ClangTypeExplicitSafety, - ExplicitSafety(ClangTypeExplicitSafetyDescriptor), Cached, - NoLocationInfo) SWIFT_REQUEST(ClangImporter, ClangDeclExplicitSafety, ExplicitSafety(CxxDeclExplicitSafetyDescriptor), Cached, NoLocationInfo) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 2a1c28b1947ff..758ca73a84259 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -8723,51 +8723,6 @@ SourceLoc swift::extractNearestSourceLoc(SafeUseOfCxxDeclDescriptor desc) { return SourceLoc(); } -void swift::simple_display(llvm::raw_ostream &out, - ClangTypeExplicitSafetyDescriptor desc) { - out << "Checking if type '" << desc.type.getAsString() - << "' is explicitly safe.\n"; -} - -SourceLoc swift::extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc) { - return SourceLoc(); -} - -ExplicitSafety ClangTypeExplicitSafety::evaluate( - Evaluator &evaluator, ClangTypeExplicitSafetyDescriptor desc) const { - auto clangType = desc.type; - - // Handle pointers. - auto pointeeType = clangType->getPointeeType(); - if (!pointeeType.isNull()) { - // Function pointers are okay. - if (pointeeType->isFunctionType()) - return ExplicitSafety::Safe; - - // Pointers to record types are okay if they come in as foreign reference - // types. - if (auto *recordDecl = pointeeType->getAsRecordDecl()) { - if (hasImportAsRefAttr(recordDecl)) - return ExplicitSafety::Safe; - } - - // All other pointers are considered unsafe. - return ExplicitSafety::Unsafe; - } - - // Handle records recursively. - if (auto recordDecl = clangType->getAsTagDecl()) { - // If we reached this point the types is not imported as a shared reference, - // so we don't need to check the bases whether they are shared references. - return evaluateOrDefault(evaluator, - ClangDeclExplicitSafety({recordDecl, false}), - ExplicitSafety::Unspecified); - } - - // Everything else is safe. - return ExplicitSafety::Safe; -} - void swift::simple_display(llvm::raw_ostream &out, CxxDeclExplicitSafetyDescriptor desc) { out << "Checking if '"; @@ -8839,11 +8794,43 @@ CustomRefCountingOperationResult CustomRefCountingOperation::evaluate( /// Check whether the given Clang type involves an unsafe type. static bool hasUnsafeType(Evaluator &evaluator, clang::QualType clangType) { + // Handle pointers. + auto pointeeType = clangType->getPointeeType(); + if (!pointeeType.isNull()) { + // Function pointers are okay. + if (pointeeType->isFunctionType()) + return false; + + // Pointers to record types are okay if they come in as foreign reference + // types. + if (auto recordDecl = pointeeType->getAsRecordDecl()) { + if (hasImportAsRefAttr(recordDecl)) + return false; + } + + // All other pointers are considered unsafe. + return true; + } - auto safety = - evaluateOrDefault(evaluator, ClangTypeExplicitSafety({clangType}), - ExplicitSafety::Unspecified); - return safety == ExplicitSafety::Unsafe; + // Handle records recursively. + if (auto recordDecl = clangType->getAsTagDecl()) { + // If we reached this point the types is not imported as a shared reference, + // so we don't need to check the bases whether they are shared references. + auto safety = evaluateOrDefault( + evaluator, ClangDeclExplicitSafety({recordDecl, false}), + ExplicitSafety::Unspecified); + switch (safety) { + case ExplicitSafety::Unsafe: + return true; + + case ExplicitSafety::Safe: + case ExplicitSafety::Unspecified: + return false; + } + } + + // Everything else is safe. + return false; } ExplicitSafety diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index dcd4916ee1357..4a04488dfaec4 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -69,7 +69,6 @@ #include "clang/AST/RecordLayout.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/StmtVisitor.h" -#include "clang/AST/TemplateBase.h" #include "clang/AST/Type.h" #include "clang/Basic/Specifiers.h" #include "clang/Basic/TargetInfo.h" @@ -2302,40 +2301,6 @@ namespace { } } - if (const auto *ctsd = - dyn_cast(decl)) { - for (auto arg : ctsd->getTemplateArgs().asArray()) { - auto done = false; - auto checkUnsafe = [&](clang::TemplateArgument tyArg) { - if (tyArg.getKind() != clang::TemplateArgument::Type) - return; - - auto safety = - evaluateOrDefault(Impl.SwiftContext.evaluator, - ClangTypeExplicitSafety({tyArg.getAsType()}), - ExplicitSafety::Unspecified); - - if (safety == ExplicitSafety::Unsafe) { - result->addAttribute(new (Impl.SwiftContext) - UnsafeAttr(/*implicit=*/true)); - done = true; - } - }; - - if (arg.getKind() == clang::TemplateArgument::Pack) { - for (auto pkArg : arg.getPackAsArray()) { - checkUnsafe(pkArg); - if (done) - break; - } - } else { - checkUnsafe(arg); - } - if (done) - break; - } - } - bool isNonEscapable = false; if (evaluateOrDefault( Impl.SwiftContext.evaluator, diff --git a/test/Interop/Cxx/class/safe-interop-mode.swift b/test/Interop/Cxx/class/safe-interop-mode.swift index 11eac1ce830f4..2696b88b19c2e 100644 --- a/test/Interop/Cxx/class/safe-interop-mode.swift +++ b/test/Interop/Cxx/class/safe-interop-mode.swift @@ -83,17 +83,6 @@ struct OwnedData { void takeSharedObject(SharedObject *) const; }; -// A class template that throws away its type argument. -// -// If this template is instantiated with an unsafe type, it should be considered -// unsafe even if that type is never used. -template struct TTake {}; - -using TTakeInt = TTake; -using TTakePtr = TTake; -using TTakeSafeTuple = TTake; -using TTakeUnsafeTuple = TTake; - struct HoldsShared { SharedObject* obj; @@ -195,21 +184,3 @@ func useSharedReference(frt: DerivedFromSharedObject, h: HoldsShared) { let _ = frt let _ = h.getObj() } - -func useTTakeInt(x: TTakeInt) { - _ = x -} - -func useTTakePtr(x: TTakePtr) { - // expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}} - _ = x // expected-note{{reference to parameter 'x' involves unsafe type}} -} - -func useTTakeSafeTuple(x: TTakeSafeTuple) { - _ = x -} - -func useTTakeUnsafeTuple(x: TTakeUnsafeTuple) { - // expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}} - _ = x // expected-note{{reference to parameter 'x' involves unsafe type}} -} From ae934749cbfd39a6904b06cd7d826274758e4750 Mon Sep 17 00:00:00 2001 From: John Hui Date: Mon, 3 Nov 2025 18:03:49 -0800 Subject: [PATCH 8/8] Revert "[cxx-interop] Do not import template type arguments" This reverts commit d5473feb5a9606d6926c97c08ff584213d3b33a0. --- lib/ClangImporter/ImportDecl.cpp | 28 ++++++++++ .../sfinae-template-type-arguments.swift | 53 ------------------- 2 files changed, 28 insertions(+), 53 deletions(-) delete mode 100644 test/Interop/Cxx/templates/sfinae-template-type-arguments.swift diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 4a04488dfaec4..17f8e32c767bd 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -2301,6 +2301,34 @@ namespace { } } + // We have to do this after populating ImportedDecls to avoid importing + // the same decl multiple times. Also after we imported the bases. + if (const auto *ctsd = + dyn_cast(decl)) { + for (auto arg : ctsd->getTemplateArgs().asArray()) { + llvm::SmallVector nonPackArgs; + if (arg.getKind() == clang::TemplateArgument::Pack) { + auto pack = arg.getPackAsArray(); + nonPackArgs.assign(pack.begin(), pack.end()); + } else { + nonPackArgs.push_back(arg); + } + for (auto realArg : nonPackArgs) { + if (realArg.getKind() != clang::TemplateArgument::Type) + continue; + auto SwiftType = Impl.importTypeIgnoreIUO( + realArg.getAsType(), ImportTypeKind::Abstract, + [](Diagnostic &&diag) {}, false, Bridgeability::None, + ImportTypeAttrs()); + if (SwiftType && SwiftType->isUnsafe()) { + auto attr = new (Impl.SwiftContext) UnsafeAttr(/*implicit=*/true); + result->getAttrs().add(attr); + break; + } + } + } + } + bool isNonEscapable = false; if (evaluateOrDefault( Impl.SwiftContext.evaluator, diff --git a/test/Interop/Cxx/templates/sfinae-template-type-arguments.swift b/test/Interop/Cxx/templates/sfinae-template-type-arguments.swift deleted file mode 100644 index e24b8bfe842ef..0000000000000 --- a/test/Interop/Cxx/templates/sfinae-template-type-arguments.swift +++ /dev/null @@ -1,53 +0,0 @@ -// RUN: split-file %s %t -// RUN: %target-clang -c -o /dev/null -Xclang -verify -I %t/Inputs %t/cxx.cpp -// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default -I %t%{fs-sep}Inputs -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}CxxHeader.h %t%{fs-sep}main.swift -// RUN: %target-swift-ide-test -print-module -module-to-print=CxxModule -I %t/Inputs -source-filename=x -cxx-interoperability-mode=default | %FileCheck %t/Inputs/CxxHeader.h - -//--- Inputs/module.modulemap -module CxxModule { - requires cplusplus - header "CxxHeader.h" -} - -//--- Inputs/CxxHeader.h -#pragma once - -struct Empty {}; -template struct MissingMember { typename T::Missing member; }; -using SUB = MissingMember; - -struct Incomplete; -template struct IncompleteField { T member; }; -using INC = IncompleteField; - -template struct DefaultedTemplateArgs {}; -using DefaultedTemplateArgsInst = DefaultedTemplateArgs<>; - -// CHECK: struct DefaultedTemplateArgs, IncompleteField> { -// CHECK: } -// CHECK: typealias DefaultedTemplateArgsInst = DefaultedTemplateArgs, IncompleteField> - -template struct ThrowsAwayTemplateArgs {}; -using ThrowsAwayTemplateArgsInst = ThrowsAwayTemplateArgs; - -// CHECK: struct ThrowsAwayTemplateArgs, IncompleteField> { -// CHECK: } -// CHECK: typealias ThrowsAwayTemplateArgsInst = ThrowsAwayTemplateArgs, IncompleteField> - -//--- cxx.cpp -// expected-no-diagnostics -#include -void make(void) { - DefaultedTemplateArgs<> dta{}; - DefaultedTemplateArgsInst dtai{}; - - ThrowsAwayTemplateArgs tata{}; - ThrowsAwayTemplateArgsInst tatai{}; -} - -//--- main.swift -import CxxModule -func make() { - let _: DefaultedTemplateArgsInst = .init() - let _: ThrowsAwayTemplateArgsInst = .init() -}