Skip to content

[Concurrency] SE-0449: nonisolated on a type should prevent isolati… #82289

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
Jun 17, 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
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5130,6 +5130,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 Down
28 changes: 28 additions & 0 deletions test/Concurrency/nonisolated_rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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