Skip to content

[6.1] [SWT-0006] Return the thrown error from #expect(throws:) and #require(throws:). #857

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

Conversation

grynspan
Copy link
Contributor

…ire(throws:)`. (#780)

This PR changes the signatures of the various `throws:` overloads of
`#expect` and `#require` so that on success they return the error that
was thrown rather than `Void`. This then allows more ergonomic
inspection of the error's properties:

```swift
let error = try #require(throws: MyError.self) {
  try f()
}
#expect(error.hasWidget)
#expect(error.userName == "John Smith")
```

For more information, see [the proposal
document](https://github.com/swiftlang/swift-testing/blob/jgrynspan/return-errors-from-expect-throws/Documentation/Proposals/0006-return-errors-from-expect-throws.md).

Resolves rdar://138235250.

<details>
<summary>Further PR Details</summary>
It is not possible to overload a macro or function solely by return type
without the compiler reporting `Ambiguous use of 'f()'`, so we are not
able to stage this change in using `@_spi(Experimental)` without
breaking test code that already imports our SPI.

This change is potentially source-breaking for tests that inadvertently
forward the result of these macro invocations to an enclosing scope. For
example, the compiler will start emitting a warning here:

```swift
func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in // ⚠️ Result of call to 'withUnsafePointer(to:_:)' is unused
  #expect(throws: BadFooError.self) {
    try bar(pfoo)
  }
}
```

This warning can be suppressed by assigning the result of `#expect` (or
of `withUnsafePointer(to:_:)`) to `_`:

```swift
func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in
  _ = #expect(throws: BadFooError.self) {
    try bar(pfoo)
  }
}
```

Because `#expect` and `#require` are macros, they cannot be referenced
by name like functions, so you cannot assign them to variables (and then
run into trouble with the types of those variables.)

Finally, this change deprecates the variants of `#expect` and `#require`
that take two closures:

```swift
#expect { // ⚠️ 'expect(_:sourceLocation:performing:throws:)' is deprecated: Examine the result of '#expect(throws:)' instead.
  ...
} throws: { error in
  guard let error = error as? FoodTruckError else {
    return false
  }
  return error.napkinCount == 0
}
```

These variants are no longer needed because you can simply examine the
result of the other variants:

```swift
let error = #expect(throws: FoodTruckError.self) {
  ...
}
#expect(error?.napkinCount == 0)
```
</details>

### 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.
@grynspan grynspan added enhancement New feature or request public-api Affects public API issue-handling Related to Issue handling within the testing library labels Dec 13, 2024
@grynspan grynspan added this to the Swift 6.1 milestone Dec 13, 2024
@grynspan grynspan self-assigned this Dec 13, 2024
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan merged commit 39d9c14 into release/6.1 Dec 13, 2024
3 checks passed
@grynspan grynspan deleted the jgrynspan/return-errors-from-expect-throws-6.1 branch December 13, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request issue-handling Related to Issue handling within the testing library public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants