Skip to content

Commit 1af0380

Browse files
committed
[Concurrency] Allow struct members to have isolation annotations.
1 parent c0486c3 commit 1af0380

File tree

5 files changed

+13
-40
lines changed

5 files changed

+13
-40
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,9 +5489,6 @@ ERROR(distributed_actor_remote_func_implemented_manually,none,
54895489
ERROR(nonisolated_distributed_actor_storage,none,
54905490
"'nonisolated' can not be applied to distributed actor stored properties",
54915491
())
5492-
ERROR(nonisolated_storage_value_type,none,
5493-
"'nonisolated' is redundant on %0's stored properties",
5494-
(DescriptiveDeclKind))
54955492
ERROR(distributed_actor_func_nonisolated, none,
54965493
"cannot declare method %0 as both 'nonisolated' and 'distributed'",
54975494
(DeclName))
@@ -5768,9 +5765,6 @@ ERROR(global_actor_on_actor_class,none,
57685765
"actor %0 cannot have a global actor", (Identifier))
57695766
ERROR(global_actor_on_local_variable,none,
57705767
"local variable %0 cannot have a global actor", (DeclName))
5771-
ERROR(global_actor_on_storage_of_value_type,none,
5772-
"stored property %0 within struct cannot have a global actor",
5773-
(DeclName))
57745768
ERROR(unsafe_global_actor,none,
57755769
"'(unsafe)' global actors are deprecated; "
57765770
"use '@preconcurrency' instead",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6909,17 +6909,6 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69096909
diag::nonisolated_distributed_actor_storage);
69106910
return;
69116911
}
6912-
6913-
// 'nonisolated' is redundant for the stored properties of a struct.
6914-
if (isa<StructDecl>(nominal) &&
6915-
!var->isStatic() &&
6916-
var->isOrdinaryStoredProperty() &&
6917-
!isWrappedValueOfPropWrapper(var)) {
6918-
diagnoseAndRemoveAttr(attr, diag::nonisolated_storage_value_type,
6919-
nominal->getDescriptiveKind())
6920-
.warnUntilSwiftVersion(6);
6921-
return;
6922-
}
69236912
}
69246913
}
69256914

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,6 @@ GlobalActorAttributeRequest::evaluate(
421421
.highlight(globalActorAttr->getRangeWithAt());
422422
return std::nullopt;
423423
}
424-
425-
// ... and not if it's the instance storage of a struct
426-
if (isStoredInstancePropertyOfStruct(var)) {
427-
var->diagnose(diag::global_actor_on_storage_of_value_type,
428-
var->getName())
429-
.highlight(globalActorAttr->getRangeWithAt())
430-
.warnUntilSwiftVersion(6);
431-
432-
// In Swift 6, once the diag above is an error, it is disallowed.
433-
if (var->getASTContext().isSwiftVersionAtLeast(6))
434-
return std::nullopt;
435-
}
436424
}
437425
} else if (isa<ExtensionDecl>(decl)) {
438426
// Extensions are okay.

test/Concurrency/actor_isolation.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct InferredFromContext {
144144
nonisolated var status: Bool = true // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}{{3-15=}}{{3-15=}}{{14-14=(unsafe)}}
145145
// expected-note@-1{{convert 'status' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
146146

147-
nonisolated let flag: Bool = false // expected-warning {{'nonisolated' is redundant on struct's stored properties; this is an error in the Swift 6 language mode}}{{3-15=}}
147+
nonisolated let flag: Bool = false
148148

149149
subscript(_ i: Int) -> Int { return i }
150150

@@ -170,15 +170,13 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
170170
_ = await NoGlobalActorValueType.polygon // expected-warning {{non-sendable type '[Point]' in implicitly asynchronous access to main actor-isolated static property 'polygon' cannot cross actor boundary}}
171171
}
172172

173-
// check for instance members that do not need global-actor protection
174-
175173
// expected-warning@+2 {{memberwise initializer for 'NoGlobalActorValueType' cannot be both nonisolated and global actor 'SomeGlobalActor'-isolated; this is an error in the Swift 6 language mode}}
176174
// expected-note@+1 2 {{consider making struct 'NoGlobalActorValueType' conform to the 'Sendable' protocol}}
177175
struct NoGlobalActorValueType {
178-
@SomeGlobalActor var point: Point // expected-warning {{stored property 'point' within struct cannot have a global actor; this is an error in the Swift 6 language mode}}
176+
@SomeGlobalActor var point: Point
179177
// expected-note@-1 {{initializer for property 'point' is global actor 'SomeGlobalActor'-isolated}}
180178

181-
@MainActor let counter: Int // expected-warning {{stored property 'counter' within struct cannot have a global actor; this is an error in the Swift 6 language mode}}
179+
@MainActor let counter: Int
182180

183181
@MainActor static var polygon: [Point] = []
184182
}

test/Concurrency/actor_isolation_swift6.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct InferredFromContext {
3636
get { [] }
3737
}
3838

39-
nonisolated let flag: Bool = false // expected-error {{'nonisolated' is redundant on struct's stored properties}}{{3-15=}}
39+
nonisolated let flag: Bool = false
4040

4141
subscript(_ i: Int) -> Int { return i }
4242

@@ -49,15 +49,20 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
4949
// these do not need an await, since it's a value type
5050
_ = ext.point
5151
_ = formance.counter
52-
_ = anno.point
5352
_ = anno.counter
5453

5554
// make sure it's just a warning if someone was awaiting on it previously
5655
_ = await ext.point // expected-warning {{no 'async' operations occur within 'await' expression}}
5756
_ = await formance.counter // expected-warning {{no 'async' operations occur within 'await' expression}}
58-
_ = await anno.point // expected-warning {{no 'async' operations occur within 'await' expression}}
5957
_ = await anno.counter // expected-warning {{no 'async' operations occur within 'await' expression}}
6058

59+
// We could extend the 'nonisolated within the module' rule to vars
60+
// value types types if the property type is 'Sendable'.
61+
_ = anno.point
62+
// expected-error@-1 {{expression is 'async' but is not marked with 'await'}}
63+
// expected-note@-2 {{property access is 'async'}}
64+
_ = await anno.point
65+
6166
// these do need await, regardless of reference or value type
6267
_ = await (formance as any MainCounter).counter
6368
// expected-error@-1 {{non-sendable type 'any MainCounter' passed in implicitly asynchronous call to main actor-isolated property 'counter' cannot cross actor boundary}}
@@ -68,11 +73,10 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
6873
_ = await NoGlobalActorValueType.polygon
6974
}
7075

71-
// check for instance members that do not need global-actor protection
7276
struct NoGlobalActorValueType {
73-
@SomeGlobalActor var point: ImmutablePoint // expected-error {{stored property 'point' within struct cannot have a global actor}}
77+
@SomeGlobalActor var point: ImmutablePoint
7478

75-
@MainActor let counter: Int // expected-error {{stored property 'counter' within struct cannot have a global actor}}
79+
@MainActor let counter: Int
7680

7781
@MainActor static var polygon: [ImmutablePoint] = []
7882
}

0 commit comments

Comments
 (0)