Skip to content

Commit 1e0aa66

Browse files
committed
[sending] Convert the TaskGroup APIs
(cherry picked from commit c2f3e67)
1 parent fc4507b commit 1e0aa66

File tree

5 files changed

+47
-40
lines changed

5 files changed

+47
-40
lines changed

stdlib/public/Concurrency/DiscardingTaskGroup.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public struct DiscardingTaskGroup {
178178
#endif
179179
public mutating func addTask(
180180
priority: TaskPriority? = nil,
181-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
181+
operation: sending @escaping @isolated(any) () async -> Void
182182
) {
183183
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
184184
let flags = taskCreateFlags(
@@ -235,7 +235,7 @@ public struct DiscardingTaskGroup {
235235
#endif
236236
public mutating func addTaskUnlessCancelled(
237237
priority: TaskPriority? = nil,
238-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
238+
operation: sending @escaping @isolated(any) () async -> Void
239239
) -> Bool {
240240
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
241241

@@ -287,7 +287,7 @@ public struct DiscardingTaskGroup {
287287
@_alwaysEmitIntoClient
288288
@_allowFeatureSuppression(IsolatedAny)
289289
public mutating func addTask(
290-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
290+
operation: sending @escaping @isolated(any) () async -> Void
291291
) {
292292
let flags = taskCreateFlags(
293293
priority: nil, isChildTask: true, copyTaskLocals: false,
@@ -332,7 +332,7 @@ public struct DiscardingTaskGroup {
332332
@_allowFeatureSuppression(IsolatedAny)
333333
@_alwaysEmitIntoClient
334334
public mutating func addTaskUnlessCancelled(
335-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
335+
operation: sending @escaping @isolated(any) () async -> Void
336336
) -> Bool {
337337
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
338338
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -394,7 +394,7 @@ public struct DiscardingTaskGroup {
394394
#endif
395395
public mutating func addTask(
396396
priority: TaskPriority? = nil,
397-
operation: __owned @Sendable @escaping () async -> Void
397+
operation: sending @escaping () async -> Void
398398
) {
399399
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
400400
let flags = taskCreateFlags(
@@ -442,7 +442,7 @@ public struct DiscardingTaskGroup {
442442
#endif
443443
public mutating func addTaskUnlessCancelled(
444444
priority: TaskPriority? = nil,
445-
operation: __owned @Sendable @escaping () async -> Void
445+
operation: sending @escaping () async -> Void
446446
) -> Bool {
447447
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
448448

@@ -485,7 +485,7 @@ public struct DiscardingTaskGroup {
485485

486486
@_alwaysEmitIntoClient
487487
public mutating func addTask(
488-
operation: __owned @Sendable @escaping () async -> Void
488+
operation: sending @escaping () async -> Void
489489
) {
490490
let flags = taskCreateFlags(
491491
priority: nil, isChildTask: true, copyTaskLocals: false,
@@ -521,7 +521,7 @@ public struct DiscardingTaskGroup {
521521
#endif
522522
@_alwaysEmitIntoClient
523523
public mutating func addTaskUnlessCancelled(
524-
operation: __owned @Sendable @escaping () async -> Void
524+
operation: sending @escaping () async -> Void
525525
) -> Bool {
526526
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
527527
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -830,7 +830,7 @@ public struct ThrowingDiscardingTaskGroup<Failure: Error> {
830830
@_allowFeatureSuppression(IsolatedAny)
831831
public mutating func addTask(
832832
priority: TaskPriority? = nil,
833-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> Void
833+
operation: sending @escaping @isolated(any) () async throws -> Void
834834
) {
835835
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
836836
let flags = taskCreateFlags(
@@ -874,7 +874,7 @@ public struct ThrowingDiscardingTaskGroup<Failure: Error> {
874874
@_allowFeatureSuppression(IsolatedAny)
875875
public mutating func addTaskUnlessCancelled(
876876
priority: TaskPriority? = nil,
877-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> Void
877+
operation: sending @escaping @isolated(any) () async throws -> Void
878878
) -> Bool {
879879
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
880880
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ extension Task where Failure == Never {
223223
public init(
224224
executorPreference taskExecutor: consuming (any TaskExecutor)?,
225225
priority: TaskPriority? = nil,
226-
operation: __owned @Sendable @escaping () async -> Success
226+
operation: sending @escaping () async -> Success
227227
) {
228228
guard let taskExecutor else {
229229
self = Self.init(priority: priority, operation: operation)
@@ -283,7 +283,7 @@ extension Task where Failure == Error {
283283
public init(
284284
executorPreference taskExecutor: consuming (any TaskExecutor)?,
285285
priority: TaskPriority? = nil,
286-
operation: __owned @Sendable @escaping () async throws -> Success
286+
operation: sending @escaping () async throws -> Success
287287
) {
288288
guard let taskExecutor else {
289289
self = Self.init(priority: priority, operation: operation)
@@ -342,7 +342,7 @@ extension Task where Failure == Never {
342342
public static func detached(
343343
executorPreference taskExecutor: (any TaskExecutor)?,
344344
priority: TaskPriority? = nil,
345-
operation: __owned @Sendable @escaping () async -> Success
345+
operation: sending @escaping () async -> Success
346346
) -> Task<Success, Failure> {
347347
guard let taskExecutor else {
348348
return Self.detached(priority: priority, operation: operation)
@@ -401,7 +401,7 @@ extension Task where Failure == Error {
401401
public static func detached(
402402
executorPreference taskExecutor: (any TaskExecutor)?,
403403
priority: TaskPriority? = nil,
404-
operation: __owned @Sendable @escaping () async throws -> Success
404+
operation: sending @escaping () async throws -> Success
405405
) -> Task<Success, Failure> {
406406
guard let taskExecutor else {
407407
return Self.detached(priority: priority, operation: operation)

stdlib/public/Concurrency/TaskGroup+TaskExecutor.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension TaskGroup {
3535
public mutating func addTask(
3636
executorPreference taskExecutor: (any TaskExecutor)?,
3737
priority: TaskPriority? = nil,
38-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
38+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
3939
) {
4040
guard let taskExecutor else {
4141
return self.addTask(priority: priority, operation: operation)
@@ -84,7 +84,7 @@ extension TaskGroup {
8484
public mutating func addTaskUnlessCancelled(
8585
executorPreference taskExecutor: (any TaskExecutor)?,
8686
priority: TaskPriority? = nil,
87-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
87+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
8888
) -> Bool {
8989
guard let taskExecutor else {
9090
return self.addTaskUnlessCancelled(priority: priority, operation: operation)
@@ -144,7 +144,7 @@ extension ThrowingTaskGroup {
144144
public mutating func addTask(
145145
executorPreference taskExecutor: (any TaskExecutor)?,
146146
priority: TaskPriority? = nil,
147-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult
147+
operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult
148148
) {
149149
guard let taskExecutor else {
150150
return self.addTask(priority: priority, operation: operation)
@@ -189,7 +189,7 @@ extension ThrowingTaskGroup {
189189
public mutating func addTaskUnlessCancelled(
190190
executorPreference taskExecutor: (any TaskExecutor)?,
191191
priority: TaskPriority? = nil,
192-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult
192+
operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult
193193
) -> Bool {
194194
guard let taskExecutor else {
195195
return self.addTaskUnlessCancelled(priority: priority, operation: operation)
@@ -249,7 +249,7 @@ extension DiscardingTaskGroup {
249249
public mutating func addTask(
250250
executorPreference taskExecutor: (any TaskExecutor)?,
251251
priority: TaskPriority? = nil,
252-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
252+
operation: sending @escaping @isolated(any) () async -> Void
253253
) {
254254
guard let taskExecutor else {
255255
return self.addTask(priority: priority, operation: operation)
@@ -299,7 +299,7 @@ extension DiscardingTaskGroup {
299299
public mutating func addTaskUnlessCancelled(
300300
executorPreference taskExecutor: (any TaskExecutor)?,
301301
priority: TaskPriority? = nil,
302-
operation: __owned @Sendable @escaping @isolated(any) () async -> Void
302+
operation: sending @escaping @isolated(any) () async -> Void
303303
) -> Bool {
304304
guard let taskExecutor else {
305305
return self.addTaskUnlessCancelled(priority: priority, operation: operation)
@@ -359,7 +359,7 @@ extension ThrowingDiscardingTaskGroup {
359359
public mutating func addTask(
360360
executorPreference taskExecutor: (any TaskExecutor)?,
361361
priority: TaskPriority? = nil,
362-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> Void
362+
operation: sending @escaping @isolated(any) () async throws -> Void
363363
) {
364364
guard let taskExecutor else {
365365
return self.addTask(priority: priority, operation: operation)
@@ -409,7 +409,7 @@ extension ThrowingDiscardingTaskGroup {
409409
public mutating func addTaskUnlessCancelled(
410410
executorPreference taskExecutor: (any TaskExecutor)?,
411411
priority: TaskPriority? = nil,
412-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> Void
412+
operation: sending @escaping @isolated(any) () async throws -> Void
413413
) -> Bool {
414414
guard let taskExecutor else {
415415
return self.addTaskUnlessCancelled(priority: priority, operation: operation)

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
269269
@_allowFeatureSuppression(IsolatedAny)
270270
public mutating func addTask(
271271
priority: TaskPriority? = nil,
272-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
272+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
273273
) {
274274
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
275275
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@@ -316,7 +316,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
316316
@_allowFeatureSuppression(IsolatedAny)
317317
public mutating func addTaskUnlessCancelled(
318318
priority: TaskPriority? = nil,
319-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
319+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
320320
) -> Bool {
321321
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
322322
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -362,7 +362,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
362362
@_alwaysEmitIntoClient
363363
public mutating func addTask(
364364
priority: TaskPriority? = nil,
365-
operation: __owned @Sendable @escaping () async -> ChildTaskResult
365+
operation: sending @escaping () async -> ChildTaskResult
366366
) {
367367
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
368368
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@@ -390,7 +390,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
390390
@_alwaysEmitIntoClient
391391
public mutating func addTaskUnlessCancelled(
392392
priority: TaskPriority? = nil,
393-
operation: __owned @Sendable @escaping () async -> ChildTaskResult
393+
operation: sending @escaping () async -> ChildTaskResult
394394
) -> Bool {
395395
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
396396
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -428,7 +428,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
428428
@_allowFeatureSuppression(IsolatedAny)
429429
public mutating func addTask(
430430
priority: TaskPriority? = nil,
431-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
431+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
432432
) {
433433
fatalError("Unavailable in task-to-thread concurrency model")
434434
}
@@ -440,7 +440,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
440440
@_alwaysEmitIntoClient
441441
@_allowFeatureSuppression(IsolatedAny)
442442
public mutating func addTask(
443-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
443+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
444444
) {
445445
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
446446
let flags = taskCreateFlags(
@@ -469,7 +469,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
469469
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
470470
public mutating func addTaskUnlessCancelled(
471471
priority: TaskPriority? = nil,
472-
operation: __owned @Sendable @escaping () async -> ChildTaskResult
472+
operation: sending @escaping () async -> ChildTaskResult
473473
) -> Bool {
474474
fatalError("Unavailable in task-to-thread concurrency model")
475475
}
@@ -483,7 +483,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
483483
@_alwaysEmitIntoClient
484484
@_allowFeatureSuppression(IsolatedAny)
485485
public mutating func addTaskUnlessCancelled(
486-
operation: __owned @Sendable @escaping @isolated(any) () async -> ChildTaskResult
486+
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
487487
) -> Bool {
488488
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
489489
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -814,7 +814,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
814814
@_allowFeatureSuppression(IsolatedAny)
815815
public mutating func addTask(
816816
priority: TaskPriority? = nil,
817-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult
817+
operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult
818818
) {
819819
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
820820
let flags = taskCreateFlags(
@@ -856,7 +856,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
856856
@_allowFeatureSuppression(IsolatedAny)
857857
public mutating func addTaskUnlessCancelled(
858858
priority: TaskPriority? = nil,
859-
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult
859+
operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult
860860
) -> Bool {
861861
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
862862
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -894,7 +894,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
894894
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
895895
public mutating func addTask(
896896
priority: TaskPriority? = nil,
897-
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
897+
operation: sending @escaping () async throws -> ChildTaskResult
898898
) {
899899
fatalError("Unavailable in task-to-thread concurrency model")
900900
}
@@ -908,7 +908,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
908908
/// - operation: The operation to execute as part of the task group.
909909
@_alwaysEmitIntoClient
910910
public mutating func addTask(
911-
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
911+
operation: sending @escaping () async throws -> ChildTaskResult
912912
) {
913913
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
914914
let flags = taskCreateFlags(
@@ -928,7 +928,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
928928
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
929929
public mutating func addTaskUnlessCancelled(
930930
priority: TaskPriority? = nil,
931-
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
931+
operation: sending @escaping () async throws -> ChildTaskResult
932932
) -> Bool {
933933
fatalError("Unavailable in task-to-thread concurrency model")
934934
}
@@ -944,7 +944,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
944944
/// otherwise `false`.
945945
@_alwaysEmitIntoClient
946946
public mutating func addTaskUnlessCancelled(
947-
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
947+
operation: sending @escaping () async throws -> ChildTaskResult
948948
) -> Bool {
949949
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
950950
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)

test/Concurrency/async_task_groups.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify
22
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted
3-
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
4-
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation
3+
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -verify-additional-prefix tns-
54

65
// REQUIRES: concurrency
76
// REQUIRES: asserts
@@ -195,9 +194,17 @@ extension Collection where Self: Sendable, Element: Sendable, Self.Index: Sendab
195194
var submitted = 0
196195

197196
func submitNext() async throws {
197+
// The reason that we emit an error here is b/c we capture the var box
198+
// to i and that is task isolated. This is the region isolation version
199+
// of the 'escaping closure captures non-escaping parameter' error.
200+
//
201+
// TODO: When we have isolation history, isolation history will be able
202+
// to tell us what is going on.
198203
group.addTask { [submitted,i] in // expected-error {{escaping closure captures non-escaping parameter 'transform'}}
199-
let value = try await transform(self[i]) // expected-note {{captured here}}
200-
return SendableTuple2(submitted, value)
204+
// expected-tns-warning @-1 {{task-isolated value of type '() async throws -> SendableTuple2<Int, T>' passed as a strongly transferred parameter}}
205+
let _ = try await transform(self[i]) // expected-note {{captured here}}
206+
let value: T? = nil
207+
return SendableTuple2(submitted, value!)
201208
}
202209
submitted += 1
203210
formIndex(after: &i)

0 commit comments

Comments
 (0)