Skip to content

Commit 6b72c82

Browse files
authored
Merge pull request #38771 from DougGregor/task-group-non-throwing-next-result
2 parents 0fbe171 + 9222bf2 commit 6b72c82

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,9 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
577577
return try await _taskGroupWaitNext(group: _group)
578578
}
579579

580-
/// - SeeAlso: `next()`
581-
public mutating func nextResult() async throws -> Result<ChildTaskResult, Failure>? {
580+
@_silgen_name("$sScg10nextResults0B0Oyxq_GSgyYaKF")
581+
@usableFromInline
582+
mutating func nextResultForABI() async throws -> Result<ChildTaskResult, Failure>? {
582583
do {
583584
guard let success: ChildTaskResult = try await _taskGroupWaitNext(group: _group) else {
584585
return nil
@@ -590,6 +591,12 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
590591
}
591592
}
592593

594+
/// - SeeAlso: `next()`
595+
@_alwaysEmitIntoClient
596+
public mutating func nextResult() async -> Result<ChildTaskResult, Failure>? {
597+
return try! await nextResultForABI()
598+
}
599+
593600
/// Query whether the group has any remaining tasks.
594601
///
595602
/// Task groups are always empty upon entry to the `withTaskGroup` body, and

test/Concurrency/Runtime/async_taskgroup_throw_recover.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func boom() async throws -> Int {
2121
@available(SwiftStdlib 5.5, *)
2222
func test_taskGroup_throws() async {
2323
let got: Int = try await withThrowingTaskGroup(of: Int.self) { group in
24-
group.spawn { try await boom() }
24+
group.addTask { try await boom() }
2525

2626
do {
2727
while let r = try await group.next() {
@@ -33,19 +33,24 @@ func test_taskGroup_throws() async {
3333
let gc = group.isCancelled
3434
print("group cancelled: \(gc)")
3535

36-
group.spawn { () async -> Int in
36+
group.addTask { () async -> Int in
3737
let c = Task.isCancelled
3838
print("task 3 (cancelled: \(c))")
3939
return 3
4040
}
4141

42-
guard let third = try! await group.next() else {
42+
switch await group.nextResult() {
43+
case .success(let third):
44+
print("task group returning normally: \(third)")
45+
return third
46+
47+
case .failure(let error):
48+
fatalError("got an erroneous third result")
49+
50+
case .none:
4351
print("task group failed to get 3")
4452
return 0
4553
}
46-
47-
print("task group returning normally: \(third)")
48-
return third
4954
}
5055

5156
fatalError("Should have thrown and handled inside the catch block")

0 commit comments

Comments
 (0)