@@ -63,9 +63,36 @@ import Swift
6363/// such as returning the work completed so far, returning an empty result, or returning `nil`.
6464/// For tasks that need to handle cancellation by throwing an error,
6565/// use the `withThrowingTaskGroup(of:returning:body:)` method instead.
66+ @available ( SwiftStdlib 5 . 1 , * )
67+ @backDeployed ( before: SwiftStdlib 6.0 , * )
68+ @inlinable
69+ public func withTaskGroup< ChildTaskResult, GroupResult> (
70+ of childTaskResultType: ChildTaskResult . Type ,
71+ returning returnType: GroupResult . Type = GroupResult . self,
72+ isolation: isolated ( any Actor ) ? = #isolation,
73+ body: ( inout TaskGroup < ChildTaskResult > ) async -> GroupResult
74+ ) async -> GroupResult {
75+ #if compiler(>=5.5) && $BuiltinTaskGroupWithArgument
76+
77+ let _group = Builtin . createTaskGroup ( ChildTaskResult . self)
78+ var group = TaskGroup < ChildTaskResult > ( group: _group)
79+
80+ // Run the withTaskGroup body.
81+ let result = await body ( & group)
82+
83+ await group. awaitAllRemainingTasks ( )
84+
85+ Builtin . destroyTaskGroup ( _group)
86+ return result
87+
88+ #else
89+ fatalError ( " Swift compiler is incompatible with this SDK version " )
90+ #endif
91+ }
92+
6693@available ( SwiftStdlib 5 . 1 , * )
6794@_silgen_name ( " $ss13withTaskGroup2of9returning4bodyq_xm_q_mq_ScGyxGzYaXEtYar0_lF " )
68- @_unsafeInheritExecutor
95+ @_unsafeInheritExecutor // for ABI compatibility
6996@inlinable
7097public func withTaskGroup< ChildTaskResult, GroupResult> (
7198 of childTaskResultType: ChildTaskResult . Type ,
@@ -80,7 +107,6 @@ public func withTaskGroup<ChildTaskResult, GroupResult>(
80107 // Run the withTaskGroup body.
81108 let result = await body ( & group)
82109
83- // TODO(concurrency): should get isolation from param from withThrowingTaskGroup
84110 await group. awaitAllRemainingTasks ( )
85111
86112 Builtin . destroyTaskGroup ( _group)
@@ -167,10 +193,46 @@ public func withTaskGroup<ChildTaskResult, GroupResult>(
167193/// which gives you a chance to handle the individual error
168194/// or to let the group rethrow the error.
169195@available ( SwiftStdlib 5 . 1 , * )
170- @_silgen_name ( " $ss21withThrowingTaskGroup2of9returning4bodyq_xm_q_mq_Scgyxs5Error_pGzYaKXEtYaKr0_lF " )
171- @_unsafeInheritExecutor
196+ @backDeployed ( before: SwiftStdlib 6.0 )
172197@inlinable
173198public func withThrowingTaskGroup< ChildTaskResult, GroupResult> (
199+ of childTaskResultType: ChildTaskResult . Type ,
200+ returning returnType: GroupResult . Type = GroupResult . self,
201+ isolation: isolated ( any Actor ) ? = #isolation,
202+ body: ( inout ThrowingTaskGroup < ChildTaskResult , Error > ) async throws -> GroupResult
203+ ) async rethrows -> GroupResult {
204+ #if compiler(>=5.5) && $BuiltinTaskGroupWithArgument
205+
206+ let _group = Builtin . createTaskGroup ( ChildTaskResult . self)
207+ var group = ThrowingTaskGroup < ChildTaskResult , Error > ( group: _group)
208+
209+ do {
210+ // Run the withTaskGroup body.
211+ let result = try await body ( & group)
212+
213+ await group. awaitAllRemainingTasks ( )
214+ Builtin . destroyTaskGroup ( _group)
215+
216+ return result
217+ } catch {
218+ group. cancelAll ( )
219+
220+ await group. awaitAllRemainingTasks ( )
221+ Builtin . destroyTaskGroup ( _group)
222+
223+ throw error
224+ }
225+
226+ #else
227+ fatalError ( " Swift compiler is incompatible with this SDK version " )
228+ #endif
229+ }
230+
231+ @available ( SwiftStdlib 5 . 1 , * )
232+ @_silgen_name ( " $ss21withThrowingTaskGroup2of9returning4bodyq_xm_q_mq_Scgyxs5Error_pGzYaKXEtYaKr0_lF " )
233+ @_unsafeInheritExecutor // for ABI compatibility
234+ @usableFromInline
235+ internal func withThrowingTaskGroup< ChildTaskResult, GroupResult> (
174236 of childTaskResultType: ChildTaskResult . Type ,
175237 returning returnType: GroupResult . Type = GroupResult . self,
176238 body: ( inout ThrowingTaskGroup < ChildTaskResult , Error > ) async throws -> GroupResult
@@ -184,15 +246,13 @@ public func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
184246 // Run the withTaskGroup body.
185247 let result = try await body ( & group)
186248
187- // TODO(concurrency): should get isolation from param from withThrowingTaskGroup
188249 await group. awaitAllRemainingTasks ( )
189250 Builtin . destroyTaskGroup ( _group)
190251
191252 return result
192253 } catch {
193254 group. cancelAll ( )
194255
195- // TODO(concurrency): should get isolation from param from withThrowingTaskGroup
196256 await group. awaitAllRemainingTasks ( )
197257 Builtin . destroyTaskGroup ( _group)
198258
0 commit comments