File: objc_async_overload.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 (69 lines) | stat: -rw-r--r-- 3,347 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
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk)  -disable-availability-checking -emit-sil -o /dev/null -verify -import-objc-header %S/Inputs/Delegate.h -enable-experimental-feature SendableCompletionHandlers %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk)  -disable-availability-checking -emit-sil -o /dev/null -verify -import-objc-header %S/Inputs/Delegate.h -enable-experimental-feature SendableCompletionHandlers %s -strict-concurrency=targeted
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk)  -disable-availability-checking -emit-sil -o /dev/null -verify -import-objc-header %S/Inputs/Delegate.h -enable-experimental-feature SendableCompletionHandlers %s -strict-concurrency=complete
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk)  -disable-availability-checking -emit-sil -o /dev/null -verify -import-objc-header %S/Inputs/Delegate.h -enable-experimental-feature SendableCompletionHandlers %s -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation

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

// overload resolution should pick sync version in a sync context
func syncContext() {
  let r = Request()
  let d = Delegate()

  // https://github.com/apple/swift/issues/56157
  // This use to trigger an overload resolution error.
  d.makeRequest1(r)

  d.makeRequest2(r)
  d.makeRequest3(r)
}

// overload resolution should pick async version in an async context
func asyncNoAwait() async {
  let r = Request()
  let d = Delegate()
  d.makeRequest1(r) // expected-error@:3 {{expression is 'async' but is not marked with 'await'}} expected-note {{call is 'async'}}
  d.makeRequest2(r) // expected-error@:3 {{expression is 'async' but is not marked with 'await'}} expected-note {{call is 'async'}}
  d.makeRequest3(r) // expected-error@:3 {{expression is 'async' but is not marked with 'await'}} expected-note {{call is 'async'}}
}


func asyncWithAwait() async {
  let r = Request()
  let d = Delegate()
  await d.makeRequest1(r)
  await d.makeRequest2(r)
  await d.makeRequest3(r)
}

// rdar://88703266 - Swift 5 mode should warn, not error, if an imported
// completion handler's implicit `@Sendable` isn't respected.
extension Delegate {
  nonisolated func makeRequest(_ req: Request??, completionHandler: (() -> Void)? = nil) {
    // expected-note@-1 {{parameter 'completionHandler' is implicitly non-sendable}}
    if let req = (req ?? nil) {
      makeRequest1(req, completionHandler: completionHandler)
      // expected-warning@-1 {{passing non-sendable parameter 'completionHandler' to function expecting a @Sendable closure}}
    }
  }
}

@MainActor class C {
  func finish() { }
  // expected-note@-1 {{calls to instance method 'finish()' from outside of its actor context are implicitly asynchronous}}

  func handle(_ req: Request, with delegate: Delegate) {
    delegate.makeRequest1(req) {
      self.finish()
      // expected-warning@-1 {{call to main actor-isolated instance method 'finish()' in a synchronous nonisolated context; this is an error in the Swift 6 language mode}}
    }
  }
}

// rdar://95887113 - Implementing an ObjC category method in Swift is not strictly valid, but should be tolerated

extension Delegate {
  @objc public func makeRequest(fromSwift: Request, completionHandler: (() -> Void)?) {}
}