File: objc_async.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (409 lines) | stat: -rw-r--r-- 13,430 bytes parent folder | download
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/custom-modules %s -verify -verify-additional-file %swift_src_root/test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h -strict-concurrency=targeted -parse-as-library -enable-experimental-feature SendableCompletionHandlers

// REQUIRES: objc_interop
// REQUIRES: concurrency
// REQUIRES: asserts

import Foundation
import ObjCConcurrency
// expected-warning@-1{{add '@preconcurrency' to suppress 'Sendable'-related warnings from module 'ObjCConcurrency'}}

@available(SwiftStdlib 5.5, *)
@MainActor func onlyOnMainActor() { }

@available(SwiftStdlib 5.5, *)
func testSlowServer(slowServer: SlowServer) async throws {
  let _: Int = await slowServer.doSomethingSlow("mail")
  let _: Bool = await slowServer.checkAvailability()
  let _: String = try await slowServer.findAnswer()
  let _: String = try await slowServer.findAnswerFailingly()

  let (aOpt, b) = try await slowServer.findQAndA()
  if let a = aOpt { // make sure aOpt is optional
    print(a)
  }
  let _: String = b // make sure b is non-optional

  let _: String = try await slowServer.findAnswer()

  let _: Void = await slowServer.doSomethingFun("jump")
  let _: (Int) -> Void = slowServer.completionHandler

  // async version
  let _: Int = await slowServer.doSomethingConflicted("thinking")

  // still async version...
  let _: Int = slowServer.doSomethingConflicted("thinking")
  // expected-error@-1{{expression is 'async' but is not marked with 'await'}}{{16-16=await }}
  // expected-note@-2{{call is 'async'}}

  let _: String? = try await slowServer.fortune()
  let _: Int = try await slowServer.magicNumber(withSeed: 42)

  await slowServer.serverRestart("localhost")
  await slowServer.serverRestart("localhost", atPriority: 0.8)

  _ = await slowServer.allOperations()

  let _: Int = await slowServer.bestName("hello")
  let _: Int = await slowServer.customize("hello")

  slowServer.unavailableMethod() // expected-warning{{instance method 'unavailableMethod' is unavailable from asynchronous contexts}}
  slowServer.unavailableMethodWithMessage() // expected-warning{{instance method 'unavailableMethodWithMessage' is unavailable from asynchronous contexts; Blarpy!}}

  let _: String = await slowServer.dance("slide")
  let _: String = await slowServer.__leap(17)

  slowServer.repeatTrick("jump") // expected-error{{missing argument for parameter 'completionHandler' in call}}

  _ = try await slowServer.someAsyncMethod()


  _ = await slowServer.operations()

  _ = await slowServer.runOnMainThread()
}

@available(SwiftStdlib 5.5, *)
func testSlowServerSynchronous(slowServer: SlowServer) {
  // synchronous version
  let _: Int = slowServer.doSomethingConflicted("thinking")
  slowServer.poorlyNamed("hello") { (i: Int) in print(i) }
  slowServer.customize(with: "hello") { (i: Int) in print(i) }

  slowServer.dance("jig") { s in print(s + "") }
  slowServer.leap(17) { s in print(s + "") }
  slowServer.repeatTrick("jump") { i in print(i + 1) }

  let s = slowServer.operations
  _ = s + []

  slowServer.runOnMainThread { s in
    print(s)
    onlyOnMainActor() // okay because runOnMainThread has a @MainActor closure
  }

  slowServer.overridableButRunsOnMainThread { s in
    print(s)
    onlyOnMainActor() // okay because parameter is @MainActor
  }

  let _: Int = slowServer.overridableButRunsOnMainThread // expected-error{{cannot convert value of type '(((String) -> Void)?) -> Void' to specified type 'Int'}}
}

@available(SwiftStdlib 5.5, *)
func testSlowServerOldSchool(slowServer: SlowServer) {
  slowServer.doSomethingSlow("mail") { i in
    _ = i
  }

  _ = slowServer.allOperations
}

@available(SwiftStdlib 5.5, *)
func testSendable(fn: () -> Void) {
  doSomethingConcurrently(fn) // okay, due to implicit @preconcurrency
  doSomethingConcurrentlyButUnsafe(fn) // okay, @Sendable not part of the type

  var x = 17
  doSomethingConcurrently {
    print(x)
    x = x + 1
  }
}

@available(SwiftStdlib 5.5, *)
func testSendableInAsync() async {
  var x = 17
  doSomethingConcurrentlyButUnsafe {
    x = 42 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
  }
  print(x)
}

