Skip to content

Commit 59aee57

Browse files
committed
Incorporate feedback
1 parent c59ee36 commit 59aee57

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

Documentation/Proposals/NNNN-return-errors-from-expect-throws.md

+30-27
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ We offer three variants of `#expect(throws:)`:
2525
error that compares equal to it; and
2626
- One that takes a trailing closure and allows test authors to write arbitrary
2727
validation logic.
28-
28+
2929
The third overload has proven to be somewhat problematic. First, it yields the
3030
error to its closure as an instance of `any Error`, which typically forces the
3131
developer to cast it before doing any useful comparisons. Second, the test
@@ -61,7 +61,7 @@ that were thrown.
6161

6262
## Detailed design
6363

64-
All overloads of `#expect(throws:)` and` #require(throws:)` will be updated to
64+
All overloads of `#expect(throws:)` and `#require(throws:)` will be updated to
6565
return an instance of the error type specified by their arguments, with the
6666
problematic overloads returning `any Error` since more precise type information
6767
is not statically available. The problematic overloads will also be deprecated:
@@ -151,7 +151,7 @@ let error = try #require(throws: PotatoError.self) {
151151

152152
The new code is more concise than the old code and avoids boilerplate casting
153153
from `any Error`.
154-
154+
155155
## Source compatibility
156156

157157
In most cases, this change does not affect source compatibility. Swift does not
@@ -217,31 +217,10 @@ N/A
217217

218218
## Future directions
219219

220-
No specific future directions are indicated.
221-
222-
## Alternatives considered
223-
224-
- Leaving the existing implementation and signatures in place. We've had
225-
sufficient feedback about the ergonomics of this API that we want to address
226-
the problem.
227-
228-
- Having the return type of the macros be `any Error` and returning _any_ error
229-
that was thrown even on mismatch. This would make the ergonomics of the
230-
subsequent test code less optimal because the test author would need to cast
231-
the error to the appropriate type before inspecting it.
232-
233-
There's a philosophical argument to be made here that if a mismatched error is
234-
thrown, then the test has already failed and is in an inconsistent state, so
235-
we should allow the test to fail rather than return what amounts to "bad
236-
output".
237-
238-
If the test author wants to inspect any arbitrary thrown error, they can
239-
specify `(any Error).self` instead of a concrete error type.
240-
241220
- Adopting [typed throws](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0413-typed-throws.md)
242221
to statically require that the error thrown from test code is of the correct
243222
type.
244-
223+
245224
If we adopted typed throws in the signatures of these macros, it would force
246225
adoption of typed throws in the code under test even when it may not be
247226
appropriate. For example, if we adopted typed throws, the following code would
@@ -258,7 +237,31 @@ No specific future directions are indicated.
258237
}
259238
```
260239

240+
We believe it may be possible to overload these macros or their expansions so
241+
that the code sample above _does_ compile and behave as intended. We intend to
242+
experiment further with this idea and potentially revisit typed throws support
243+
in a future proposal.
244+
245+
## Alternatives considered
246+
247+
- Leaving the existing implementation and signatures in place. We've had
248+
sufficient feedback about the ergonomics of this API that we want to address
249+
the problem.
250+
251+
- Having the return type of the macros be `any Error` and returning _any_ error
252+
that was thrown even on mismatch. This would make the ergonomics of the
253+
subsequent test code less optimal because the test author would need to cast
254+
the error to the appropriate type before inspecting it.
255+
256+
There's a philosophical argument to be made here that if a mismatched error is
257+
thrown, then the test has already failed and is in an inconsistent state, so
258+
we should allow the test to fail rather than return what amounts to "bad
259+
output".
260+
261+
If the test author wants to inspect any arbitrary thrown error, they can
262+
specify `(any Error).self` instead of a concrete error type.
263+
261264
## Acknowledgments
262265

263-
Thanks to the team and to @jakepetroules for starting the discussion that
264-
ultimately led to this proposal.
266+
Thanks to the team and to [@jakepetroules](https://github.com/jakepetroules) for
267+
starting the discussion that ultimately led to this proposal.

Sources/Testing/Testing.docc/AvailabilityStubs/ExpectComplexThrows.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<!--
44
This source file is part of the Swift.org open source project
55
6-
Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
Copyright (c) 2024 Apple Inc. and the Swift project authors
77
Licensed under Apache License v2.0 with Runtime Library Exception
88
99
See https://swift.org/LICENSE.txt for license information

Sources/Testing/Testing.docc/AvailabilityStubs/RequireComplexThrows.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<!--
44
This source file is part of the Swift.org open source project
55
6-
Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
Copyright (c) 2024 Apple Inc. and the Swift project authors
77
Licensed under Apache License v2.0 with Runtime Library Exception
88
99
See https://swift.org/LICENSE.txt for license information

0 commit comments

Comments
 (0)