forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_sequence_rethrows.swift
87 lines (65 loc) · 2.33 KB
/
async_sequence_rethrows.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify
// REQUIRES: concurrency
// Tests for the use of 'rethrows' on generic functions that have AsyncSequence
// and AsyncIteratorProtocol requirements.
func f1(_ seq:someAsyncSequence)asyncrethrows{
fortryawait_in seq {}
}
func f2(_ seq:someAsyncSequence, body:()throws->Void)asyncrethrows{
fortryawait_in seq {
trybody()
}
}
func f3(_ seq:someAsyncSequence, _ seq2:someAsyncSequence)asyncrethrows{
fortryawait_in seq {
}
fortryawait_in seq2 {
}
}
enumHomeworkError:Error{
case dogAteIt
}
func testCalls(x:someAsyncSequence<Int,Never>, y:anyAsyncSequence<Int,anyError>)asyncthrows{
awaitf1(x)
tryawaitf1(y)
awaitf2(x){print("Hello")}
tryawaitf2(y){print("Hello")}
tryawaitf2(x){throwHomeworkError.dogAteIt }
tryawaitf2(y){throwHomeworkError.dogAteIt }
awaitf3(x, x)
tryawaitf3(x, y)
}
// Treat @rethrows protocols that inherit AsyncSequence like they are
// AsyncSequence for the purpose of rethrowing methods.
@rethrows
protocolInheritsAsyncSequence:AsyncSequence{}
extensionInheritsAsyncSequence{
func blah()asyncrethrows->[Element]{
tryawaitself.reduce(into:[Element]()){ $0.append($1)}
}
}
// Ensure that we can get the thrown error type from next().
structData{}
structErrorSequence<Element, Failure:Error>:AsyncSequence,AsyncIteratorProtocol{
letthrowError:Failure
func makeAsyncIterator()->ErrorSequence<Element,Failure>{
self
}
mutatingfunc next()asyncthrows(Failure)->Element?{
throw throwError
}
}
enumMyError:Error{
case foo
}
func getASequence()->anyAsyncSequence<Data,MyError>{
returnErrorSequence<Data,_>(throwError:MyError.foo) // ERROR: Cannot convert return expression of type 'any Error' to return type 'MyError'
}
// Test the default implementation of next() in terms of next(isolation:).
structAsyncIteratorWithOnlyNextIsolation:AsyncIteratorProtocol{
publicmutatingfunc next(isolation:(anyActor)?)throws(MyError)->Int?{0}
}
// Test the default implementation of next(isolation:) in terms of next().
structAsyncIteratorWithOnlyNext:AsyncIteratorProtocol{
publicmutatingfunc next()throws(MyError)->Int?{0}
}