@available(SwiftStdlib 5.5, *)
func testSendableAttrs(
  sendableClass: SendableClass, nonSendableClass: NonSendableClass,
  sendableEnum: SendableEnum, nonSendableEnum: NonSendableEnum,
  sendableOptions: SendableOptions, nonSendableOptions: NonSendableOptions,
  sendableError: SendableError, nonSendableError: NonSendableError,
  sendableStringEnum: SendableStringEnum, nonSendableStringEnum: NonSendableStringEnum,
  sendableStringStruct: SendableStringStruct, nonSendableStringStruct: NonSendableStringStruct
) async {
  func takesSendable<T: Sendable>(_: T) {}

  takesSendable(sendableClass)        // no-error
  takesSendable(nonSendableClass)     // expected-warning{{conformance of 'NonSendableClass' to 'Sendable' is unavailable}}

  doSomethingConcurrently {
    print(sendableClass)               // no-error
    print(nonSendableClass)            // expected-warning{{capture of 'nonSendableClass' with non-sendable type 'NonSendableClass' in a `@Sendable` closure}}

    print(sendableEnum)                // no-error
    print(nonSendableEnum)             // expected-warning{{capture of 'nonSendableEnum' with non-sendable type 'NonSendableEnum' in a `@Sendable` closure}}

    print(sendableOptions)             // no-error
    print(nonSendableOptions)          // expected-warning{{capture of 'nonSendableOptions' with non-sendable type 'NonSendableOptions' in a `@Sendable` closure}}

    print(sendableError)               // no-error
    print(nonSendableError)            // no-error--we don't respect `@_nonSendable` on `ns_error_domain` types because all errors are Sendable

    print(sendableStringEnum)          // no-error
    print(nonSendableStringEnum)       // expected-warning{{capture of 'nonSendableStringEnum' with non-sendable type 'NonSendableStringEnum' in a `@Sendable` closure}}

    print(sendableStringStruct)        // no-error
    print(nonSendableStringStruct)     // expected-warning{{capture of 'nonSendableStringStruct' with non-sendable type 'NonSendableStringStruct' in a `@Sendable` closure}}
  }
}

// Check import of attributes
@available(SwiftStdlib 5.5, *)
func globalAsync() async { }

@available(SwiftStdlib 5.5, *)
actor MySubclassCheckingSwiftAttributes : ProtocolWithSwiftAttributes {
  func syncMethod() { } // expected-note 2{{calls to instance method 'syncMethod()' from outside of its actor context are implicitly asynchronous}}

  nonisolated func independentMethod() {
    syncMethod() // expected-error{{call to actor-isolated instance method 'syncMethod()' in a synchronous nonisolated context}}
  }

  nonisolated func nonisolatedMethod() {
  }

  @MainActor func mainActorMethod() {
    syncMethod() // expected-error{{call to actor-isolated instance method 'syncMethod()' in a synchronous main actor-isolated context}}
  }

  @MainActor func uiActorMethod() { }
}

// Sendable conformance inference for imported types.
func acceptCV<T: Sendable>(_: T) { }

struct MyStruct: Sendable {
  var range: NSRange
  var inner: SendableStructWithNonSendable
}

@available(SwiftStdlib 5.5, *)
func testCV(r: NSRange, someStruct: SendableStructWithNonSendable) async {
  acceptCV(r)
  acceptCV(someStruct)
}

// Global actor (unsafe) isolation.

@available(SwiftStdlib 5.5, *)
actor SomeActor { }

@available(SwiftStdlib 5.5, *)
@globalActor
struct SomeGlobalActor {
  static let shared = SomeActor()
}

@available(SwiftStdlib 5.5, *)
class MyButton : NXButton {
  @MainActor func testMain() {
    onButtonPress() // okay
  }

  @SomeGlobalActor func testOther() {
    onButtonPress() // expected-warning{{call to main actor-isolated instance method 'onButtonPress()' in a synchronous global actor 'SomeGlobalActor'-isolated context}}
  }

  func test() {
    onButtonPress() // okay
  }
}

@available(SwiftStdlib 5.5, *)
func testButtons(mb: MyButton) {
  mb.onButtonPress()
}

@available(SwiftStdlib 5.5, *)
func testMirrored(instance: ClassWithAsync) async {
  await instance.instanceAsync()
  await instance.protocolMethod()
  await instance.customAsyncName()
}

@available(SwiftStdlib 5.5, *)
@MainActor class MyToolbarButton : NXButton {
  var count = 5

  func f() {
    Task {
      let c = count
      print(c)
    }
  }
}

@available(SwiftStdlib 5.5, *)
@MainActor class MyView: NXView {
  func f() {
    Task {
      await self.g()
    }
  }

  func g() async { }
}



@available(SwiftStdlib 5.5, *)
@MainActor func mainActorFn() {}
@available(SwiftStdlib 5.5, *)
@SomeGlobalActor func sgActorFn() {}

// rdar://87217618
// https://github.com/apple/swift/issues/57973
// Check inferred isolation for overridden decls from ObjC. Note that even if
// the override is not present, it can have an affect.
@MainActor
@available(SwiftStdlib 5.5, *)
class FooFrame: PictureFrame {
  init() {
    super.init(size: 0)
  }

  override init(size n: Int) {
    super.init(size: n)
  }

  override func rotate() {
    mainActorFn()
  }
}

