Skip to content

Commit fe524c8

Browse files
committed
[Typed throws] Check thrown error type from an async for..in loop
While it is not yet possible for an `AsyncSequence` to specify its thrown error type (so it always throws `any Error`), check that `any Error` is a suitable error type to be thrown in context.
1 parent d4231d3 commit fe524c8

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,15 @@ class ApplyClassifier {
918918

919919
Classification classifyConformance(ProtocolConformanceRef conformanceRef,
920920
EffectKind kind) {
921+
if (conformanceRef.isInvalid())
922+
return Classification::forInvalidCode();
923+
921924
if (conformanceRef.hasEffect(kind)) {
922925
assert(kind == EffectKind::Throws); // there is no async
923926
ASTContext &ctx = conformanceRef.getRequirement()->getASTContext();
924927
// FIXME: typed throws, if it becomes a thing for conformances
925928
return Classification::forThrows(
926-
ctx.getAnyExistentialType(),
929+
ctx.getErrorExistentialType(),
927930
ConditionalEffectKind::Conditional,
928931
PotentialEffectReason::forConformance());
929932
}

test/Concurrency/typed_throws.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature TypedThrows
2+
3+
// REQUIRES: concurrency
4+
5+
enum MyError: Error {
6+
case failed
7+
case epicFailed
8+
}
9+
10+
11+
@available(SwiftStdlib 5.1, *)
12+
func testAsyncFor<S: AsyncSequence>(seq: S) async throws(MyError) {
13+
// expected-error@+1{{thrown expression type 'any Error' cannot be converted to error type 'MyError'}}
14+
for try await _ in seq {
15+
}
16+
}

0 commit comments

Comments
 (0)