-
Notifications
You must be signed in to change notification settings - Fork 77
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
[SWT-NNNN] Return the thrown error from #expect(throws:)
and #require(throws:)
.
#780
Open
grynspan
wants to merge
11
commits into
main
Choose a base branch
from
jgrynspan/return-errors-from-expect-throws
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.
+606
−60
Conversation
This file contains 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
grynspan
added
enhancement
New feature or request
public-api
Affects public API
issue-handling
Related to Issue handling within the testing library
labels
Oct 22, 2024
@swift-ci test |
grynspan
added a commit
that referenced
this pull request
Oct 22, 2024
grynspan
force-pushed
the
jgrynspan/return-errors-from-expect-throws
branch
from
October 22, 2024 21:03
e15c39e
to
61854c2
Compare
@swift-ci test |
@swift-ci test |
grynspan
force-pushed
the
jgrynspan/return-errors-from-expect-throws
branch
from
October 23, 2024 14:47
e58a2ce
to
89c147c
Compare
grynspan
changed the title
[DNM] Return the thrown error from
[SWT-NNNN] Return the thrown error from Oct 23, 2024
#expect(throws:)
and #require(throws:)
.#expect(throws:)
and #require(throws:)
.
grynspan
requested review from
stmontgomery,
dennisweissmann,
briancroom,
SeanROlszewski and
suzannaratcliff
as code owners
October 23, 2024 15:48
@swift-ci test |
1 similar comment
@swift-ci test |
Nice! |
grynspan
commented
Oct 24, 2024
Documentation/Proposals/NNNN-return-errors-from-expect-throws.md
Outdated
Show resolved
Hide resolved
grynspan
commented
Oct 24, 2024
Sources/TestingMacros/Support/Additions/MacroExpansionContextAdditions.swift
Show resolved
Hide resolved
@swift-ci test |
grynspan
force-pushed
the
jgrynspan/return-errors-from-expect-throws
branch
2 times, most recently
from
October 28, 2024 12:35
7cfe55b
to
6d4fd83
Compare
@swift-ci test |
@swift-ci test Linux |
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") ``` 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) { 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) { 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.)
…` only throws if you try to cast the result to `any Error` (can't instantiate `Never`), add a way to suppress our diagnostics in our own unit tests.
grynspan
force-pushed
the
jgrynspan/return-errors-from-expect-throws
branch
from
November 2, 2024 17:05
6d4fd83
to
ec1fa5a
Compare
@swift-ci test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
api-proposal
API proposal PRs (documentation only)
enhancement
New feature or request
issue-handling
Related to Issue handling within the testing library
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 changes the signatures of the various
throws:
overloads of#expect
and#require
so that on success they return the error that was thrown rather thanVoid
. This then allows more ergonomic inspection of the error's properties:For more information, see the proposal document.
Resolves rdar://138235250.
Further PR Details
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:
This warning can be suppressed by assigning the result of
#expect
(or ofwithUnsafePointer(to:_:)
) to_
: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:These variants are no longer needed because you can simply examine the result of the other variants:
Checklist: