File: isolated_default_arguments.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 (319 lines) | stat: -rw-r--r-- 10,883 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
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -I %t  -disable-availability-checking -strict-concurrency=complete -parse-as-library -emit-sil -o /dev/null -verify -enable-upcoming-feature IsolatedDefaultValues -enable-upcoming-feature RegionBasedIsolation -enable-upcoming-feature InferSendableFromCaptures %s

// REQUIRES: concurrency
// REQUIRES: asserts

@globalActor
actor SomeGlobalActor {
  static let shared = SomeGlobalActor()
}

// expected-note@+2 2 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}}
@MainActor
func requiresMainActor() -> Int { 0 }

@SomeGlobalActor
func requiresSomeGlobalActor() -> Int { 0 }

// expected-error@+1 {{main actor-isolated default value in a nonisolated context}}
func mainActorDefaultArgInvalid(value: Int = requiresMainActor()) {}

func mainActorClosureInvalid(
  closure: () -> Int = { // expected-error {{main actor-isolated default value in a nonisolated context}}
    requiresMainActor()
  }
) {}

// expected-note@+2 {{calls to global function 'mainActorDefaultArg(value:)' from outside of its actor context are implicitly asynchronous}}
@MainActor
func mainActorDefaultArg(value: Int = requiresMainActor()) {}

// expected-note@+1 {{calls to global function 'mainActorClosure(closure:)' from outside of its actor context are implicitly asynchronous}}
@MainActor func mainActorClosure(
  closure: () -> Int = {
    requiresMainActor()
  }
) {}

func mainActorClosureCall(
  closure: Int = { // expected-error {{main actor-isolated default value in a nonisolated context}}
    requiresMainActor()
  }()
) {}

@MainActor func mainActorCaller() {
  mainActorDefaultArg()
  mainActorClosure()
  mainActorClosureCall()
}

// expected-note@+1 2 {{add '@MainActor' to make global function 'nonisolatedCaller()' part of global actor 'MainActor'}}
func nonisolatedCaller() {
  // expected-error@+1 {{call to main actor-isolated global function 'mainActorDefaultArg(value:)' in a synchronous nonisolated context}}
  mainActorDefaultArg()

  // expected-error@+1 {{call to main actor-isolated global function 'mainActorClosure(closure:)' in a synchronous nonisolated context}}
  mainActorClosure()
}

func nonisolatedAsyncCaller() async {
  // expected-error@+2 {{expression is 'async' but is not marked with 'await'}}
  // expected-note@+1 {{calls to global function 'mainActorDefaultArg(value:)' from outside of its actor context are implicitly asynchronous}}
  mainActorDefaultArg()

  // expected-error@+2 {{expression is 'async' but is not marked with 'await'}}
  // expected-note@+1 {{calls to global function 'mainActorClosure(closure:)' from outside of its actor context are implicitly asynchronous}}
  mainActorClosure()

  await mainActorDefaultArg(value: requiresMainActor())

  await mainActorClosure()

  mainActorClosureCall()
}

func conflictingIsolationDefaultArg(
  // expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
  value: (Int, Int) = (requiresMainActor(), requiresSomeGlobalActor())
) {}

func closureLosesIsolation(
  closure: @Sendable () -> Void = {
    // expected-note@+1 {{calls to local function 'f()' from outside of its actor context are implicitly asynchronous}}
    @MainActor func f() {}
    // expected-error@+1 {{call to main actor-isolated local function 'f()' in a synchronous nonisolated context}}
    f()
  }
) {}

func closureIsolationMismatch(
  closure: @SomeGlobalActor () -> Void = {
    // expected-note@+1 {{calls to local function 'f()' from outside of its actor context are implicitly asynchronous}}
    @MainActor func f() {}
    // expected-error@+1 {{call to main actor-isolated local function 'f()' in a synchronous global actor 'SomeGlobalActor'-isolated context}}
    f()
  }
) {}

func conflictingClosureIsolation(
  // expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
  closure: () -> Void = {
    _ = requiresMainActor()
    _ = requiresSomeGlobalActor()
  }
) {}

func isolationInLocalFunction(
  closure: () -> Void = {
    nonisolated func local() -> Int {
      // expected-error@+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}}
      requiresMainActor()
    }
  }
) {}

actor A {}

func closureWithIsolatedParam(
  closure: (isolated A) -> Void = { _ in
    // expected-error@+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous actor-isolated context}}
    _ = requiresMainActor()
  }
) {}

// expected-note@+2 3 {{calls to initializer 'init(required:x:y:)' from outside of its actor context are implicitly asynchronous}}
@MainActor
struct S1 {
  var required: Int

  var x: Int = requiresMainActor()

  lazy var y: Int = requiresMainActor()

  static var z: Int = requiresMainActor()
}

// expected-note@+2 3 {{calls to initializer 'init(required:x:y:)' from outside of its actor context are implicitly asynchronous}}
@SomeGlobalActor
struct S2 {
  var required: Int

  var x: Int = requiresSomeGlobalActor()

  lazy var y: Int = requiresSomeGlobalActor()

  static var z: Int = requiresSomeGlobalActor()
}

struct S3 {
  // expected-error@+1 3 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
  var (x, y, z) = (requiresMainActor(), requiresSomeGlobalActor(), 10)
}

struct S4 {
  // expected-warning@+1 {{main actor-isolated default value in a nonisolated context; this is an error in the Swift 6 language mode}}
  var x: Int = requiresMainActor()
}