@available(SwiftStdlib 5.5, *)
class BarFrame: PictureFrame {
  init() {
    super.init(size: 0)
  }

  override init(size n: Int) {
    super.init(size: n)
  }

  override func rotate() {
    mainActorFn()
  }
}

@available(SwiftStdlib 5.5, *)
@SomeGlobalActor
class BazFrame: NotIsolatedPictureFrame {
// expected-note@-1 2 {{class 'BazFrame' does not conform to the 'Sendable' protocol}}
  init() {
    super.init(size: 0)
  }

  override init(size n: Int) {
    super.init(size: n)
  }

  override func rotate() {
    sgActorFn()
  }
}

@available(SwiftStdlib 5.5, *)
@SomeGlobalActor
class BazFrameIso: PictureFrame { // expected-error {{global actor 'SomeGlobalActor'-isolated class 'BazFrameIso' has different actor isolation from main actor-isolated superclass 'PictureFrame'}}
}

@available(SwiftStdlib 5.5, *)
func check() async {
  _ = await BarFrame()
  _ = await FooFrame()
  _ = await BazFrame()
  // expected-warning@-1 {{non-sendable type 'BazFrame' returned by call to global actor 'SomeGlobalActor'-isolated function cannot cross actor boundary; this is an error in the Swift 6 language mode}}

  _ = await BarFrame(size: 0)
  _ = await FooFrame(size: 0)
  _ = await BazFrame(size: 0)
  // expected-warning@-1 {{non-sendable type 'BazFrame' returned by call to global actor 'SomeGlobalActor'-isolated function cannot cross actor boundary; this is an error in the Swift 6 language mode}}
}

@available(SwiftStdlib 5.5, *)
func testSender(
  sender: NXSender,
  sendableObject: SendableClass,
  nonSendableObject: NonSendableClass,
  sendableSubclassOfNonSendableObject: NonSendableClass & Sendable,
  sendableProtos: LabellyProtocol & ObjCClub & Sendable,
  nonSendableProtos: LabellyProtocol & ObjCClub,
  sendableGeneric: GenericObject<SendableClass> & Sendable,
  nonSendableGeneric: GenericObject<SendableClass>,
  ptr: UnsafeMutableRawPointer,
  stringArray: [String]
) async {
  sender.sendAny(sendableObject)
  sender.sendAny(nonSendableObject)
  // expected-warning@-1 {{conformance of 'NonSendableClass' to 'Sendable' is unavailable}}

  sender.sendOptionalAny(sendableObject)
  sender.sendOptionalAny(nonSendableObject)
  // expected-warning@-1 {{conformance of 'NonSendableClass' to 'Sendable' is unavailable}}

  sender.sendSendable(sendableObject)

  sender.sendSendableSubclasses(nonSendableObject)
  // expected-warning@-1 {{conformance of 'NonSendableClass' to 'Sendable' is unavailable}}
  sender.sendSendableSubclasses(sendableSubclassOfNonSendableObject)

  sender.sendProto(sendableProtos)
  sender.sendProto(nonSendableProtos)
  // expected-warning@-1 {{type 'any LabellyProtocol & ObjCClub' does not conform to the 'Sendable' protocol}}

  sender.sendProtos(sendableProtos)
  sender.sendProtos(nonSendableProtos)
  // expected-warning@-1 {{type 'any LabellyProtocol & ObjCClub' does not conform to the 'Sendable' protocol}}

  sender.sendAnyArray([sendableObject])
  sender.sendAnyArray([nonSendableObject])
  // expected-warning@-1 {{conformance of 'NonSendableClass' to 'Sendable' is unavailable; this is an error in the Swift 6 language mode}}

  sender.sendGeneric(sendableGeneric) // no warning

  sender.sendGeneric(nonSendableGeneric)
  // expected-warning@-1 {{type 'GenericObject<SendableClass>' does not conform to the 'Sendable' protocol}}

  sender.sendPtr(ptr)
  sender.sendStringArray(stringArray)
}

// Sendable checking
public struct SomeWrapper<T: AuditedNonSendable> {
  public let unit: T
}

extension SomeWrapper: Sendable where T: Sendable {}


// rdar://96830159
@available(SwiftStdlib 5.5, *)
@MainActor class SendableCompletionHandler {
  var isolatedThing: [String] = []
  // expected-note@-1 {{property declared here}}

  func makeCall(slowServer: SlowServer) {
    slowServer.doSomethingSlow("churn butter") { (_ : Int) in
      let _ = self.isolatedThing
      // expected-warning@-1 {{main actor-isolated property 'isolatedThing' can not be referenced from a Sendable closure; this is an error in the Swift 6 language mode}}
    }
  }
}

// rdar://97646309 -- lookup and direct call of an optional global-actor constrained method would crash in SILGen
@available(SwiftStdlib 5.5, *)
extension CoffeeDelegate  {
    @MainActor func test() async -> (NSObject?, NSObject, NSObject) {
        return await self.icedMochaServiceGenerateMocha!(NSObject())
    }
}