Skip to content

Commit 7e5a57a

Browse files
committed
Guard the changes behind an experimental feature flag.
1 parent 6ff022d commit 7e5a57a

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ EXPERIMENTAL_FEATURE(PreambleMacros, false)
232232
EXPERIMENTAL_FEATURE(TupleConformances, false)
233233
EXPERIMENTAL_FEATURE(FullTypedThrows, false)
234234
EXPERIMENTAL_FEATURE(SameElementRequirements, false)
235+
EXPERIMENTAL_FEATURE(GlobalActorInferenceCutoff, false)
235236

236237
// Whether to enable @_used and @_section attributes
237238
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ UNINTERESTING_FEATURE(FixedArrays)
205205
UNINTERESTING_FEATURE(GroupActorErrors)
206206
UNINTERESTING_FEATURE(SameElementRequirements)
207207
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
208+
UNINTERESTING_FEATURE(GlobalActorInferenceCutoff)
208209

209210
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
210211
auto isFunctionTypeWithSending = [](Type type) {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7127,6 +7127,15 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
71277127
// 'nonisolated' can be applied to global and static/class variables
71287128
// that do not have storage.
71297129
auto dc = D->getDeclContext();
7130+
auto &ctx = D->getASTContext();
7131+
7132+
if (!ctx.LangOpts.hasFeature(Feature::GlobalActorInferenceCutoff)) {
7133+
if (isa<ProtocolDecl>(D) || isa<ExtensionDecl>(D) || isa<ClassDecl>(D) ||
7134+
isa<StructDecl>(D) || isa<EnumDecl>(D)) {
7135+
diagnoseAndRemoveAttr(attr, diag::invalid_decl_modifier, attr);
7136+
return;
7137+
}
7138+
}
71307139

71317140
// nonisolated(unsafe) is unsafe, but only under strict concurrency.
71327141
if (attr->isUnsafe() &&
@@ -7149,12 +7158,14 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
71497158
}
71507159
}
71517160

7152-
// Additionally, a stored property of a non-'Sendable' type can be
7153-
// explicitly marked 'nonisolated'.
7154-
if (auto parentDecl = dc->getDeclaredTypeInContext())
7155-
if (!parentDecl->isSendableType()) {
7156-
canBeNonisolated = true;
7157-
}
7161+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorInferenceCutoff)) {
7162+
// Additionally, a stored property of a non-'Sendable' type can be
7163+
// explicitly marked 'nonisolated'.
7164+
if (auto parentDecl = dc->getDeclaredTypeInContext())
7165+
if (!parentDecl->isSendableType()) {
7166+
canBeNonisolated = true;
7167+
}
7168+
}
71587169

71597170
// Otherwise, this stored property has to be qualified as 'unsafe'.
71607171
if (var->supportsMutation() && !attr->isUnsafe() && !canBeNonisolated) {
@@ -7166,12 +7177,15 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
71667177

71677178
// 'nonisolated' without '(unsafe)' is not allowed on non-Sendable
71687179
// variables, unless they are a member of a non-'Sendable' type.
7169-
if (!attr->isUnsafe() && !type->hasError() && !canBeNonisolated) {
7170-
bool diagnosed = diagnoseIfAnyNonSendableTypes(
7171-
type, SendableCheckContext(dc), Type(), SourceLoc(),
7172-
attr->getLocation(), diag::nonisolated_non_sendable);
7173-
if (diagnosed)
7174-
return;
7180+
if (!attr->isUnsafe() && !type->hasError()) {
7181+
if (!(canBeNonisolated &&
7182+
ctx.LangOpts.hasFeature(Feature::GlobalActorInferenceCutoff))) {
7183+
bool diagnosed = diagnoseIfAnyNonSendableTypes(
7184+
type, SendableCheckContext(dc), Type(), SourceLoc(),
7185+
attr->getLocation(), diag::nonisolated_non_sendable);
7186+
if (diagnosed)
7187+
return;
7188+
}
71757189
}
71767190
}
71777191

test/Concurrency/nonisolated_rules.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %target-swift-frontend -disable-availability-checking -swift-version 6 -parse-as-library %s -emit-sil -o /dev/null -verify
2-
// RUN: %target-swift-frontend -disable-availability-checking -swift-version 6 -parse-as-library %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
1+
// RUN: %target-swift-frontend -disable-availability-checking -swift-version 6 -enable-experimental-feature GlobalActorInferenceCutoff -parse-as-library %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
32

43
// REQUIRES: concurrency
54
// REQUIRES: asserts
@@ -22,8 +21,6 @@ struct ImplicitlySendable {
2221
// always okay
2322
nonisolated let b = 0
2423
nonisolated var c: Int { 0 }
25-
26-
// okay
2724
nonisolated var d = 0
2825

2926
// never okay
@@ -37,10 +34,7 @@ struct ImplicitlyNonSendable {
3734
// always okay
3835
nonisolated let b = 0
3936
nonisolated var c: Int { 0 }
40-
41-
// not okay
42-
nonisolated var d = 0 // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
43-
// expected-note@-1 {{convert 'd' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
37+
nonisolated var d = 0
4438

4539
// never okay
4640
nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}}
@@ -51,8 +45,6 @@ public struct PublicSendable: Sendable {
5145
// always okay
5246
nonisolated let b = 0
5347
nonisolated var c: Int { 0 }
54-
55-
// okay
5648
nonisolated var d = 0
5749

5850
// never okay
@@ -64,10 +56,7 @@ public struct PublicNonSendable {
6456
// always okay
6557
nonisolated let b = 0
6658
nonisolated var c: Int { 0 }
67-
68-
// not okay
69-
nonisolated var d = 0 // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
70-
// expected-note@-1 {{convert 'd' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
59+
nonisolated var d = 0
7160

7261
// never okay
7362
nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}}

0 commit comments

Comments
 (0)