-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathasync_task_locals_basic.swift
271 lines (236 loc) · 9.04 KB
/
async_task_locals_basic.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// RUN: %target-run-simple-swift( -plugin-path %swift-plugin-dir -parse-as-library %import-libdispatch) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
final class StringLike: Sendable, ExpressibleByStringLiteral, CustomStringConvertible {
let value: String
init(_ value: String) {
self.value = value
}
init(stringLiteral value: StringLiteralType) {
self.value = value
}
var description: String { value }
}
@available(SwiftStdlib 5.1, *)
enum TL {
@TaskLocal
static var string: String = "<undefined>"
@TaskLocal
static var number: Int = 0
@TaskLocal
static var never: StringLike = StringLike("<never>")
@TaskLocal
static var clazz: ClassTaskLocal?
@TaskLocal
static var force: ForceUnwrapMe!
}
@TaskLocal
@available(SwiftStdlib 5.1, *)
var globalTaskLocal: StringLike = StringLike("<not-set>")
@available(SwiftStdlib 5.10, *)
struct LessAvailable {}
struct ForceUnwrapMe {}
@TaskLocal
@available(SwiftStdlib 5.10, *)
var globalLessAvailable: LessAvailable?
@available(SwiftStdlib 5.1, *)
final class ClassTaskLocal: Sendable {
init() {
print("clazz init \(ObjectIdentifier(self))")
}
deinit {
print("clazz deinit \(ObjectIdentifier(self))")
}
}
@available(SwiftStdlib 5.1, *)
@discardableResult
func printTaskLocalAsync<V>(
_ key: TaskLocal<V>,
_ expected: V? = nil,
file: String = #file, line: UInt = #line
) async -> V? {
printTaskLocal(key, expected, file: file, line: line)
}
@available(SwiftStdlib 5.1, *)
@discardableResult
func printTaskLocal<V>(
_ key: TaskLocal<V>,
_ expected: V? = nil,
file: String = #file, line: UInt = #line
) -> V? {
let value = key.get()
print("\(key) (\(value)) at \(file):\(line)")
if let expected = expected {
assert("\(expected)" == "\(value)",
"Expected [\(expected)] but found: \(value), at \(file):\(line)")
}
return expected
}
// ==== ------------------------------------------------------------------------
@available(SwiftStdlib 5.1, *)
func simple() async {
printTaskLocal(TL.$number) // CHECK: TaskLocal<Int>(defaultValue: 0) (0)
TL.$number.withValue(1) {
printTaskLocal(TL.$number) // CHECK-NEXT: TaskLocal<Int>(defaultValue: 0) (1)
}
// async body
await TL.$number.withValue(1) {
await Task.yield()
print("OK: \(TL.number)")
}
// sync body
TL.$number.withValue(1) {
print("OK: \(TL.number)")
}
}
@available(SwiftStdlib 5.1, *)
func simple_deinit() async {
TL.$clazz.withValue(ClassTaskLocal()) {
// CHECK: clazz init [[C:.*]]
printTaskLocal(TL.$clazz) // CHECK: TaskLocal<Optional<ClassTaskLocal>>(defaultValue: nil) (Optional(main.ClassTaskLocal))
}
// CHECK: clazz deinit [[C]]
printTaskLocal(TL.$clazz) // CHECK: TaskLocal<Optional<ClassTaskLocal>>(defaultValue: nil) (nil)
}
struct Boom: Error {
let value: String
}
@available(SwiftStdlib 5.1, *)
func simple_throw() async {
do {
try TL.$clazz.withValue(ClassTaskLocal()) {
throw Boom(value: "oh no!")
}
} catch {
//CHECK: error: Boom(value: "oh no!")
print("error: \(error)")
}
}
@available(SwiftStdlib 5.1, *)
func nested() async {
printTaskLocal(TL.$string) // CHECK: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
TL.$string.withValue("hello") {
printTaskLocal(TL.$number) // CHECK-NEXT: TaskLocal<Int>(defaultValue: 0) (0)
printTaskLocal(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (hello)
TL.$number.withValue(2) {
printTaskLocal(TL.$number) // CHECK-NEXT: TaskLocal<Int>(defaultValue: 0) (2)
printTaskLocal(TL.$string, "hello") // CHECK: TaskLocal<String>(defaultValue: <undefined>) (hello)
}
printTaskLocal(TL.$number) // CHECK-NEXT: TaskLocal<Int>(defaultValue: 0) (0)
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (hello)
}
printTaskLocal(TL.$number) // CHECK-NEXT: TaskLocal<Int>(defaultValue: 0) (0)
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
}
@available(SwiftStdlib 5.1, *)
func nested_allContribute() async {
printTaskLocal(TL.$string) // CHECK: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
TL.$string.withValue("one") {
printTaskLocal(TL.$string, "one")// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
TL.$string.withValue("two") {
printTaskLocal(TL.$string, "two") // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (two)
TL.$string.withValue("three") {
printTaskLocal(TL.$string, "three") // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (three)
}
printTaskLocal(TL.$string, "two") // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (two)
}
printTaskLocal(TL.$string, "one")// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
}
@available(SwiftStdlib 5.1, *)
func nested_3_onlyTopContributes() async {
printTaskLocal(TL.$string) // CHECK: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
TL.$string.withValue("one") {
printTaskLocal(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
TL.$number.withValue(2) {
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
TL.$number.withValue(3) {
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
printTaskLocal(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
}
@available(SwiftStdlib 5.1, *)
func nested_3_onlyTopContributesAsync() async {
await printTaskLocalAsync(TL.$string) // CHECK: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
await TL.$string.withValue("one") {
await printTaskLocalAsync(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
await TL.$number.withValue(2) {
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
await TL.$number.withValue(3) {
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
}
@available(SwiftStdlib 5.1, *)
func nested_3_onlyTopContributesMixed() async {
await printTaskLocalAsync(TL.$string) // CHECK: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
await TL.$string.withValue("one") {
await printTaskLocalAsync(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
await TL.$number.withValue(2) {
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
TL.$number.withValue(3) {
printTaskLocal(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string)// CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (one)
}
await printTaskLocalAsync(TL.$string) // CHECK-NEXT: TaskLocal<String>(defaultValue: <undefined>) (<undefined>)
}
@available(SwiftStdlib 5.1, *)
func withLocal_body_mustNotEscape() async {
var something = "Nice"
TL.$string.withValue("xxx") {
something = "very nice"
}
_ = something // silence not used warning
}
@available(SwiftStdlib 5.1, *)
actor Worker {
@TaskLocal
static var declaredInActor: String = "<default-value>"
func setAndRead() async {
print("setAndRead") // CHECK: setAndRead
await Worker.$declaredInActor.withValue("value-1") {
await printTaskLocalAsync(Worker.$declaredInActor) // CHECK-NEXT: TaskLocal<String>(defaultValue: <default-value>) (value-1)
}
}
}
@available(SwiftStdlib 5.1, *)
func inside_actor() async {
await Worker().setAndRead()
}
@available(SwiftStdlib 5.1, *)
func global_task_local() async {
await $globalTaskLocal.withValue("value-1") {
await printTaskLocalAsync($globalTaskLocal) // CHECK-NEXT: TaskLocal<StringLike>(defaultValue: <not-set>) (value-1)
}
}
@available(SwiftStdlib 5.1, *)
@main struct Main {
static func main() async {
await simple()
await simple_deinit()
await simple_throw()
await nested()
await nested_allContribute()
await nested_3_onlyTopContributes()
await nested_3_onlyTopContributesAsync()
await nested_3_onlyTopContributesMixed()
await inside_actor()
await global_task_local()
}
}