Skip to content

Commit b99d8f4

Browse files
committed
Add the inference rules for throw error types
1 parent caf42a5 commit b99d8f4

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

TSPL.docc/ReferenceManual/Expressions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,13 +922,13 @@ explicitly marks a closure as throwing or asynchronous.
922922
}
923923
```
924924

925-
If the body of a closure includes a try expression,
925+
If the body of a closure includes a `throws` statement or a `try` expression
926+
that isn't nested inside of a `do` statement with exhaustive error handling,
926927
the closure is understood to be throwing.
927-
<!-- XXX
928-
The same rule to infer the thrown error type
929-
applies to closures as to functions.
930-
-->
931-
Likewise, if it includes an await expression,
928+
If a throwing closure throws only errors of a single type,
929+
the closure is understood as throwing that error type;
930+
otherwise it's understood as throwing `any Error`.
931+
Likewise, if the body includes an `await` expression,
932932
it's understood to be asynchronous.
933933

934934
There are several special forms

TSPL.docc/ReferenceManual/Statements.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,31 @@ do throws(<#type#>) {
889889
}
890890
```
891891

892+
<!-- XXX Is "do throws { }" allowed? -->
893+
892894
If the `do` statement includes a `throws` clause,
893-
the `do` block can throw only errors of the specified type.
894-
The *type* must be a concrete type that conforms to the `Error` protocol,
895+
the `do` block can throw errors of only the specified *type*.
896+
The *type* must be either
897+
a concrete type that conforms to the `Error` protocol,
895898
an opaque type that conforms to the `Error` protocol,
896899
or the boxed protocol type `any Error`.
897900
If the `do` statement doesn't specify the type of error it throws,
898-
that type is implied to be `any Error`.
901+
the error type is inferred as follows:
902+
903+
- If every `throws` statement and `try` expression in the `do` code block
904+
is nested inside of an exhaustive error handling mechanism,
905+
then the `do` statement is inferred as nonthrowing.
906+
907+
- If the `do` code block contains code that throws
908+
errors of only a single type
909+
outside of exhaustive error handling,
910+
then the `do` statement is inferred as throwing that concrete error type.
911+
912+
- If the `do` code block contains code that throws
913+
errors of more than a single type
914+
outside of exhaustive error handling,
915+
then the `do` statement is inferred as throwing `any Error`.
916+
899917
For more information about working with errors that have explicit types,
900918
see <doc:ErrorHandling#Specifying-a-Concrete-Error-Type>.
901919

0 commit comments

Comments
 (0)