Skip to content

Commit 2fe85ff

Browse files
Incorporate tech review.
Co-authored-by: Doug Gregor <[email protected]>
1 parent a0ee3a9 commit 2fe85ff

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

TSPL.docc/LanguageGuide/ErrorHandling.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ This approach matches the reality that
708708
you don't know ahead of time every error that could happen
709709
while the code is running,
710710
especially when propagating errors thrown somewhere else.
711-
This approach also reflects the fact that errors can change over time.
711+
It also reflects the fact that errors can change over time.
712712
New versions of a library ---
713713
including libraries that your dependencies use ---
714714
can throw new errors,
@@ -729,7 +729,7 @@ in the following special cases:
729729
requires allocating memory at runtime to store the error.
730730
In contrast,
731731
throwing an error of a specific type
732-
lets Swift allocate that memory upfront.
732+
lets Swift avoid heap allocation for errors.
733733

734734
- When the errors are an implementation detail of some unit of code,
735735
like a library,
@@ -740,7 +740,7 @@ in the following special cases:
740740
And because these errors are an implementation detail of the library,
741741
they're always handled within that library.
742742

743-
- In code that throws only errors that were thrown elsewhere,
743+
- In code that only propagates errors described by generic parameters,
744744
like a function that takes a closure argument
745745
and propagates any errors from that closure.
746746
For a comparison between propagating a specific error type
@@ -824,7 +824,18 @@ In this code,
824824
`someThrowingFunction()` propagates any errors that `summarize(_:)` throws.
825825
The errors from `summarize(_:)` are always `StatisticsError` values,
826826
which is also a valid error for `someThrowingFunction()` to throw.
827-
<!-- XXX Expand on subtyping here? -->
827+
828+
Just like you can write a function that never returns
829+
with a return type of `Never`,
830+
you can write a function that never throws with `throws(Never)`:
831+
832+
```swift
833+
func nonThrowingFunction() throws(Never) {
834+
// ...
835+
}
836+
```
837+
This function can't throw because
838+
it's impossible to create a value of type `Never` to throw.
828839

829840
In addition to specifying a function's error type,
830841
you can also write a specific error type for a `do`-`catch` statement.
@@ -859,8 +870,6 @@ and binds the error to a local constant named `error`.
859870
Because the `do`-`catch` statement throws `StatisticsError` values,
860871
`error` is a value of type `StatisticsError`.
861872

862-
<!-- XXX show multiple catch clauses with different patterns? -->
863-
864873
The `catch` clause above uses a `switch` statement
865874
to match and handle each possible error.
866875
If you tried to add a new case to `StatisticsError`

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,12 @@ func f<E: Error>(closure: () throws(E) -> Int) throws(E) -> Int { ... }
16001600
func g(closure: () throws -> Int) rethrows -> Int { ... }
16011601
16021602
map() from the stdlib is another example
1603+
1604+
XXX FROM DOUG
1605+
Part of me wants to suggest that this whole section be revised to replace rethrows entirely with the use of generic typed throws, because that's how I expect the language to go over time. Then, rethrows would become more of a historical anecdote. However, that is a lot of work, and perhaps we'll find that rethrows will remain for longer than I expect. If so, I think it's fine to keep this comparison short: take the someFunction example from the beginning of the section and make it generic over the thrown error type, and note that it's providing a more precise guarantee about what errors it rethrows than rethrows does (because rethrows always produces an any Error).
1606+
XXX END FROM DOUG
1607+
1608+
Filed rdar://128972373
16031609
-->
16041610

16051611
### Asynchronous Functions and Methods

TSPL.docc/ReferenceManual/Statements.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,6 @@ do throws(<#type#>) {
889889
}
890890
```
891891

892-
<!-- XXX Is "do throws { }" allowed? -->
893-
894892
If the `do` statement includes a `throws` clause,
895893
the `do` block can throw errors of only the specified *type*.
896894
The *type* must be
@@ -907,6 +905,7 @@ Swift infers the error type as follows:
907905
- If the `do` code block contains code that throws
908906
errors of only a single type
909907
outside of exhaustive error handling,
908+
other than throwing `Never`,
910909
then Swift infers that the `do` statement throws that concrete error type.
911910

912911
- If the `do` code block contains code that throws

TSPL.docc/ReferenceManual/Types.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ another function that takes and returns an `Int`.
348348

349349
Function types for functions
350350
that can throw or rethrow an error must include the `throws` keyword.
351-
<!-- XXX TR: Confirm rethrowing functions use 'throws' -->
352351
You can include a type after `throws` in parentheses
353352
to specify the type of error that the function throws.
354353
The throw error type must conform to the `Error` protocol.

0 commit comments

Comments
 (0)