File: 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 (182 lines) | stat: -rw-r--r-- 5,282 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
// RUN: %target-typecheck-verify-swift 

// REQUIRES: concurrency

@available(SwiftStdlib 5.5, *)
func doAsynchronously() async { }
@available(SwiftStdlib 5.5, *)
func doSynchronously() { }

@available(SwiftStdlib 5.5, *)
func testConversions() async {
  let _: () -> Void = doAsynchronously // expected-error{{invalid conversion from 'async' function of type '() async -> ()' to synchronous function type '() -> Void'}}
  let _: () async -> Void = doSynchronously // okay
}

// Overloading
@available(SwiftStdlib 5.5, *)
@available(swift, deprecated: 4.0, message: "synchronous is no fun")
func overloadedSame(_: Int = 0) -> String { "synchronous" }

@available(SwiftStdlib 5.5, *)
func overloadedSame() async -> String { "asynchronous" }

@available(SwiftStdlib 5.5, *)
func overloaded() -> String { "synchronous" }
@available(SwiftStdlib 5.5, *)
func overloaded() async -> Double { 3.14159 }

@available(SwiftStdlib 5.5, *)
@available(swift, deprecated: 4.0, message: "synchronous is no fun")
func overloadedOptDifference() -> String { "synchronous" }

@available(SwiftStdlib 5.5, *)
func overloadedOptDifference() async -> String? { nil }

@available(SwiftStdlib 5.5, *)
func testOverloadedSync() {
  _ = overloadedSame() // expected-warning{{synchronous is no fun}}

  let _: String? = overloadedOptDifference() // expected-warning{{synchronous is no fun}}

  let _ = overloaded()
  let fn = {
    overloaded()
  }
  let _: Int = fn // expected-error{{value of type '() -> String'}}

  let fn2 = {
    print("fn2")
    _ = overloaded()
  }
  let _: Int = fn2 // expected-error{{value of type '() -> ()'}}

  let fn3 = {
    await overloaded()
  }
  let _: Int = fn3 // expected-error{{value of type '() async -> Double'}}

  let fn4 = {
    print("fn2")
    _ = await overloaded()
  }
  let _: Int = fn4 // expected-error{{value of type '() async -> ()'}}
}

@available(SwiftStdlib 5.5, *)
func testOverloadedAsync() async {
  _ = await overloadedSame() // no warning

  let _: String? = await overloadedOptDifference() // no warning

  let _ = await overloaded()
  let _ = overloaded()
  // expected-error@-1:11{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
  // expected-note@-2:11{{call is 'async'}}


  let fn = {
    overloaded()
  }
  let _: Int = fn // expected-error{{value of type '() -> String'}}

  let fn2 = {
    print("fn2")
    _ = overloaded()
  }
  let _: Int = fn2 // expected-error{{value of type '() -> ()'}}

  let fn3 = {
    await overloaded()
  }
  let _: Int = fn3 // expected-error{{value of type '() async -> Double'}}

  let fn4 = {
    print("fn2")
    _ = await overloaded()
  }
  let _: Int = fn4 // expected-error{{value of type '() async -> ()'}}
}

@available(SwiftStdlib 5.5, *)
func takesAsyncClosure(_ closure: () async -> String) -> Int { 0 }
@available(SwiftStdlib 5.5, *)
func takesAsyncClosure(_ closure: () -> String) -> String { "" }

@available(SwiftStdlib 5.5, *)
func testPassAsyncClosure() {
  let a = takesAsyncClosure { await overloadedSame() }
  let _: Double = a // expected-error{{convert value of type 'Int'}}

  let b = takesAsyncClosure { overloadedSame() } // expected-warning{{synchronous is no fun}}
  let _: Double = b // expected-error{{convert value of type 'String'}}
}

@available(SwiftStdlib 5.5, *)
struct FunctionTypes {
  var syncNonThrowing: () -> Void
  var syncThrowing: () throws -> Void
  var asyncNonThrowing: () async -> Void
  var asyncThrowing: () async throws -> Void

  mutating func demonstrateConversions() {
    // Okay to add 'async' and/or 'throws'
    asyncNonThrowing = syncNonThrowing
    asyncThrowing = syncThrowing
    syncThrowing = syncNonThrowing
    asyncThrowing = asyncNonThrowing

    // Error to remove 'async' or 'throws'
    syncNonThrowing = asyncNonThrowing // expected-error{{invalid conversion}}
    syncThrowing = asyncThrowing       // expected-error{{invalid conversion}}
    syncNonThrowing = syncThrowing     // expected-error{{invalid conversion}}
    asyncNonThrowing = syncThrowing    // expected-error{{invalid conversion}}
  }
}

// Overloading when there is conversion from sync to async.
@available(SwiftStdlib 5.5, *)
func bar(_ f: (Int) -> Int) -> Int {
  return f(2)
}

@available(SwiftStdlib 5.5, *)
func bar(_ f: (Int) async -> Int) async -> Int {
  return await f(2)
}

@available(SwiftStdlib 5.5, *)
func incrementSync(_ x: Int) -> Int {
  return x + 1
}

@available(SwiftStdlib 5.5, *)
func incrementAsync(_ x: Int) async -> Int {
  return x + 1
}

@available(SwiftStdlib 5.5, *)
func testAsyncWithConversions() async {
  _ = bar(incrementSync)
  _ = bar { -$0 }
  _ = bar(incrementAsync)
  // expected-error@-1:7{{expression is 'async' but is not marked with 'await'}}{{7-7=await }}
  // expected-note@-2:7{{call is 'async'}}
}

// rdar://88692889 - make sure overload resolution cues off the presence of
// 'await' in the body to determine whether to prefer async functions, not
// whether the closure is in a context where it will be converted to async.
@available(SwiftStdlib 5.1, *)
struct OverloadInImplicitAsyncClosure {
  init(int: Int) async throws {
    let task = Task { () -> Self in
      let result = try Self(int: int)
      return result
    }

    self = try await task.value
  }

  init(int: Int) throws { }
}