@MainActor
func initializeFromMainActor() {
  _ = S1(required: 10)

  // expected-error@+1 {{call to global actor 'SomeGlobalActor'-isolated initializer 'init(required:x:y:)' in a synchronous main actor-isolated context}}
  _ = S2(required: 10)
}

@SomeGlobalActor
func initializeFromSomeGlobalActor() {
  // expected-error@+1 {{call to main actor-isolated initializer 'init(required:x:y:)' in a synchronous global actor 'SomeGlobalActor'-isolated context}}
  _ = S1(required: 10)

  _ = S2(required: 10)
}

// expected-note@+2 {{add '@MainActor' to make global function 'initializeFromNonisolated()' part of global actor 'MainActor'}}
// expected-note@+1 {{add '@SomeGlobalActor' to make global function 'initializeFromNonisolated()' part of global actor 'SomeGlobalActor'}}
func initializeFromNonisolated() {
  // expected-error@+1 {{call to main actor-isolated initializer 'init(required:x:y:)' in a synchronous nonisolated context}}
  _ = S1(required: 10)

  // expected-error@+1 {{call to global actor 'SomeGlobalActor'-isolated initializer 'init(required:x:y:)' in a synchronous nonisolated context}}
  _ = S2(required: 10)
}

extension A {
  func initializeFromActorInstance() {
    // expected-error@+1 {{call to main actor-isolated initializer 'init(required:x:y:)' in a synchronous actor-isolated context}}
    _ = S1(required: 10)

    // expected-error@+1 {{call to global actor 'SomeGlobalActor'-isolated initializer 'init(required:x:y:)' in a synchronous actor-isolated context}}
    _ = S2(required: 10)
  }
}

// expected-warning@+1 {{default initializer for 'C1' cannot be both nonisolated and main actor-isolated; this is an error in the Swift 6 language mode}}
class C1 {
  // expected-note@+1 {{initializer for property 'x' is main actor-isolated}}
  @MainActor var x = requiresMainActor()
  @SomeGlobalActor var y = requiresSomeGlobalActor()
}

class NonSendable {}

// expected-warning@+1 {{default initializer for 'C2' cannot be both nonisolated and main actor-isolated; this is an error in the Swift 6 language mode}}
class C2 {
  // expected-note@+1 {{initializer for property 'x' is main actor-isolated}}
  @MainActor var x = NonSendable()
  @SomeGlobalActor var y = NonSendable()
}

class C3 {
  @MainActor var x = 1
  @SomeGlobalActor var y = 2
}

@MainActor struct NonIsolatedInit {
  var x = 0
  var y = 0
}

@MainActor class MultipleVars {
  var (x, y) = (0, 0)
}

func callDefaultInit() async {
  _ = C2()
  _ = NonIsolatedInit()
  _ = NonIsolatedInit(x: 10)
  _ = MultipleVars()
}

// expected-warning@+1 {{default initializer for 'MultipleVarsInvalid' cannot be both nonisolated and main actor-isolated; this is an error in the Swift 6 language mode}}
class MultipleVarsInvalid {
  // expected-note@+1 {{initializer for property 'x' is main actor-isolated}}
  @MainActor var (x, y) = (requiresMainActor(), requiresMainActor())
}

@propertyWrapper 
@preconcurrency @MainActor
struct RequiresMain<Value>  {
  var wrappedValue: Value

  init(wrappedValue: Value) {
    self.wrappedValue = wrappedValue
  }
}

// This is okay; UseRequiresMain has an inferred 'MainActor'
// attribute.
struct UseRequiresMain {
  @RequiresMain private var x = 10
}

nonisolated func test() async {
  // expected-warning@+2 {{expression is 'async' but is not marked with 'await'; this is an error in the Swift 6 language mode}}
  // expected-note@+1 {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
  _ = UseRequiresMain()
}

// expected-warning@+2 {{memberwise initializer for 'InitAccessors' cannot be both nonisolated and main actor-isolated; this is an error in the Swift 6 language mode}}
// expected-warning@+1 {{default initializer for 'InitAccessors' cannot be both nonisolated and main actor-isolated; this is an error in the Swift 6 language mode}}
struct InitAccessors {
  private var _a: Int

  // expected-note@+1 2 {{initializer for property 'a' is main actor-isolated}}
  @MainActor var a: Int = 5 {
    @storageRestrictions(initializes: _a)
    init {
      _a = requiresMainActor()
    }
    get {
      _a
    }
  }
}

// Make sure isolation inference for implicit initializers
// doesn't impact conformance synthesis.

struct CError: Error, RawRepresentable {
  var rawValue: CInt
}

// Consider isolated key-paths when computing initializer isolation

@MainActor
class UseIsolatedKeyPath {
  let kp: KeyPath<UseIsolatedKeyPath, Nested> = \.x // okay

  // expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
  let kp2: KeyPath<UseIsolatedKeyPath, Bool> = \.x.y // okay

  var x: Nested = .init()

  class Nested {
    @SomeGlobalActor var y: Bool = true
  }
}

@MainActor
protocol InferMainActor {}

struct UseIsolatedPropertyWrapperInit: InferMainActor {
  @Wrapper(\.value) var value: Int // okay

  // expected-warning@+1 {{global actor 'SomeGlobalActor'-isolated default value in a main actor-isolated context; this is an error in the Swift 6 language mode}}
  @Wrapper(\.otherValue) var otherValue: Int
}

@propertyWrapper struct Wrapper<T> {
  init(_: KeyPath<Values, T>) {}
  var wrappedValue: T { fatalError() }
}

struct Values {
  @MainActor var value: Int { 0 }
  @SomeGlobalActor var otherValue: Int { 0 }
}