Skip to content

Commit 796dc92

Browse files
committed
[Concurrency] Never treat nonisolated(unsafe) properties as actor
isolated.
1 parent a7b8cb7 commit 796dc92

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
@@ -7557,9 +7557,19 @@ ActorIsolation swift::getActorIsolationForReference(ValueDecl *decl,
75577557
// the access is outside the module or if the property type is not
75587558
// 'Sendable'.
75597559
//
7560+
// Note that this is only allowed for source compatibility reasons.
7561+
// It is generally invalid to write `nonisolated` on an actor `let`
7562+
// if the type of the property is not Sendable. However, older compilers
7563+
// used to allow this, so it's only an error in the Swift 6 language
7564+
// mode, and it is a warning in prior language modes.
7565+
//
75607566
// FIXME: getActorIsolation(decl) should treat these as isolated.
75617567
// FIXME: Expand this out to local variables?
75627568
if (auto var = dyn_cast<VarDecl>(decl)) {
7569+
// 'nonisolated(unsafe)' opts out of actor isolation.
7570+
if (declIsolation.isNonisolatedUnsafe())
7571+
return declIsolation;
7572+
75637573
auto *fromModule = fromDC->getParentModule();
75647574
ActorReferenceResult::Options options = std::nullopt;
75657575
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)