Skip to content

Commit b8d5e4c

Browse files
committed
[ConstraintSystem] InferSendableFromCaptures: Use of actor isolated members makes key path non-Sendable
1 parent 6f981e5 commit b8d5e4c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7551,6 +7551,20 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
75517551
if (!storage)
75527552
return fail();
75537553

7554+
switch (getActorIsolation(storage)) {
7555+
case ActorIsolation::Unspecified:
7556+
case ActorIsolation::Nonisolated:
7557+
case ActorIsolation::NonisolatedUnsafe:
7558+
break;
7559+
7560+
// A reference to an actor isolated state make key path non-Sendable.
7561+
case ActorIsolation::ActorInstance:
7562+
case ActorIsolation::GlobalActor:
7563+
case ActorIsolation::GlobalActorUnsafe:
7564+
isSendable = false;
7565+
break;
7566+
}
7567+
75547568
if (isReadOnlyKeyPathComponent(storage, component.getLoc())) {
75557569
mutability = KeyPathMutability::ReadOnly;
75567570
continue;

test/Concurrency/sendable_keypaths.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,19 @@ do {
138138

139139
_ = Test(obj: "Hello").utf8.count // Ok
140140
}
141+
142+
// Global actor isolated properties.
143+
do {
144+
@MainActor struct Isolated {
145+
var data: Int = 42
146+
subscript(v: Int) -> Bool { false }
147+
}
148+
149+
let dataKP = \Isolated.data
150+
let subscriptKP = \Isolated.[42]
151+
152+
let _: KeyPath<Isolated, Int> & Sendable = dataKP
153+
// expected-warning@-1 {{type 'WritableKeyPath<Isolated, Int>' does not conform to the 'Sendable' protocol}}
154+
let _: KeyPath<Isolated, Bool> & Sendable = subscriptKP
155+
// expected-warning@-1 {{type 'KeyPath<Isolated, Bool>' does not conform to the 'Sendable' protocol}}
156+
}

0 commit comments

Comments
 (0)