Skip to content
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

[Concurrency] Look for explicit 'nonisolated' when getting isolation from protocol conformances. #79893

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5021,19 +5021,35 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
}

auto *proto = conformance->getProtocol();
switch (auto protoIsolation = getActorIsolation(proto)) {
auto inferredIsolation = getInferredActorIsolation(proto);
auto protoIsolation = inferredIsolation.isolation;
switch (protoIsolation) {
case ActorIsolation::ActorInstance:
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::CallerIsolationInheriting:
case ActorIsolation::NonisolatedUnsafe:
break;
case ActorIsolation::Nonisolated:
if (inferredIsolation.source.kind == IsolationSource::Kind::Explicit) {
if (!foundIsolation) {
// We found an explicitly 'nonisolated' protocol.
foundIsolation = {
protoIsolation,
IsolationSource(proto, IsolationSource::Conformance)};
}
continue;
} else {
break;
}

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

case ActorIsolation::GlobalActor:
if (!foundIsolation) {
// If we encountered an explicit globally isolated conformance, allow it
// to override the nonisolated isolation kind.
if (!foundIsolation ||
conformance->getSourceKind() == ConformanceEntryKind::Explicit) {
foundIsolation = {
protoIsolation,
IsolationSource(proto, IsolationSource::Conformance)
Expand Down
65 changes: 64 additions & 1 deletion test/Concurrency/nonisolated_rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public struct PublicNonSendable {
}


nonisolated struct NonisolatedStruct: GloballyIsolated {
nonisolated struct StructRemovesGlobalActor: GloballyIsolated {
var x: NonSendable
var y: Int = 1

Expand Down Expand Up @@ -100,12 +100,75 @@ nonisolated struct S1: GloballyIsolated {
// MARK: - Protocols

nonisolated protocol Refined: GloballyIsolated {}
nonisolated protocol WhyNot {}

struct A: Refined {
var x: NonSendable
init(x: NonSendable) {
self.x = x // okay
}

init() {
self.x = NonSendable()
}

func f() {}
}

@MainActor protocol ExplicitGlobalActor: Refined {}

struct IsolatedA: ExplicitGlobalActor {
// expected-note@+2 {{main actor isolation inferred from conformance to protocol 'ExplicitGlobalActor'}}
// expected-note@+1 {{calls to instance method 'g()' from outside of its actor context are implicitly asynchronous}}
func g() {}
}

struct IsolatedB: Refined, ExplicitGlobalActor {
// expected-note@+2 {{calls to instance method 'h()' from outside of its actor context are implicitly asynchronous}}
// expected-note@+1 {{main actor isolation inferred from conformance to protocol 'ExplicitGlobalActor'}}
func h() {}
}

struct IsolatedC: WhyNot, GloballyIsolated {
// expected-note@+2 {{calls to instance method 'k()' from outside of its actor context are implicitly asynchronous}}
// expected-note@+1 {{main actor isolation inferred from conformance to protocol 'GloballyIsolated'}}
func k() {}
}

struct IsolatedCFlipped: GloballyIsolated, WhyNot {
// expected-note@+2 {{calls to instance method 'k2()' from outside of its actor context are implicitly asynchronous}}
// expected-note@+1 {{main actor isolation inferred from conformance to protocol 'GloballyIsolated'}}
func k2() {}
}

struct NonisolatedStruct {
func callF() {
return A().f() // okay, 'A' is non-isolated.
}

// expected-note@+1 {{add '@MainActor' to make instance method 'callG()' part of global actor 'MainActor'}}
func callG() {
// expected-error@+1{{call to main actor-isolated instance method 'g()' in a synchronous nonisolated context}}
return IsolatedA().g()
}

// expected-note@+1 {{add '@MainActor' to make instance method 'callH()' part of global actor 'MainActor'}}
func callH() {
// expected-error@+1 {{call to main actor-isolated instance method 'h()' in a synchronous nonisolated context}}
return IsolatedB().h()
}

// expected-note@+1 {{add '@MainActor' to make instance method 'callK()' part of global actor 'MainActor'}}
func callK() {
// expected-error@+1 {{call to main actor-isolated instance method 'k()' in a synchronous nonisolated context}}
return IsolatedC().k()
}

// expected-note@+1 {{add '@MainActor' to make instance method 'callK2()' part of global actor 'MainActor'}}
func callK2() {
// expected-error@+1 {{call to main actor-isolated instance method 'k2()' in a synchronous nonisolated context}}
return IsolatedCFlipped().k2()
}
}

// MARK: - Extensions
Expand Down