Skip to content

Commit f414dda

Browse files
committed
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.
1 parent f232f86 commit f414dda

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

stdlib/public/Concurrency/AsyncIteratorProtocol.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ public protocol AsyncIteratorProtocol<Element, Failure> {
111111

112112
@available(SwiftStdlib 5.1, *)
113113
extension AsyncIteratorProtocol {
114-
/// Default implementation of `next()` in terms of `next()`, which is
115-
/// required to maintain backward compatibility with existing async iterators.
114+
/// Default implementation of `next(isolation:)` in terms of `next()`, which
115+
/// is required to maintain backward compatibility with existing async
116+
/// iterators.
116117
@available(SwiftStdlib 6.0, *)
117118
@inlinable
118119
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? {
@@ -123,3 +124,15 @@ extension AsyncIteratorProtocol {
123124
}
124125
}
125126
}
127+
128+
@available(SwiftStdlib 5.1, *)
129+
extension AsyncIteratorProtocol {
130+
/// Default implementation of `next()` in terms of `next(isolation:)`, which
131+
/// is required to maintain backward compatibility with existing async
132+
/// iterators.
133+
@available(SwiftStdlib 6.0, *)
134+
@inlinable
135+
public mutating func next() async throws(Failure) -> Element? {
136+
return try await next(isolation: nil)
137+
}
138+
}

test/Concurrency/async_sequence_rethrows.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ enum MyError: Error {
7575
func getASequence() -> any AsyncSequence<Data, MyError> {
7676
return ErrorSequence<Data, _>(throwError: MyError.foo) // ERROR: Cannot convert return expression of type 'any Error' to return type 'MyError'
7777
}
78+
79+
// Test the default implementation of next() in terms of next(isolation:).
80+
struct AsyncIteratorWithOnlyNextIsolation: AsyncIteratorProtocol {
81+
public mutating func next(isolation: (any Actor)?) throws(MyError) -> Int? { 0 }
82+
}
83+
84+
// Test the default implementation of next(isolation:) in terms of next().
85+
struct AsyncIteratorWithOnlyNext: AsyncIteratorProtocol {
86+
public mutating func next() throws(MyError) -> Int? { 0 }
87+
}

0 commit comments

Comments
 (0)