[SWT-0006] Return the thrown error from #expect(throws:) and #require(throws:).#780
Merged
Merged
Conversation
Contributor
Author
|
@swift-ci test |
grynspan
added a commit
that referenced
this pull request
Oct 22, 2024
e15c39e to
61854c2
Compare
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
e58a2ce to
89c147c
Compare
#expect(throws:) and #require(throws:).#expect(throws:) and #require(throws:).
Contributor
Author
|
@swift-ci test |
1 similar comment
Contributor
Author
|
@swift-ci test |
|
Nice! |
grynspan
commented
Oct 24, 2024
grynspan
commented
Oct 24, 2024
Contributor
Author
|
@swift-ci test |
7cfe55b to
6d4fd83
Compare
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@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.
Contributor
Author
|
@swift-ci test |
76fc544 to
59aee57
Compare
Contributor
Author
|
@swift-ci test |
#expect(throws:) and #require(throws:).#expect(throws:) and #require(throws:).
Contributor
Author
|
@swift-ci test |
stmontgomery
approved these changes
Dec 13, 2024
grynspan
added a commit
that referenced
this pull request
Dec 13, 2024
…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
added a commit
that referenced
this pull request
Dec 13, 2024
…`#require(throws:)`. (#857) - **Explanation**: Change `#expect(throws:)` and `#require(throws:)` to return the thrown error and deprecate the double-closure overloads. - **Scope**: 6.1 release, all tests that use these macros - **Issues**: rdar://138235250 - **Original PRs**: #780 - **Risk**: Low (some developers may get a new diagnostic about an unused return value, but this is a warning unless `-Werror`. - **Testing**: Unit test coverage. - **Reviewers**: @stmontgomery @briancroom
|
Apologies if I'm being completely dumb, but this seems to have gone into the current, Swift 6.0 official docs online even though as far as I can see it's not implemented until Swift 6.1... See https://developer.apple.com/documentation/testing/expect(throws:_:sourcelocation:performing:)-1hfms for example
|
Contributor
Author
|
This is a limitation of Apple's documentation system. See this forum thread for more discussion. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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#expectand#requireso 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
#expectand#requireare 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
#expectand#requirethat take two closures:These variants are no longer needed because you can simply examine the result of the other variants:
Checklist: