@@ -63,7 +63,7 @@ public struct PublicNonSendable {
63
63
}
64
64
65
65
66
- nonisolated struct NonisolatedStruct : GloballyIsolated {
66
+ nonisolated struct StructRemovesGlobalActor : GloballyIsolated {
67
67
var x : NonSendable
68
68
var y : Int = 1
69
69
@@ -100,12 +100,110 @@ nonisolated struct S1: GloballyIsolated {
100
100
// MARK: - Protocols
101
101
102
102
nonisolated protocol Refined : GloballyIsolated { }
103
+ nonisolated protocol WhyNot { }
104
+
105
+ nonisolated protocol NonisolatedWithMembers {
106
+ func test( )
107
+ }
103
108
104
109
struct A : Refined {
105
110
var x : NonSendable
106
111
init ( x: NonSendable ) {
107
112
self . x = x // okay
108
113
}
114
+
115
+ init ( ) {
116
+ self . x = NonSendable ( )
117
+ }
118
+
119
+ func f( ) { }
120
+ }
121
+
122
+ @MainActor protocol ExplicitGlobalActor : Refined { }
123
+
124
+ struct IsolatedA : ExplicitGlobalActor {
125
+ // expected-note@+2 {{main actor isolation inferred from conformance to protocol 'ExplicitGlobalActor'}}
126
+ // expected-note@+1 {{calls to instance method 'g()' from outside of its actor context are implicitly asynchronous}}
127
+ func g( ) { }
128
+ }
129
+
130
+ struct IsolatedB : Refined , ExplicitGlobalActor {
131
+ // expected-note@+2 {{calls to instance method 'h()' from outside of its actor context are implicitly asynchronous}}
132
+ // expected-note@+1 {{main actor isolation inferred from conformance to protocol 'ExplicitGlobalActor'}}
133
+ func h( ) { }
134
+ }
135
+
136
+ struct IsolatedC : WhyNot , GloballyIsolated {
137
+ // expected-note@+2 {{calls to instance method 'k()' from outside of its actor context are implicitly asynchronous}}
138
+ // expected-note@+1 {{main actor isolation inferred from conformance to protocol 'GloballyIsolated'}}
139
+ func k( ) { }
140
+ }
141
+
142
+ struct IsolatedCFlipped : GloballyIsolated , WhyNot {
143
+ // expected-note@+2 {{calls to instance method 'k2()' from outside of its actor context are implicitly asynchronous}}
144
+ // expected-note@+1 {{main actor isolation inferred from conformance to protocol 'GloballyIsolated'}}
145
+ func k2( ) { }
146
+ }
147
+
148
+ struct NonisolatedStruct {
149
+ func callF( ) {
150
+ return A ( ) . f ( ) // okay, 'A' is non-isolated.
151
+ }
152
+
153
+ // expected-note@+1 {{add '@MainActor' to make instance method 'callG()' part of global actor 'MainActor'}}
154
+ func callG( ) {
155
+ // expected-error@+1{{call to main actor-isolated instance method 'g()' in a synchronous nonisolated context}}
156
+ return IsolatedA ( ) . g ( )
157
+ }
158
+
159
+ // expected-note@+1 {{add '@MainActor' to make instance method 'callH()' part of global actor 'MainActor'}}
160
+ func callH( ) {
161
+ // expected-error@+1 {{call to main actor-isolated instance method 'h()' in a synchronous nonisolated context}}
162
+ return IsolatedB ( ) . h ( )
163
+ }
164
+
165
+ // expected-note@+1 {{add '@MainActor' to make instance method 'callK()' part of global actor 'MainActor'}}
166
+ func callK( ) {
167
+ // expected-error@+1 {{call to main actor-isolated instance method 'k()' in a synchronous nonisolated context}}
168
+ return IsolatedC ( ) . k ( )
169
+ }
170
+
171
+ // expected-note@+1 {{add '@MainActor' to make instance method 'callK2()' part of global actor 'MainActor'}}
172
+ func callK2( ) {
173
+ // expected-error@+1 {{call to main actor-isolated instance method 'k2()' in a synchronous nonisolated context}}
174
+ return IsolatedCFlipped ( ) . k2 ( )
175
+ }
176
+ }
177
+
178
+ @MainActor
179
+ struct TestIsolated : NonisolatedWithMembers {
180
+ var x : NonSendable // expected-note {{property declared here}}
181
+
182
+ // requirement behaves as if it's explicitly `nonisolated` which gets inferred onto the witness
183
+ func test( ) {
184
+ _ = x // expected-error {{main actor-isolated property 'x' can not be referenced from a nonisolated context}}
185
+ }
186
+ }
187
+
188
+ @MainActor
189
+ protocol Root {
190
+ func testRoot( )
191
+ }
192
+
193
+ nonisolated protocol Child : Root {
194
+ func testChild( )
195
+ }
196
+
197
+ struct TestDifferentLevels : Child {
198
+ func testRoot( ) { }
199
+ func testChild( ) { }
200
+ func testNonWitness( ) { }
201
+ }
202
+
203
+ nonisolated func testRequirementsOnMultipleNestingLevels( t: TestDifferentLevels ) {
204
+ t. testRoot ( ) // okay
205
+ t. testChild ( ) // okay
206
+ t. testNonWitness ( ) // okay
109
207
}
110
208
111
209
// MARK: - Extensions
@@ -144,6 +242,34 @@ nonisolated class K: GloballyIsolated {
144
242
}
145
243
}
146
244
245
+ @MainActor
246
+ protocol GloballyIsolatedWithRequirements {
247
+ var x : NonSendable { get set } // expected-note {{property declared here}}
248
+ func test( ) // expected-note {{calls to instance method 'test()' from outside of its actor context are implicitly asynchronous}}
249
+ }
250
+
251
+ nonisolated class K2 : GloballyIsolatedWithRequirements {
252
+ var x : NonSendable
253
+
254
+ func test( ) { }
255
+
256
+ func testNonWitness( ) { }
257
+
258
+ init ( x: NonSendable ) {
259
+ self . x = x // okay
260
+ test ( ) // okay
261
+ testNonWitness ( ) // okay
262
+ }
263
+
264
+ func test< T: GloballyIsolatedWithRequirements > ( t: T , s: K2 ) {
265
+ _ = s. x // okay
266
+ _ = t. x // expected-error {{main actor-isolated property 'x' can not be referenced from a nonisolated context}}
267
+
268
+ s. test ( ) // okay
269
+ t. test ( ) // expected-error {{call to main actor-isolated instance method 'test()' in a synchronous nonisolated context}}
270
+ }
271
+ }
272
+
147
273
// MARK: - Storage of non-Sendable
148
274
149
275
class KlassA {
0 commit comments