From a8f3bb5e5efd433c79a1708385b8cc395bf760e8 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 28 Mar 2024 11:20:14 -0700 Subject: [PATCH] Implement `AsyncIteratorProtocol.next()` in terms of `next(isolation:)`. New async iterators should be able to implement only `next(isolation:)` and get the older `next()` implementation via a default. Implement the appropriate default witness. Fixes rdar://125447861. --- .../Concurrency/AsyncIteratorProtocol.swift | 17 +++++++++++++++-- test/Concurrency/async_sequence_rethrows.swift | 10 ++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/stdlib/public/Concurrency/AsyncIteratorProtocol.swift b/stdlib/public/Concurrency/AsyncIteratorProtocol.swift index 56ff75bc56f..bc390d431bb 100644 --- a/stdlib/public/Concurrency/AsyncIteratorProtocol.swift +++ b/stdlib/public/Concurrency/AsyncIteratorProtocol.swift @@ -111,8 +111,9 @@ public protocol AsyncIteratorProtocol { @available(SwiftStdlib 5.1, *) extension AsyncIteratorProtocol { - /// Default implementation of `next()` in terms of `next()`, which is - /// required to maintain backward compatibility with existing async iterators. + /// Default implementation of `next(isolation:)` in terms of `next()`, which + /// is required to maintain backward compatibility with existing async + /// iterators. @available(SwiftStdlib 6.0, *) @inlinable public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? { @@ -123,3 +124,15 @@ extension AsyncIteratorProtocol { } } } + +@available(SwiftStdlib 5.1, *) +extension AsyncIteratorProtocol { + /// Default implementation of `next()` in terms of `next(isolation:)`, which + /// is required to maintain backward compatibility with existing async + /// iterators. + @available(SwiftStdlib 6.0, *) + @inlinable + public mutating func next() async throws(Failure) -> Element? { + return try await next(isolation: nil) + } +} diff --git a/test/Concurrency/async_sequence_rethrows.swift b/test/Concurrency/async_sequence_rethrows.swift index 187af6e903c..9a215f07eda 100644 --- a/test/Concurrency/async_sequence_rethrows.swift +++ b/test/Concurrency/async_sequence_rethrows.swift @@ -75,3 +75,13 @@ enum MyError: Error { func getASequence() -> any AsyncSequence { return ErrorSequence(throwError: MyError.foo) // ERROR: Cannot convert return expression of type 'any Error' to return type 'MyError' } + +// Test the default implementation of next() in terms of next(isolation:). +struct AsyncIteratorWithOnlyNextIsolation: AsyncIteratorProtocol { + public mutating func next(isolation: (any Actor)?) throws(MyError) -> Int? { 0 } +} + +// Test the default implementation of next(isolation:) in terms of next(). +struct AsyncIteratorWithOnlyNext: AsyncIteratorProtocol { + public mutating func next() throws(MyError) -> Int? { 0 } +}