Skip to content

Commit 2f920ce

Browse files
authored
Merge pull request #82289 from xedin/rdar-145519840
[Concurrency] SE-0449: `nonisolated` on a type should prevent isolati…
2 parents f6e6172 + e50acbf commit 2f920ce

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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

test/Concurrency/nonisolated_rules.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff 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

147175
class KlassA {

0 commit comments

Comments
 (0)