File: definite_init_actor.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 (322 lines) | stat: -rw-r--r-- 14,583 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
// RUN: %target-swift-frontend -module-name test -disable-availability-checking -swift-version 5 -sil-verify-all -emit-sil %s | %FileCheck --enable-var-scope --implicit-check-not='hop_to_executor' %s

// REQUIRES: concurrency
// REQUIRES: swift_in_compiler

 enum ActingError<T> : Error {
   case forgotLine
   case smuggledValue(T)
 }

func neverReturn() -> Never { fatalError("quit!") }
func arbitraryAsync() async {}

actor BoringActor {

    // CHECK-LABEL: sil hidden @$s4test11BoringActorCACyYacfc : $@convention(method) @async (@sil_isolated @owned BoringActor) -> @owned BoringActor {
    // CHECK:   bb0([[SELF:%[0-9]+]] : $BoringActor):
    // CHECK:       initializeDefaultActor
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK-NEXT:  hop_to_executor [[EI]]
    // CHECK: } // end sil function '$s4test11BoringActorCACyYacfc'
    init() async {}

    // CHECK-LABEL: sil hidden @$s4test11BoringActorC4sizeACSi_tYacfc : $@convention(method) @async (Int, @sil_isolated @owned BoringActor) -> @owned BoringActor {
    // CHECK:   bb0({{%[0-9]+}} : $Int, [[SELF:%[0-9]+]] : $BoringActor):
    // CHECK:       initializeDefaultActor
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK-NEXT:  hop_to_executor [[EI]]
    // CHECK: } // end sil function '$s4test11BoringActorC4sizeACSi_tYacfc'
    init(size: Int) async {
        var sz = size
        while sz > 0 {
            print(".")
            sz -= 1
        }
        print("done!")
    }

    // CHECK-LABEL: sil hidden @$s4test11BoringActorC04mainC0ACyt_tYacfc : $@convention(method) @async (@owned BoringActor) -> @owned BoringActor {
    // CHECK:         hop_to_executor {{%[0-9]+}} : $MainActor
    // CHECK:         builtin "initializeDefaultActor"
    // CHECK:         [[FN:%[0-9]+]] = function_ref @$s4test14arbitraryAsyncyyYaF : $@convention(thin) @async () -> ()
    // CHECK:         = apply [[FN]]() : $@convention(thin) @async () -> ()
    // CHECK-NEXT:    hop_to_executor {{%[0-9]+}} : $MainActor
    @MainActor
    init(mainActor: Void) async {
      await arbitraryAsync()
    }

    // CHECK-LABEL: sil hidden @$s4test11BoringActorC6crashyACSgyt_tYacfc : $@convention(method) @async (@sil_isolated @owned BoringActor) -> @owned Optional<BoringActor> {
    // CHECK:   bb0([[SELF:%[0-9]+]] : $BoringActor):
    // CHECK:       initializeDefaultActor
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK-NEXT:  hop_to_executor [[EI]]
    // CHECK: } // end sil function '$s4test11BoringActorC6crashyACSgyt_tYacfc'
    init!(crashy: Void) async { return nil }

    // CHECK-LABEL: sil hidden @$s4test11BoringActorC5nillyACSgSi_tYacfc : $@convention(method) @async (Int, @sil_isolated @owned BoringActor) -> @owned Optional<BoringActor> {
    // CHECK:   bb0({{%[0-9]+}} : $Int, [[SELF:%[0-9]+]] : $BoringActor):
    // CHECK:       initializeDefaultActor
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK-NEXT:  hop_to_executor [[EI]]
    // CHECK: } // end sil function '$s4test11BoringActorC5nillyACSgSi_tYacfc'
    init?(nilly: Int) async {
        guard nilly > 0 else { return nil }
    }
}

 actor SingleVarActor {
    var myVar: Int

   init(sync: Void) {
     myVar = 0
   }

    // CHECK-LABEL: sil hidden @$s4test14SingleVarActorCACyYacfc : $@convention(method) @async (@sil_isolated @owned SingleVarActor) -> @owned SingleVarActor {
    // CHECK:    bb0([[SELF:%[0-9]+]] : $SingleVarActor):
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       store {{%[0-9]+}} to [[ACCESS:%[0-9]+]]
    // CHECK-NEXT:  end_access [[ACCESS]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $SingleVarActor
    // CHECK:       store {{%[0-9]+}} to {{%[0-9]+}}
    // CHECK: } // end sil function '$s4test14SingleVarActorCACyYacfc'
    init() async {
        myVar = 0
        myVar = 1
    }

    // CHECK-LABEL: sil hidden @$s4test14SingleVarActorC10iterationsACSi_tYacfc : $@convention(method) @async (Int, @sil_isolated @owned SingleVarActor) -> @owned SingleVarActor {
    // CHECK:   bb0({{%[0-9]+}} : $Int, [[SELF:%[0-9]+]] : $SingleVarActor):
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       [[MYVAR_REF:%[0-9]+]] = ref_element_addr [[EI]] : $SingleVarActor, #SingleVarActor.myVar
    // CHECK:       [[MYVAR:%[0-9]+]] = begin_access [init] [static] [[MYVAR_REF]] : $*Int
    // CHECK:       store {{%[0-9]+}} to [[MYVAR]] : $*Int
    // CHECK-NEXT:  end_access [[MYVAR]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $SingleVarActor
    // CHECK: } // end sil function '$s4test14SingleVarActorC10iterationsACSi_tYacfc'
    init(iterations: Int) async {
        var iter = iterations
        repeat {
            myVar = 0
            iter -= 1
        } while iter > 0
    }

    // CHECK-LABEL: sil hidden @$s4test14SingleVarActorC2b12b2ACSb_SbtYacfc : $@convention(method) @async (Bool, Bool, @sil_isolated @owned SingleVarActor) -> @owned SingleVarActor {
    // CHECK:   bb0({{%[0-9]+}} : $Bool, {{%[0-9]+}} : $Bool, [[SELF:%[0-9]+]] : $SingleVarActor):

    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       store {{%[0-9]+}} to [[A1:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[A1]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $SingleVarActor

    // CHECK:       store {{%[0-9]+}} to [[A2:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[A2]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $SingleVarActor

    // CHECK:       store {{%[0-9]+}} to [[A3:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[A3]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $SingleVarActor

    // CHECK: } // end sil function '$s4test14SingleVarActorC2b12b2ACSb_SbtYacfc'
    init(b1: Bool, b2: Bool) async {
        if b1 {
            if b2 {
                myVar = 0
            }
            myVar = 1
        }
        myVar = 2
    }

   // CHECK-LABEL: sil hidden @$s4test14SingleVarActorC14failable_asyncACSgSb_tYacfc : $@convention(method) @async (Bool, @sil_isolated @owned SingleVarActor) -> @owned Optional<SingleVarActor> {
   // CHECK: bb0({{%[0-9]+}} : $Bool, {{%[0-9]+}} : $SingleVarActor):
   // CHECK:   cond_br {{%[0-9]+}}, [[SUCCESS_BB:bb[0-9]+]], {{bb[0-9]+}}
   //
   // CHECK: [[SUCCESS_BB]]:
   // CHECK:   store {{%[0-9]+}} to {{%[0-9]+}} : $*Int
   // CHECK:   hop_to_executor {{%[0-9]+}}
   // CHECK:   enum $Optional<SingleVarActor>, #Optional.some!enumelt
   //
   // CHECK: } // end sil function '$s4test14SingleVarActorC14failable_asyncACSgSb_tYacfc'
   init?(failable_async cond: Bool) async {
     guard cond else { return nil }
     myVar = 1
   }


   // FIXME: the convenience init below is missing a hop after the call to arbitraryAsync (rdar://87485045)

   // CHECK-LABEL: sil hidden @$s4test14SingleVarActorC10delegatingACSb_tYacfC : $@convention(method) @async (Bool, @thick SingleVarActor.Type) -> @owned SingleVarActor {
   // CHECK:         [[SELF_ALLOC:%[0-9]+]] = alloc_stack [lexical] [var_decl] $SingleVarActor, let, name "self"
   //           ** first hop is after the call to the synchronous init, right after initializing the allocation.
   // CHECK:         [[SYNC_FN:%[0-9]+]] = function_ref @$s4test14SingleVarActorC4syncACyt_tcfC : $@convention(method) (@thick SingleVarActor.Type) -> @owned SingleVarActor
   // CHECK:         [[INIT1:%[0-9]+]] = apply [[SYNC_FN]]({{%[0-9]+}}) : $@convention(method) (@thick SingleVarActor.Type) -> @owned SingleVarActor
   // CHECK:         store [[INIT1]] to [[SELF_ALLOC]] : $*SingleVarActor
   // CHECK-NEXT:    hop_to_executor [[INIT1]] : $SingleVarActor
   //           ** second hop is after the call to async init
   // CHECK:         [[ASYNC_FN:%[0-9]+]] = function_ref @$s4test14SingleVarActorCACyYacfC : $@convention(method) @async (@thick SingleVarActor.Type) -> @owned SingleVarActor
   // CHECK:         [[INIT2:%[0-9]+]] = apply [[ASYNC_FN]]({{%[0-9]+}}) : $@convention(method) @async (@thick SingleVarActor.Type) -> @owned SingleVarActor
   // CHECK:         store [[INIT2]] to [[SELF_ALLOC]] : $*SingleVarActor
   // CHECK-NEXT:    hop_to_executor [[INIT2]] : $SingleVarActor
   // CHECK:       } // end sil function '$s4test14SingleVarActorC10delegatingACSb_tYacfC'
   convenience init(delegating c: Bool) async {
     if c {
       self.init(sync: ())
     } else {
       await self.init()
     }
     await arbitraryAsync()
   }

 }

actor DefaultInit {
    var x: DefaultInit?
    var y: String = ""
    var z: ActingError<Int> = .smuggledValue(5)

    // CHECK-LABEL: sil hidden @$s4test11DefaultInitCACyYacfc : $@convention(method) @async (@sil_isolated @owned DefaultInit) -> @owned DefaultInit {
    // CHECK:   bb0([[SELF:%[0-9]+]] : $DefaultInit):
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       store {{%[0-9]+}} to {{%[0-9]+}} : $*ActingError<Int>
    // CHECK-NEXT:  hop_to_executor [[EI]] : $DefaultInit
    // CHECK: } // end sil function '$s4test11DefaultInitCACyYacfc'
    init() async {}

    // CHECK-LABEL: sil hidden @$s4test11DefaultInitC5nillyACSgSb_tYacfc : $@convention(method) @async (Bool, @sil_isolated @owned DefaultInit) -> @owned Optional<DefaultInit> {
    // CHECK:   bb0({{%[0-9]+}} : $Bool, [[SELF:%[0-9]+]] : $DefaultInit):
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       store {{%[0-9]+}} to {{%[0-9]+}} : $*ActingError<Int>
    // CHECK-NEXT:  hop_to_executor [[EI]] : $DefaultInit
    // CHECK: } // end sil function '$s4test11DefaultInitC5nillyACSgSb_tYacfc'
    init?(nilly: Bool) async {
        guard nilly else { return nil }
    }

    init(sync: Void) {}
    @MainActor init(mainActorSync: Void) {}
}

actor MultiVarActor {
    var firstVar: Int
    var secondVar: Float

    // CHECK-LABEL: sil hidden @$s4test13MultiVarActorC10doNotThrowACSb_tYaKcfc : $@convention(method) @async (Bool, @sil_isolated @owned MultiVarActor) -> (@owned MultiVarActor, @error any Error) {
    // CHECK:   bb0({{%[0-9]+}} : $Bool, [[SELF:%[0-9]+]] : $MultiVarActor):
    // CHECK:       [[EI:%.*]] = end_init_let_ref [[SELF]]
    // CHECK:       [[REF:%[0-9]+]] = ref_element_addr [[EI]] : $MultiVarActor, #MultiVarActor.firstVar
    // CHECK:       [[VAR:%[0-9]+]] = begin_access [init] [static] [[REF]] : $*Int
    // CHECK:       store {{%[0-9]+}} to [[VAR]] : $*Int
    // CHECK-NEXT:  end_access [[VAR]]
    // CHECK-NEXT:  hop_to_executor [[EI]] : $MultiVarActor
    // CHECK: } // end sil function '$s4test13MultiVarActorC10doNotThrowACSb_tYaKcfc'
    init(doNotThrow: Bool) async throws {
        secondVar = 0
        guard doNotThrow else { throw ActingError<Any>.forgotLine }
        firstVar = 1
    }

    // CHECK-LABEL: sil hidden @$s4test13MultiVarActorC10noSuccCaseACSb_tYacfc : $@convention(method) @async (Bool, @sil_isolated @owned MultiVarActor) -> @owned MultiVarActor {
    // CHECK:       store {{%[0-9]+}} to [[A1:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[A1]]
    // CHECK-NEXT:  hop_to_executor {{%[0-9]+}} : $MultiVarActor

    // CHECK:       store {{%[0-9]+}} to [[A2:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[A2]]
    // CHECK-NEXT:  hop_to_executor {{%[0-9]+}} : $MultiVarActor
    // CHECK: } // end sil function '$s4test13MultiVarActorC10noSuccCaseACSb_tYacfc'
    init(noSuccCase: Bool) async {
        secondVar = 0
        if noSuccCase {
            firstVar = 1
        }
        firstVar = 2
    }

    // CHECK-LABEL: sil hidden @$s4test13MultiVarActorC10noPredCaseACSb_tYacfc : $@convention(method) @async (Bool, @sil_isolated @owned MultiVarActor) -> @owned MultiVarActor {
    // CHECK:       store {{%[0-9]+}} to [[ACCESS:%[0-9]+]] : $*Int
    // CHECK-NEXT:  end_access [[ACCESS]]
    // CHECK-NEXT:  hop_to_executor {{%[0-9]+}} : $MultiVarActor
    // CHECK: } // end sil function '$s4test13MultiVarActorC10noPredCaseACSb_tYacfc'
    init(noPredCase: Bool) async {
        secondVar = 0
        firstVar = 1
        if noPredCase {
            print("hello")
        }
        print("hi")
    }


    // Some cases where we do NOT expect a hop to be emitted because they do
    // not fully-initialize `self`. The implicit check-not flag on this test
    // ensures that any unexpected hops are caught.

    init?(doesNotFullyInit1: Bool) async {
        firstVar = 1
        return nil
    }

    init(doesNotFullyInit2: Bool) async {
        firstVar = 1
        fatalError("I give up!")
    }

    init(doesNotFullyInit3: Bool) async throws {
        firstVar = 1
        throw ActingError<Any>.forgotLine
    }

    init?(doesNotFullyInit4: Bool) async {
        firstVar = 1
        neverReturn()
    }
}

@available(SwiftStdlib 5.1, *)
actor TaskMaster {
    var task: Task<Void, Never>?

    func sayHello() { print("hello") }

    ////// for the initializer
    // CHECK-LABEL: @$s4test10TaskMasterCACyYacfc : $@convention(method) @async (@sil_isolated @owned TaskMaster) -> @owned TaskMaster {
    // CHECK:           [[ELM:%[0-9]+]] = ref_element_addr [[SELF:%[0-9]+]] : $TaskMaster, #TaskMaster.task
    // CHECK:           [[NIL:%[0-9]+]] = enum $Optional<Task<(), Never>>, #Optional.none!enumelt
    // CHECK:           store [[NIL]] to [[ELM]] : $*Optional<Task<(), Never>>
    // CHECK-NEXT:      hop_to_executor [[SELF]] : $TaskMaster
    // CHECK: } // end sil function '$s4test10TaskMasterCACyYacfc'
    init() async {

        ////// for the closure
        // CHECK-LABEL: sil private @$s4test10TaskMasterCACyYacfcyyYacfU_ :
        // CHECK:           hop_to_executor {{%[0-9]+}} : $TaskMaster
        // CHECK: } // end sil function '$s4test10TaskMasterCACyYacfcyyYacfU_'
        task = Task.detached { await self.sayHello() }
    }
}

actor SomeActor {
    var x: Int = 0

    // CHECK-LABEL: sil hidden @$s4test9SomeActorCACyYacfc : $@convention(method) @async (@sil_isolated @owned SomeActor) -> @owned SomeActor {
    // CHECK-NOT:       begin_access
    // CHECK:           store {{%[0-9]+}} to {{%[0-9]+}} : $*Int
    // CHECK-NEXT:      hop_to_executor {{%[0-9]+}} : $SomeActor
    // CHECK: } // end sil function '$s4test9SomeActorCACyYacfc'
    init() async {}
}

actor Ahmad {
  var x: Int = 0
  
  // CHECK-LABEL: sil hidden @$s4test5AhmadCACyYacfc : $@convention(method) @async (@owned Ahmad) -> @owned Ahmad {
  // CHECK:         bb0{{.*}}:
  // CHECK-NEXT:      [[GENERIC:%[0-9]+]] = enum $Optional<Builtin.Executor>, #Optional.none!enumelt
  // CHECK-NEXT:      hop_to_executor [[GENERIC]]
  // CHECK:           store {{%[0-9]+}} to {{%[0-9]+}} : $*Int
  // CHECK: } // end sil function '$s4test5AhmadCACyYacfc'
  nonisolated init() async {}
}