Skip to content

Commit 0ed6c1c

Browse files
simanerushxedin
authored andcommitted
[Concurrency] Look for explicit 'nonisolated' when getting isolation from protocol conformances.
(cherry picked from commit 4fca62a)
1 parent d242750 commit 0ed6c1c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5276,13 +5276,26 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
52765276
}
52775277

52785278
auto *proto = conformance->getProtocol();
5279-
switch (auto protoIsolation = getActorIsolation(proto)) {
5279+
auto inferredIsolation = getInferredActorIsolation(proto);
5280+
auto protoIsolation = inferredIsolation.isolation;
5281+
switch (protoIsolation) {
52805282
case ActorIsolation::ActorInstance:
52815283
case ActorIsolation::Unspecified:
5282-
case ActorIsolation::Nonisolated:
52835284
case ActorIsolation::CallerIsolationInheriting:
52845285
case ActorIsolation::NonisolatedUnsafe:
52855286
break;
5287+
case ActorIsolation::Nonisolated:
5288+
if (inferredIsolation.source.kind == IsolationSource::Kind::Explicit) {
5289+
if (!foundIsolation) {
5290+
// We found an explicitly 'nonisolated' protocol.
5291+
foundIsolation = {
5292+
protoIsolation,
5293+
IsolationSource(proto, IsolationSource::Conformance)};
5294+
}
5295+
continue;
5296+
} else {
5297+
break;
5298+
}
52865299

52875300
case ActorIsolation::Erased:
52885301
llvm_unreachable("protocol cannot have erased isolation");

test/Concurrency/nonisolated_rules.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public struct PublicNonSendable {
6363
}
6464

6565

66-
nonisolated struct NonisolatedStruct: GloballyIsolated {
66+
nonisolated struct StructRemovesGlobalActor: GloballyIsolated {
6767
var x: NonSendable
6868
var y: Int = 1
6969

@@ -106,6 +106,32 @@ struct A: Refined {
106106
init(x: NonSendable) {
107107
self.x = x // okay
108108
}
109+
110+
init() {
111+
self.x = NonSendable()
112+
}
113+
114+
func f() {}
115+
}
116+
117+
@MainActor protocol ExplicitGlobalActor: Refined {}
118+
119+
struct IsolatedStruct: ExplicitGlobalActor {
120+
// expected-note@+2 {{main actor isolation inferred from conformance to protocol 'ExplicitGlobalActor'}}
121+
// expected-note@+1 {{calls to instance method 'g()' from outside of its actor context are implicitly asynchronous}}
122+
func g() {}
123+
}
124+
125+
struct NonisolatedStruct {
126+
func callF() {
127+
return A().f() // okay, 'A' is non-isolated.
128+
}
129+
130+
// expected-note@+1 {{add '@MainActor' to make instance method 'callG()' part of global actor 'MainActor'}}
131+
func callG() {
132+
// expected-error@+1{{call to main actor-isolated instance method 'g()' in a synchronous nonisolated context}}
133+
return IsolatedStruct().g()
134+
}
109135
}
110136

111137
// MARK: - Extensions

0 commit comments

Comments
 (0)