Skip to content

Commit 00f2d0f

Browse files
committed
[Concurrency] Allow isolation on subclasses of non-Sendable, non-isolated
superclasses. (cherry picked from commit 87f49a5)
1 parent a17d360 commit 00f2d0f

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4805,9 +4805,14 @@ static bool checkClassGlobalActorIsolation(
48054805
switch (superIsolation) {
48064806
case ActorIsolation::Unspecified:
48074807
case ActorIsolation::Nonisolated:
4808-
case ActorIsolation::NonisolatedUnsafe:
4808+
case ActorIsolation::NonisolatedUnsafe: {
4809+
auto &ctx = classDecl->getASTContext();
4810+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability))
4811+
return false;
4812+
48094813
downgradeToWarning = true;
48104814
break;
4815+
}
48114816

48124817
case ActorIsolation::Erased:
48134818
llvm_unreachable("class cannot have erased isolation");
@@ -6046,6 +6051,13 @@ ProtocolConformance *swift::deriveImplicitSendableConformance(
60466051
inheritedConformance.getConcrete());
60476052
}
60486053
}
6054+
6055+
// Classes that add global actor isolation to non-Sendable
6056+
// superclasses cannot be 'Sendable'.
6057+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability) &&
6058+
nominal->getGlobalActorAttr()) {
6059+
return nullptr;
6060+
}
60496061
}
60506062
}
60516063

test/Concurrency/actor_isolation.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,14 @@ actor Counter {
12821282
class C2 { }
12831283

12841284
@SomeGlobalActor
1285-
class C3: C2 { }
1286-
// expected-warning@-1 {{global actor 'SomeGlobalActor'-isolated class 'C3' has different actor isolation from nonisolated superclass 'C2'; this is an error in the Swift 6 language mode}}
1285+
class C3: C2 { // expected-note {{class 'C3' does not conform to the 'Sendable' protocol}}
1286+
func requireSendableSelf() {
1287+
Task.detached {
1288+
_ = self
1289+
// expected-warning@-1 {{capture of 'self' with non-sendable type 'C3' in a `@Sendable` closure; this is an error in the Swift 6 language mode}}
1290+
}
1291+
}
1292+
}
12871293

12881294
@GenericGlobalActor<U>
12891295
class GenericSuper<U> { }
@@ -1463,7 +1469,6 @@ class None {
14631469
// try to add inferred isolation while overriding
14641470
@MainActor
14651471
class MA_None1: None {
1466-
// expected-warning@-1 {{main actor-isolated class 'MA_None1' has different actor isolation from nonisolated superclass 'None'; this is an error in the Swift 6 language mode}}
14671472

14681473
// FIXME: bad note, since the problem is a mismatch in overridden vs inferred isolation; this wont help.
14691474
// expected-note@+1 {{add '@MainActor' to make instance method 'method()' part of global actor 'MainActor'}}
@@ -1494,7 +1499,6 @@ class None_MADirect: MADirect {
14941499

14951500
@SomeGlobalActor
14961501
class SGA_MADirect: MADirect {
1497-
// expected-warning@-1 {{global actor 'SomeGlobalActor'-isolated class 'SGA_MADirect' has different actor isolation from nonisolated superclass 'MADirect'; this is an error in the Swift 6 language mode}}
14981502

14991503
// inferred-SomeGlobalActor vs overridden-MainActor = mainactor
15001504
override func method1() { beets_ma() }

test/Concurrency/global_actor_inference.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ class SuperclassWithGlobalActors {
248248

249249
@GenericGlobalActor<String>
250250
class SubclassWithGlobalActors : SuperclassWithGlobalActors {
251-
// expected-warning@-1 {{global actor 'GenericGlobalActor<String>'-isolated class 'SubclassWithGlobalActors' has different actor isolation from nonisolated superclass 'SuperclassWithGlobalActors'; this is an error in the Swift 6 language mode}}
252-
253251
override func f() { } // okay: inferred to @GenericGlobalActor<Int>
254252

255253
@GenericGlobalActor<String> override func g() { } // expected-error{{global actor 'GenericGlobalActor<String>'-isolated instance method 'g()' has different actor isolation from global actor 'GenericGlobalActor<Int>'-isolated overridden declaration}}

0 commit comments

Comments
 (0)