From 9db691e528ebdfd0529cd8963e5d814ee547f8c4 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 6 Oct 2025 13:13:35 -0400 Subject: [PATCH] Remove some uses of `@unchecked Sendable` and `AnySequence`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR removes some outstanding uses of `@unchecked Sendable` and of `AnySequence`. Ironically we still need `Test.testCases` to type-erase with `AnySequence` _in its implementation only_ for now because if we change it from `some Sequence` to `any Sequence` we get this diagnostic: > 🛑 Runtime support for parameterized protocol types is only available in macOS > 13.0.0 or newer --- Sources/Testing/Test.swift | 21 +++++++------------ .../_TestDiscovery/TestContentRecord.swift | 4 ++-- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Sources/Testing/Test.swift b/Sources/Testing/Test.swift index 5f2ac2406..52b41137d 100644 --- a/Sources/Testing/Test.swift +++ b/Sources/Testing/Test.swift @@ -69,26 +69,19 @@ public struct Test: Sendable { public nonisolated(unsafe) var xcTestCompatibleSelector: __XCTestCompatibleSelector? /// An enumeration describing the evaluation state of a test's cases. - /// - /// This use of `@unchecked Sendable` and of `AnySequence` in this type's - /// cases is necessary because it is not currently possible to express - /// `Sequence & Sendable` as an existential (`any`) - /// ([96960993](rdar://96960993)). It is also not possible to have a value of - /// an underlying generic sequence type without specifying its generic - /// parameters. - fileprivate enum TestCasesState: @unchecked Sendable { + fileprivate enum TestCasesState: Sendable { /// The test's cases have not yet been evaluated. /// /// - Parameters: /// - function: The function to call to evaluate the test's cases. The /// result is a sequence of test cases. - case unevaluated(_ function: @Sendable () async throws -> AnySequence) + case unevaluated(_ function: @Sendable () async throws -> any Sequence & Sendable) /// The test's cases have been evaluated. /// /// - Parameters: /// - testCases: The test's cases. - case evaluated(_ testCases: AnySequence) + case evaluated(_ testCases: any Sequence & Sendable) /// An error was thrown when the testing library attempted to evaluate the /// test's cases. @@ -124,7 +117,7 @@ public struct Test: Sendable { // attempt to run it, and thus never access this property. preconditionFailure("Attempting to access test cases with invalid state. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new and include this information: \(String(reflecting: testCasesState))") } - return testCases + return AnySequence(testCases) } } @@ -139,7 +132,7 @@ public struct Test: Sendable { var uncheckedTestCases: (some Sequence)? { testCasesState.flatMap { testCasesState in if case let .evaluated(testCases) = testCasesState { - return testCases + return AnySequence(testCases) } return nil } @@ -239,7 +232,7 @@ public struct Test: Sendable { self.sourceLocation = sourceLocation self.containingTypeInfo = containingTypeInfo self.xcTestCompatibleSelector = xcTestCompatibleSelector - self.testCasesState = .unevaluated { .init(try await testCases()) } + self.testCasesState = .unevaluated { try await testCases() } self.parameters = parameters } @@ -260,7 +253,7 @@ public struct Test: Sendable { self.sourceLocation = sourceLocation self.containingTypeInfo = containingTypeInfo self.xcTestCompatibleSelector = xcTestCompatibleSelector - self.testCasesState = .evaluated(.init(testCases)) + self.testCasesState = .evaluated(testCases) self.parameters = parameters } } diff --git a/Sources/_TestDiscovery/TestContentRecord.swift b/Sources/_TestDiscovery/TestContentRecord.swift index 384113c1b..b830026e2 100644 --- a/Sources/_TestDiscovery/TestContentRecord.swift +++ b/Sources/_TestDiscovery/TestContentRecord.swift @@ -85,7 +85,7 @@ public struct TestContentRecord where T: DiscoverableAsTestContent { public private(set) nonisolated(unsafe) var imageAddress: UnsafeRawPointer? /// A type defining storage for the underlying test content record. - private enum _RecordStorage: @unchecked Sendable { + private enum _RecordStorage { /// The test content record is stored by address. case atAddress(UnsafePointer<_TestContentRecord>) @@ -94,7 +94,7 @@ public struct TestContentRecord where T: DiscoverableAsTestContent { } /// Storage for `_record`. - private var _recordStorage: _RecordStorage + private nonisolated(unsafe) var _recordStorage: _RecordStorage /// The underlying test content record. private var _record: _TestContentRecord {