Skip to content

Commit b3633b9

Browse files
committed
[Concurrency] Remove the global actor check when allowing 'nonisolated' on mutable Sendable storage.
1 parent 2226bbf commit b3633b9

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6941,15 +6941,13 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69416941
if (var->hasStorage()) {
69426942
{
69436943
// 'nonisolated' can not be applied to mutable stored properties unless
6944-
// qualified as 'unsafe', or is of a Sendable type on a
6945-
// globally-isolated value type.
6944+
// qualified as 'unsafe', or is of a Sendable type on a Sendable
6945+
// value type.
69466946
bool canBeNonisolated = false;
6947-
if (dc->isTypeContext()) {
6948-
if (auto nominal = dc->getSelfStructDecl()) {
6949-
if (!var->isStatic() && type->isSendableType() &&
6950-
getActorIsolation(nominal).isGlobalActor()) {
6951-
canBeNonisolated = true;
6952-
}
6947+
if (auto nominal = dc->getSelfStructDecl()) {
6948+
if (nominal->getDeclaredTypeInContext()->isSendableType() &&
6949+
!var->isStatic() && type->isSendableType()) {
6950+
canBeNonisolated = true;
69536951
}
69546952
}
69556953

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete -parse-as-library %s -emit-sil -o /dev/null -verify
2+
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete -parse-as-library %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
3+
4+
// REQUIRES: concurrency
5+
// REQUIRES: asserts
6+
7+
class NonSendable {}
8+
9+
struct ImplicitlySendable {
10+
var x: Int
11+
nonisolated var y: Int // okay
12+
}
13+
14+
struct ImplicitlyNonSendable {
15+
let x: NonSendable
16+
// expected-note@+1 {{convert 'y' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
17+
nonisolated var y: Int // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
18+
}
19+
20+
public struct PublicSendable: Sendable {
21+
nonisolated var x: Int // okay
22+
}
23+
24+
public struct PublicNonSendable {
25+
// expected-note@+1 {{convert 'x' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
26+
nonisolated var x: Int // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
27+
}

test/Concurrency/nonisolated_access.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
@MainActor
2020
public protocol P {}
2121

22+
@frozen
23+
public struct ImplicitlySendable {
24+
nonisolated public var prop: Bool = true
25+
26+
nonisolated public init() {}
27+
}
28+
2229
public struct S: P {
2330
nonisolated public var x: Int = 0
2431

@@ -32,5 +39,7 @@ actor A {
3239
func test() {
3340
var s = S()
3441
s.x += 0 // okay
42+
var sendable = ImplicitlySendable()
43+
sendable.prop = false // okay
3544
}
3645
}

0 commit comments

Comments
 (0)