Swift-Testing Swift 6 Strict Concurrency Legit Build Failure?

Hi! I'm experimenting with swift-testing and Swift 6 strict concurrency checking. I'm seeing some not-super-clear build failure. Maybe this is something that should work and might be fixed later this year… or maybe it's a legit failure and I should rethink how this test is built.

Here is a test that builds (and passes) from Swift 6 strict concurrency:

func doSomething(_: Int) async throws {
  
}

@Suite actor Tests {
  let count = 0
}

extension Tests {
  @Test func exampleBuilds() async throws {
    let count = 0
    try await doSomething(count)
    await #expect(throws: Never.self) {
      try await doSomething(count)
    }
    try await doSomething(self.count)
  }
}

Here is a test that fails to build from Swift 6 strict concurrency:

extension Tests {
  @Test func exampleFails() async throws {
    await #expect(throws: Never.self) {
      try await doSomething(self.count)
    }
  }
}

Here is the failure:

24 |     await #expect(throws: Never.self) {
25 |       try await doSomething(self.count)
26 |     }
   +--- macro expansion #expect ----------------------------------------
   |1 | Testing.__checkClosureCall(throws: Never.self, performing: {
   |  | `- error: sending 'self'-isolated value of type '() async throws -> ()' with later accesses to nonisolated context risks causing data races
   |2 |       try await doSomething(self.count)
   |3 |     }, expression: .__fromSyntaxNode("try await doSomething(self.count)"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation()).__expected()
   +--------------------------------------------------------------------
27 |   }
28 | }

Any ideas about what else I can do about that? I don't see this error when building from Swift 5 mode. Is this error legit? Should I rethink how I'm building this test? Any ideas about that would be great. Thanks!

Would you please file a GitHub issue against the main Swift repo? This may well be a legitimate problem in Swift Testing, but the diagnostic does not provide enough info for us to fix it if so.

4 Likes