swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.18k stars 10.32k forks source link

Swift-Testing `confirmation` fails to build with `try` nested in `expect`. #75256

Open vanvoorden opened 1 month ago

vanvoorden commented 1 month ago

Description

https://forums.swift.org/t/try-expect-throwing-or-expect-try-throwing/73076/

This is a confusing error-slash-warning when a throwing function is nested in a test expectation (which is then nested in a test confirmation closure).

Reproduction

import Testing

func f() throws -> Bool {
  true
}

@Test func test() async throws {
  try await confirmation { onConfirm in
    #expect(try f())
  }
}

Here is the error:

macro expansion #expect:1:22: error: errors thrown from here are not handled
`- /Users/rick/Desktop/TestingDemo/Tests/TestingDemoTests/TestingDemoTests.swift:9:21: note: expanded code originates here
 7 | @Test func test() async throws {
 8 |   try await confirmation { onConfirm in
 9 |     #expect(try f())
   +--- macro expansion #expect ----------------------------------------
   |1 | Testing.__checkValue(try f(), expression: .__fromSyntaxNode("try f()"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
   |  |                      `- error: errors thrown from here are not handled
   +--------------------------------------------------------------------
10 |   }
11 | }

/Users/rick/Desktop/TestingDemo/Tests/TestingDemoTests/TestingDemoTests.swift:8:3: warning: no calls to throwing functions occur within 'try' expression
 6 | 
 7 | @Test func test() async throws {
 8 |   try await confirmation { onConfirm in
   |   `- warning: no calls to throwing functions occur within 'try' expression
 9 |     #expect(try f())
10 |   }

Here is a workaround:

@Test func testWorkaround() async throws {
  try await confirmation { onConfirm in
    let value = try f()
    #expect(value)
  }
}

Environment

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6) Target: arm64-apple-macosx14.0

vanvoorden commented 1 month ago

cc @grynspan @DougGregor

grynspan commented 1 month ago

I am able to reproduce this failure. confirmation() is rethrows, and it looks like the type checker (rethrows checker?) isn't picking up on the throw in the closure body. Changing to typed throws did not resolve the issue.

grynspan commented 1 week ago

Tracked internally as rdar://133743501.