Skip to content

[6.2][Concurrency] SE-0449: Fix a few issues related to nonisolated type declarations #82324

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 5 commits into from
Jun 18, 2025
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
47 changes: 44 additions & 3 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5148,6 +5148,13 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
if (dc->getSelfProtocolDecl())
return std::nullopt;

// Prevent isolation inference from requirements if the conforming type
// has an explicit `nonisolated` attribute.
if (auto *NTD = dc->getSelfNominalTypeDecl()) {
if (NTD->getAttrs().hasAttribute<NonisolatedAttr>())
return std::nullopt;
}

// Walk through each of the conformances in this context, collecting any
// requirements that have actor isolation.
auto conformances = idc->getLocalConformances( // note this
Expand All @@ -5156,6 +5163,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 @@ -5165,6 +5177,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 @@ -5276,19 +5301,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
128 changes: 127 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,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 Expand Up @@ -144,6 +242,34 @@ nonisolated class K: GloballyIsolated {
}
}

@MainActor
protocol GloballyIsolatedWithRequirements {
var x: NonSendable { get set } // expected-note {{property declared here}}
func test() // expected-note {{calls to instance method 'test()' from outside of its actor context are implicitly asynchronous}}
}

nonisolated class K2: GloballyIsolatedWithRequirements {
var x: NonSendable

func test() {}

func testNonWitness() {}

init(x: NonSendable) {
self.x = x // okay
test() // okay
testNonWitness() // okay
}

func test<T: GloballyIsolatedWithRequirements>(t: T, s: K2) {
_ = s.x // okay
_ = t.x // expected-error {{main actor-isolated property 'x' can not be referenced from a nonisolated context}}

s.test() // okay
t.test() // expected-error {{call to main actor-isolated instance method 'test()' in a synchronous nonisolated context}}
}
}

// MARK: - Storage of non-Sendable

class KlassA {
Expand Down