Skip to content

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

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
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
40 changes: 37 additions & 3 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5138,6 +5138,11 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
std::tuple<ProtocolConformance *, ActorIsolation, ValueDecl *>;
SmallVector<IsolatedRequirement, 2> isolatedRequirements;
for (auto conformance : conformances) {
auto *implied =
conformance->getSourceKind() == ConformanceEntryKind::Implied
? conformance->getImplyingConformance()
: nullptr;

auto protocol = conformance->getProtocol();
for (auto found : protocol->lookupDirect(value->getName())) {
if (!isa<ProtocolDecl>(found->getDeclContext()))
Expand All @@ -5147,6 +5152,19 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
if (!requirement || isa<TypeDecl>(requirement))
continue;

// The conformance implied by an explicitly stated nonisolated protocol
// makes all of the requirements nonisolated.
if (implied &&
implied->getSourceKind() == ConformanceEntryKind::Explicit) {
auto protocol = implied->getProtocol();
if (protocol->getAttrs().hasAttribute<NonisolatedAttr>()) {
isolatedRequirements.push_back(IsolatedRequirement{
implied, ActorIsolation::forNonisolated(/*unsafe=*/false),
requirement});
continue;
}
}

auto requirementIsolation = getActorIsolation(requirement);
switch (requirementIsolation) {
case ActorIsolation::ActorInstance:
Expand Down Expand Up @@ -5257,19 +5275,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
100 changes: 99 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 @@ -98,12 +98,110 @@ nonisolated struct S1: GloballyIsolated {
// MARK: - Protocols

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

nonisolated protocol NonisolatedWithMembers {
func test()
}

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()
}
}

@MainActor
struct TestIsolated : NonisolatedWithMembers {
var x: NonSendable // expected-note {{property declared here}}

// requirement behaves as if it's explicitly `nonisolated` which gets inferred onto the witness
func test() {
_ = x // expected-error {{main actor-isolated property 'x' can not be referenced from a nonisolated context}}
}
}

@MainActor
protocol Root {
func testRoot()
}

nonisolated protocol Child : Root {
func testChild()
}

struct TestDifferentLevels : Child {
func testRoot() {}
func testChild() {}
func testNonWitness() {}
}

nonisolated func testRequirementsOnMultipleNestingLevels(t: TestDifferentLevels) {
t.testRoot() // okay
t.testChild() // okay
t.testNonWitness() // okay
}

// MARK: - Extensions
Expand Down