Skip to content

Commit 7cc2afd

Browse files
committed
[Concurrency] Fix an issue where nonisolated(unsafe) did not suppress
`Sendable` warnings on stored properties of `Sendable` types.
1 parent ba76c3c commit 7cc2afd

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5490,13 +5490,18 @@ static bool checkSendableInstanceStorage(
54905490

54915491
/// Handle a stored property.
54925492
bool operator()(VarDecl *property, Type propertyType) override {
5493+
ActorIsolation isolation = getActorIsolation(property);
5494+
5495+
// 'nonisolated' properties are always okay in 'Sendable' types because
5496+
// they can be accessed from anywhere. Note that 'nonisolated' without
5497+
// '(unsafe)' can only be applied to immutable, 'Sendable' properties.
5498+
if (isolation.isNonisolated())
5499+
return false;
5500+
54935501
// Classes with mutable properties are Sendable if property is
54945502
// actor-isolated
54955503
if (isa<ClassDecl>(nominal)) {
5496-
ActorIsolation isolation = getActorIsolation(property);
5497-
5498-
if (property->supportsMutation() &&
5499-
(isolation.isNonisolated() || isolation.isUnspecified())) {
5504+
if (property->supportsMutation() && isolation.isUnspecified()) {
55005505
auto behavior =
55015506
SendableCheckContext(dc, check).defaultDiagnosticBehavior();
55025507
if (behavior != DiagnosticBehavior::Ignore) {

test/Concurrency/sendable_checking.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,10 @@ enum SynthesizedConformances {
361361
let x: NotSendable
362362
}
363363
}
364+
365+
@available(SwiftStdlib 5.1, *)
366+
final class UseNonisolatedUnsafe: Sendable {
367+
nonisolated(unsafe) var x1: NonSendable = .init()
368+
nonisolated(unsafe) let x2: NonSendable = .init()
369+
nonisolated(unsafe) var x3: Int = 0
370+
}

0 commit comments

Comments
 (0)