Skip to content

Commit eaf00fe

Browse files
authored
Merge pull request #80330 from hborla/nonisolated-unsafe-let
[Concurrency] Never treat `nonisolated(unsafe)` properties as actor isolated.
2 parents 367e5e2 + 796dc92 commit eaf00fe

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,9 +7539,19 @@ ActorIsolation swift::getActorIsolationForReference(ValueDecl *decl,
75397539
// the access is outside the module or if the property type is not
75407540
// 'Sendable'.
75417541
//
7542+
// Note that this is only allowed for source compatibility reasons.
7543+
// It is generally invalid to write `nonisolated` on an actor `let`
7544+
// if the type of the property is not Sendable. However, older compilers
7545+
// used to allow this, so it's only an error in the Swift 6 language
7546+
// mode, and it is a warning in prior language modes.
7547+
//
75427548
// FIXME: getActorIsolation(decl) should treat these as isolated.
75437549
// FIXME: Expand this out to local variables?
75447550
if (auto var = dyn_cast<VarDecl>(decl)) {
7551+
// 'nonisolated(unsafe)' opts out of actor isolation.
7552+
if (declIsolation.isNonisolatedUnsafe())
7553+
return declIsolation;
7554+
75457555
auto *fromModule = fromDC->getParentModule();
75467556
ActorReferenceResult::Options options = std::nullopt;
75477557
if (varIsSafeAcrossActors(fromModule, var, declIsolation, std::nullopt, options) &&

test/Concurrency/nonisolated_rules.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ final class KlassB: Sendable {
166166
// expected-error@+1 {{'nonisolated' cannot be applied to mutable stored properties}}
167167
nonisolated var test: Int = 1
168168
}
169+
170+
class NotSendable {}
171+
172+
@MainActor
173+
struct UnsafeInitialization {
174+
nonisolated(unsafe) let ns: NotSendable
175+
176+
nonisolated init(ns: NotSendable) {
177+
self.ns = ns // okay
178+
}
179+
}

0 commit comments

Comments
 (0)