-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathasync_initializer.swift
177 lines (137 loc) · 6.09 KB
/
async_initializer.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
// RUN: %target-swift-frontend -enable-experimental-concurrency -target %target-swift-5.1-abi-triple -emit-sil -o /dev/null -verify %s
// RUN: %target-swift-frontend -enable-experimental-concurrency -target %target-swift-5.1-abi-triple -emit-sil -o /dev/null -verify -strict-concurrency=targeted %s
// RUN: %target-swift-frontend -enable-experimental-concurrency -target %target-swift-5.1-abi-triple -emit-sil -o /dev/null -verify -strict-concurrency=complete %s -verify-additional-prefix complete-and-tns-
// RUN: %target-swift-frontend -enable-experimental-concurrency -target %target-swift-5.1-abi-triple -emit-sil -o /dev/null -verify -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation %s -verify-additional-prefix complete-and-tns-
// REQUIRES: concurrency
// REQUIRES: swift_feature_RegionBasedIsolation
////
// some functions to play with
func f() async {
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}} {{11-11=await }}
let _ = Person() // expected-note {{call is 'async'}}
}
func g() {}
////
// test super.init interactions
class Person {
init() async {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{5-5=await }}
f() // expected-note{{call is 'async'}}
}
convenience init(_ s: String) async {
await self.init()
}
}
class Bertrand: Person {
override init() {} // expected-error {{missing call to superclass's initializer; 'super.init' is 'async' and requires an explicit call}}
init(_ x: Int) async {} // expected-error {{missing call to superclass's initializer; 'super.init' is 'async' and requires an explicit call}}
}
class Barbara: Person {
// expected-note@+2 {{add 'async' to function 'init(_:)' to make it asynchronous}} {{20-20= async}}
// expected-error@+1 {{missing call to superclass's initializer; 'super.init' is 'async' and requires an explicit call}}
init(_ d: Double) {
f() // expected-error{{'async' call in a function that does not support concurrency}}
}
init(x: Int, y: Int) async {
await super.init()
}
convenience init(a: Double, b: Double) async {
await self.init(x: 0, y: 0)
}
}
class Fruit {
init() async {}
init(name: String) {}
}
class Banana: Fruit {
override init() {
super.init(name: "banana")
}
}
class Cat {} // expected-note {{overridden declaration is here}}
class Calico: Cat {
override init() async {} // expected-error {{cannot override non-async initializer with async initializer}}
}
func reconstruct(c: Cat) {
c.init() // expected-error {{'init' is a member of the type}}
}
////
// test reasync initializers
class MyType {
init(_ f: () async -> Void) reasync {
await f()
}
}
func beep() async {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
let _ = MyType(f) // expected-note{{call is 'async'}}
let _ = await MyType(f)
let _ = MyType(g)
}
////
// test other types with constructors
actor A {
init() async {
await f()
}
}
actor B: A { // expected-error{{actor types do not support inheritance}}
init(x : String) async {} // expected-error {{missing call to superclass's initializer; 'super.init' is 'async' and requires an explicit call}}
}
enum E {
init() async {
await f()
}
}
struct SomeStruct {
@MainActor init(asyncMainActor: Int) async {}
@MainActor init(mainActor: Int) {} // expected-note {{calls to initializer 'init(mainActor:)' from outside of its actor context are implicitly asynchronous}}
// expected-warning@+1 {{'(unsafe)' global actors are deprecated; use '@preconcurrency' instead}}
@MainActor(unsafe) init(asyncMainActorUnsafe: Int) async {}
// expected-warning@+2 {{'(unsafe)' global actors are deprecated; use '@preconcurrency' instead}}
// expected-complete-and-tns-note@+1 {{calls to initializer 'init(mainActorUnsafe:)' from outside of its actor context are implicitly asynchronous}}
@MainActor(unsafe) init(mainActorUnsafe: Int) {}
}
// expected-complete-and-tns-note @+3 {{add '@MainActor' to make global function 'globActorTest1()' part of global actor 'MainActor'}}
// expected-note @+2 {{add '@MainActor' to make global function 'globActorTest1()' part of global actor 'MainActor'}}
// expected-note @+1 2 {{add 'async' to function 'globActorTest1()' to make it asynchronous}}
func globActorTest1() {
_ = SomeStruct(asyncMainActor: 0) // expected-error {{'async' call in a function that does not support concurrency}}
_ = SomeStruct(mainActor: 0) // expected-error {{call to main actor-isolated initializer 'init(mainActor:)' in a synchronous nonisolated context}}
_ = SomeStruct(asyncMainActorUnsafe: 0) // expected-error {{'async' call in a function that does not support concurrency}}
_ = SomeStruct(mainActorUnsafe: 0) // expected-complete-and-tns-warning {{call to main actor-isolated initializer 'init(mainActorUnsafe:)' in a synchronous nonisolated context}}
}
func globActorTestAsyncEdition() async {
_ = await SomeStruct(asyncMainActor: 0)
_ = await SomeStruct(mainActor: 0)
_ = await SomeStruct(asyncMainActorUnsafe: 0)
_ = await SomeStruct(mainActorUnsafe: 0)
}
////
// check protocol conformance & inheritance
protocol AsyncDefaultConstructable {
init() async
}
struct Location {
var x : Int
var y : Int
init() async { // expected-note {{candidate is 'async', but protocol requirement is not}}
self.x = 0
self.y = 0
}
}
protocol DefaultConstructable {
init() // expected-note {{protocol requires initializer 'init()' with type '()'}}
}
extension Location: DefaultConstructable {} // expected-error {{type 'Location' does not conform to protocol 'DefaultConstructable'}} expected-note {{add stubs for conformance}} {{43-43=\n init() {\n <#code#>\n \}\n}}
extension Location: AsyncDefaultConstructable {}
protocol Plain {
// expected-note@+2 {{overridden declaration is here}}
// expected-note@+1 {{attempt to override convenience initializer here}}
init()
}
protocol Spicy: Plain {
// expected-error@+2 {{cannot override non-async initializer with async initializer}}
// expected-error@+1 {{initializer does not override a designated initializer from its parent protocol}}
override init() async
}