File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -5130,6 +5130,13 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
51305130 if (dc->getSelfProtocolDecl ())
51315131 return std::nullopt ;
51325132
5133+ // Prevent isolation inference from requirements if the conforming type
5134+ // has an explicit `nonisolated` attribute.
5135+ if (auto *NTD = dc->getSelfNominalTypeDecl ()) {
5136+ if (NTD->getAttrs ().hasAttribute <NonisolatedAttr>())
5137+ return std::nullopt ;
5138+ }
5139+
51335140 // Walk through each of the conformances in this context, collecting any
51345141 // requirements that have actor isolation.
51355142 auto conformances = idc->getLocalConformances ( // note this
Original file line number Diff line number Diff line change @@ -142,6 +142,34 @@ nonisolated class K: GloballyIsolated {
142142 }
143143}
144144
145+ @MainActor
146+ protocol GloballyIsolatedWithRequirements {
147+ var x : NonSendable { get set } // expected-note {{property declared here}}
148+ func test( ) // expected-note {{calls to instance method 'test()' from outside of its actor context are implicitly asynchronous}}
149+ }
150+
151+ nonisolated class K2 : GloballyIsolatedWithRequirements {
152+ var x : NonSendable
153+
154+ func test( ) { }
155+
156+ func testNonWitness( ) { }
157+
158+ init ( x: NonSendable ) {
159+ self . x = x // okay
160+ test ( ) // okay
161+ testNonWitness ( ) // okay
162+ }
163+
164+ func test< T: GloballyIsolatedWithRequirements > ( t: T , s: K2 ) {
165+ _ = s. x // okay
166+ _ = t. x // expected-error {{main actor-isolated property 'x' can not be referenced from a nonisolated context}}
167+
168+ s. test ( ) // okay
169+ t. test ( ) // expected-error {{call to main actor-isolated instance method 'test()' in a synchronous nonisolated context}}
170+ }
171+ }
172+
145173// MARK: - Storage of non-Sendable
146174
147175class KlassA {
You can’t perform that action at this time.
0 commit comments