Skip to content

Commit b30cc9d

Browse files
authored
[Sema] Diagnose 'nonisolated' attribute on wrapped properties (#60475)
* [Sema] Diagnose 'nonisolated' attribute on wrapped properties * [AST] Update diagnostic wording for nonisolated_wrapped_property
1 parent 42135e2 commit b30cc9d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,9 @@ ERROR(nonisolated_local_var,none,
47884788
ERROR(nonisolated_actor_sync_init,none,
47894789
"'nonisolated' on an actor's synchronous initializer is invalid",
47904790
())
4791+
ERROR(nonisolated_wrapped_property,none,
4792+
"'nonisolated' is not supported on properties with property wrappers",
4793+
())
47914794

47924795
ERROR(actor_instance_property_wrapper,none,
47934796
"%0 property in property wrapper type %1 cannot be isolated to "

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6026,6 +6026,14 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
60266026
}
60276027
}
60286028

6029+
// Using 'nonisolated' with wrapped properties is unsupported, because
6030+
// backing storage is a stored 'var' that is part of the internal state
6031+
// of the actor which could only be accessed in actor's isolation context.
6032+
if (var->hasAttachedPropertyWrapper()) {
6033+
diagnoseAndRemoveAttr(attr, diag::nonisolated_wrapped_property);
6034+
return;
6035+
}
6036+
60296037
// nonisolated can not be applied to local properties.
60306038
if (dc->isLocalContext()) {
60316039
diagnoseAndRemoveAttr(attr, diag::nonisolated_local_var);

test/Concurrency/global_actor_inference.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,23 @@ func testInferredFromWrapper(x: InferredFromPropertyWrapper) { // expected-note{
457457
_ = x.test() // expected-error{{call to global actor 'SomeGlobalActor'-isolated instance method 'test()' in a synchronous nonisolated context}}
458458
}
459459

460+
@propertyWrapper
461+
struct SimplePropertyWrapper {
462+
var wrappedValue: Int { .zero }
463+
var projectedValue: Int { .max }
464+
}
465+
466+
@MainActor
467+
class WrappedContainsNonisolatedAttr {
468+
@SimplePropertyWrapper nonisolated var value
469+
// expected-error@-1 {{'nonisolated' is not supported on properties with property wrappers}}
470+
// expected-note@-2 2{{property declared here}}
471+
472+
nonisolated func test() {
473+
_ = value // expected-error {{main actor-isolated property 'value' can not be referenced from a non-isolated context}}
474+
_ = $value // expected-error {{main actor-isolated property '$value' can not be referenced from a non-isolated context}}
475+
}
476+
}
460477

461478

462479
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)