Skip to content

Make ThrowingTaskGroup.nextResult() non-throwing. #38771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions stdlib/public/Concurrency/TaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,9 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
return try await _taskGroupWaitNext(group: _group)
}

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

/// - SeeAlso: `next()`
@_alwaysEmitIntoClient
public mutating func nextResult() async -> Result<ChildTaskResult, Failure>? {
return try! await nextResultForABI()
}

/// Query whether the group has any remaining tasks.
///
/// Task groups are always empty upon entry to the `withTaskGroup` body, and
Expand Down
17 changes: 11 additions & 6 deletions test/Concurrency/Runtime/async_taskgroup_throw_recover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func boom() async throws -> Int {
@available(SwiftStdlib 5.5, *)
func test_taskGroup_throws() async {
let got: Int = try await withThrowingTaskGroup(of: Int.self) { group in
group.spawn { try await boom() }
group.addTask { try await boom() }

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

group.spawn { () async -> Int in
group.addTask { () async -> Int in
let c = Task.isCancelled
print("task 3 (cancelled: \(c))")
return 3
}

guard let third = try! await group.next() else {
switch await group.nextResult() {
case .success(let third):
print("task group returning normally: \(third)")
return third

case .failure(let error):
fatalError("got an erroneous third result")

case .none:
print("task group failed to get 3")
return 0
}

print("task group returning normally: \(third)")
return third
}

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