Skip to content

Commit 87f49a5

Browse files
committed
[Concurrency] Allow isolation on subclasses of non-Sendable, non-isolated
superclasses.
1 parent e7db0ec commit 87f49a5

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
@@ -4776,9 +4776,14 @@ static bool checkClassGlobalActorIsolation(
47764776
switch (superIsolation) {
47774777
case ActorIsolation::Unspecified:
47784778
case ActorIsolation::Nonisolated:
4779-
case ActorIsolation::NonisolatedUnsafe:
4779+
case ActorIsolation::NonisolatedUnsafe: {
4780+
auto &ctx = classDecl->getASTContext();
4781+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability))
4782+
return false;
4783+
47804784
downgradeToWarning = true;
47814785
break;
4786+
}
47824787

47834788
case ActorIsolation::Erased:
47844789
llvm_unreachable("class cannot have erased isolation");
@@ -6004,6 +6009,13 @@ ProtocolConformance *swift::deriveImplicitSendableConformance(
60046009
inheritedConformance.getConcrete());
60056010
}
60066011
}
6012+
6013+
// Classes that add global actor isolation to non-Sendable
6014+
// superclasses cannot be 'Sendable'.
6015+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability) &&
6016+
nominal->getGlobalActorAttr()) {
6017+
return nullptr;
6018+
}
60076019
}
60086020
}
60096021

test/Concurrency/actor_isolation.swift

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

13101310
@SomeGlobalActor
1311-
class C3: C2 { }
1312-
// 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}}
1311+
class C3: C2 { // expected-note {{class 'C3' does not conform to the 'Sendable' protocol}}
1312+
func requireSendableSelf() {
1313+
Task.detached {
1314+
_ = self
1315+
// 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}}
1316+
}
1317+
}
1318+
}
13131319

13141320
@GenericGlobalActor<U>
13151321
class GenericSuper<U> { }
@@ -1489,7 +1495,6 @@ class None {
14891495
// try to add inferred isolation while overriding
14901496
@MainActor
14911497
class MA_None1: None {
1492-
// 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}}
14931498

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

15211526
@SomeGlobalActor
15221527
class SGA_MADirect: MADirect {
1523-
// 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}}
15241528

15251529
// inferred-SomeGlobalActor vs overridden-MainActor = mainactor
15261530
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)