Skip to content

[Concurrency] Allow isolation on subclasses of non-Sendable, non-isolated superclasses. #73497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4776,9 +4776,14 @@ static bool checkClassGlobalActorIsolation(
switch (superIsolation) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::NonisolatedUnsafe: {
auto &ctx = classDecl->getASTContext();
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability))
return false;

downgradeToWarning = true;
break;
}

case ActorIsolation::Erased:
llvm_unreachable("class cannot have erased isolation");
Expand Down Expand Up @@ -6004,6 +6009,13 @@ ProtocolConformance *swift::deriveImplicitSendableConformance(
inheritedConformance.getConcrete());
}
}

// Classes that add global actor isolation to non-Sendable
// superclasses cannot be 'Sendable'.
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability) &&
nominal->getGlobalActorAttr()) {
return nullptr;
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,14 @@ actor Counter {
class C2 { }

@SomeGlobalActor
class C3: C2 { }
// 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}}
class C3: C2 { // expected-note {{class 'C3' does not conform to the 'Sendable' protocol}}
func requireSendableSelf() {
Task.detached {
_ = self
// 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}}
}
}
}

@GenericGlobalActor<U>
class GenericSuper<U> { }
Expand Down Expand Up @@ -1489,7 +1495,6 @@ class None {
// try to add inferred isolation while overriding
@MainActor
class MA_None1: None {
// 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}}

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

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

// inferred-SomeGlobalActor vs overridden-MainActor = mainactor
override func method1() { beets_ma() }
Expand Down
2 changes: 0 additions & 2 deletions test/Concurrency/global_actor_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ class SuperclassWithGlobalActors {

@GenericGlobalActor<String>
class SubclassWithGlobalActors : SuperclassWithGlobalActors {
// 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}}

override func f() { } // okay: inferred to @GenericGlobalActor<Int>

@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}}
Expand Down