Skip to content

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Sep 24, 2025

This PR adopts typed throws (throws(E) instead of throws) in the #expect() and #require() macros' declarations and in their implementations. In particular, the return type of the implementation functions changes from Result<R, any Error> to Result<R, ExpectationFailedError> and all implementation functions that take a closure have been updated with an additional generic E parameter.

#expect(throws:) and #require(throws:) take two generic error types, EExpected and EActual. The former is the error type specified by the test author, while the latter is the actual error type inferred from the body closure. In common usage, both will resolve to any Error.

The deprecated #expect {} throws: {} and #require {} throws: {} variants are marked @_unavailableInEmbedded and continue to use any Error.

If you see throws(any Error) in the diff, take it as meaning that section of the code was audited, but for now we don't have something better to use than any Error.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR adopts typed throws (`throws(E)` instead of `throws`) in the `#expect()`
and `#require()` macros' declarations and in their implementations. In
particular, the return type of the implementation functions changes from
`Result<R, any Error>` to `Result<R, ExpectationFailedError>` and all
implementation functions that take a closure have been updated with an
additional generic `E` parameter.

`#expect(throws:)` and `#require(throws:)` take two generic error types,
`EExpected` and `EActual`. The former is the error type specified by the test
author, while the latter is the actual error type inferred from the body
closure. In common usage, both will resolve to `any Error`.

The deprecated `#expect {} throws: {}` and `#require {} throws: {}` variants are
marked `@_unavailableInEmbedded` and continue to use `any Error`.
@grynspan grynspan added this to the Swift 6.x (main) milestone Sep 24, 2025
@grynspan grynspan self-assigned this Sep 24, 2025
@grynspan grynspan added enhancement New feature or request public-api Affects public API embedded-swift 📟 Embedded Swift issues exit-tests ☠️ Work related to exit tests issue-handling Related to Issue handling within the testing library macros 🔭 Related to Swift macros such as @Test or #expect labels Sep 24, 2025
/// - Warning: This function is used to implement the `#expect()` and
/// `#require()` macros. Do not call it directly.
@discardableResult public func __required<T>() throws -> T where Success == T? {
@discardableResult public func __required<T>() throws(any Error) -> T where Success == T? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use throws(some Error) unfortunately.

(We'll likely need to rewrite this function for Embedded Swift to throw some public-but-opaque error type AKA AnyTestingLibraryError.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
embedded-swift 📟 Embedded Swift issues enhancement New feature or request exit-tests ☠️ Work related to exit tests issue-handling Related to Issue handling within the testing library macros 🔭 Related to Swift macros such as @Test or #expect public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant