-
Notifications
You must be signed in to change notification settings - Fork 138
Adopt typed throws in the implementation of #expect()/#require().
#1336
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
Open
grynspan
wants to merge
11
commits into
main
Choose a base branch
from
jgrynspan/typed-throws-in-expect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
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? { |
Contributor
Author
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adopts typed throws (
throws(E)instead ofthrows) in the#expect()and#require()macros' declarations and in their implementations. In particular, the return type of the implementation functions changes fromResult<R, any Error>toResult<R, ExpectationFailedError>and all implementation functions that take a closure have been updated with an additional genericEparameter.#expect(throws:)and#require(throws:)take two generic error types,EExpectedandEActual. 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 toany Error.The deprecated
#expect {} throws: {}and#require {} throws: {}variants are marked@_unavailableInEmbeddedand continue to useany 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 thanany Error.Checklist: