Skip to content

Commit 3ad851e

Browse files
authored
Simplify usages of withTaskGroup to infer ChildTaskResult type where possible (#1102)
This adjusts usages of `withTaskGroup` and `withThrowingTaskGroup` to take advantage of [SE-0442: Allow TaskGroup's ChildTaskResult Type To Be Inferred](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0442-allow-taskgroup-childtaskresult-type-to-be-inferred.md) by inferring the child task result type. I successfully built this PR using a Swift 6.1 toolchain. A couple usages I _did_ need to leave explicitly specified, but most I was able to simplify. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 91f7889 commit 3ad851e

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

Sources/Testing/Running/Runner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ extension Runner {
156156
in sequence: some Sequence<E>,
157157
_ body: @Sendable @escaping (E) async throws -> Void
158158
) async throws where E: Sendable {
159-
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
159+
try await withThrowingTaskGroup { taskGroup in
160160
for element in sequence {
161161
// Each element gets its own subtask to run in.
162162
_ = taskGroup.addTaskUnlessCancelled {
@@ -430,7 +430,7 @@ extension Runner {
430430
Event.post(.iterationEnded(iterationIndex), for: (nil, nil), configuration: runner.configuration)
431431
}
432432

433-
await withTaskGroup(of: Void.self) { [runner] taskGroup in
433+
await withTaskGroup { [runner] taskGroup in
434434
_ = taskGroup.addTaskUnlessCancelled {
435435
try? await _runStep(atRootOf: runner.plan.stepGraph)
436436
}

Sources/Testing/Test+Discovery.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension Test {
8484
// a task group and collate their results.
8585
if useNewMode {
8686
let generators = Generator.allTestContentRecords().lazy.compactMap { $0.load() }
87-
await withTaskGroup(of: Self.self) { taskGroup in
87+
await withTaskGroup { taskGroup in
8888
for generator in generators {
8989
taskGroup.addTask { await generator.rawValue() }
9090
}
@@ -96,7 +96,7 @@ extension Test {
9696
// Perform legacy test discovery if needed.
9797
if useLegacyMode && result.isEmpty {
9898
let generators = Generator.allTypeMetadataBasedTestContentRecords().lazy.compactMap { $0.load() }
99-
await withTaskGroup(of: Self.self) { taskGroup in
99+
await withTaskGroup { taskGroup in
100100
for generator in generators {
101101
taskGroup.addTask { await generator.rawValue() }
102102
}

Sources/Testing/Traits/TimeLimitTrait.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func withTimeLimit(
264264
_ body: @escaping @Sendable () async throws -> Void,
265265
timeoutHandler: @escaping @Sendable () -> Void
266266
) async throws {
267-
try await withThrowingTaskGroup(of: Void.self) { group in
267+
try await withThrowingTaskGroup { group in
268268
group.addTask {
269269
// If sleep() returns instead of throwing a CancellationError, that means
270270
// the timeout was reached before this task could be cancelled, so call

Tests/TestingTests/Support/CartesianProductTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct CartesianProductTests {
9696
// Test that the product can be iterated multiple times concurrently.
9797
let (_, _, product) = computeCartesianProduct()
9898
let expectedSum = product.reduce(into: 0) { $0 &+= $1.1 }
99-
await withTaskGroup(of: Int.self) { taskGroup in
99+
await withTaskGroup { taskGroup in
100100
for _ in 0 ..< 10 {
101101
taskGroup.addTask {
102102
product.reduce(into: 0) { $0 &+= $1.1 }

Tests/TestingTests/Support/LockTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct LockTests {
3636
@Test("No lock")
3737
func noLock() async {
3838
let lock = LockedWith<Never, Int>(rawValue: 0)
39-
await withTaskGroup(of: Void.self) { taskGroup in
39+
await withTaskGroup { taskGroup in
4040
for _ in 0 ..< 100_000 {
4141
taskGroup.addTask {
4242
lock.increment()

Tests/TestingTests/Traits/TimeLimitTraitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ struct TimeLimitTraitTests {
181181
@Test("Cancelled tests can exit early (cancellation checking works)")
182182
func cancelledTestExitsEarly() async throws {
183183
let timeAwaited = await Test.Clock().measure {
184-
await withTaskGroup(of: Void.self) { taskGroup in
184+
await withTaskGroup { taskGroup in
185185
taskGroup.addTask {
186186
await Test {
187187
try await Test.Clock.sleep(for: .seconds(60) * 60)

0 commit comments

Comments
 (0)