swiftlang / swift-testing

A modern, expressive testing package for Swift
Apache License 2.0
1.67k stars 68 forks source link

confirmation() does not work from @MainActor tests #622

Closed macguru closed 2 weeks ago

macguru commented 3 weeks ago

Description

The following test function will not compile in the Swift 6 language mode:

@MainActor @Test func example() async {
    await confirmation { _ in } // Error: Sending main actor-isolated value of type '(Confirmation) async -> ()' with later accesses to nonisolated context risks causing data races
}

Expected behavior

I think this should just work. confirmation() should take (and respect) an additional isolation argument isolation: isolated (any Actor)? = #isolation.

Proposed new signature (also discussed here):

func confirmation<R>(
    _ comment: Testing.Comment? = nil,
    expectedCount: Int = 1,
    isolation: isolated (any Actor)? = #isolation,
    sourceLocation: Testing.SourceLocation = #_sourceLocation,
    _ body: (Testing.Confirmation) async throws -> R
) async rethrows -> R

Actual behavior

No response

Steps to reproduce

No response

swift-testing version/commit hash

Xcode 16 beta 5

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx15.0 Darwin Mac.localdomain 24.0.0 Darwin Kernel Version 24.0.0: Wed Aug 7 03:08:56 PDT 2024; root:xnu-11215.1.9~22/RELEASE_ARM64_T6020 arm64

grynspan commented 3 weeks ago

Yes, we need to add an isolation argument. :) Thanks for filing!

grynspan commented 3 weeks ago

Tracked internally as rdar://134375046.