Skip to content

Commit 26a1ae7

Browse files
authored
[5.7] [Sema] Diagnose 'nonisolated' attribute on wrapped properties (swiftlang#60499)
1 parent 825d11a commit 26a1ae7

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
@@ -4794,6 +4794,9 @@ ERROR(nonisolated_local_var,none,
47944794
ERROR(nonisolated_actor_sync_init,none,
47954795
"'nonisolated' on an actor's synchronous initializer is invalid",
47964796
())
4797+
ERROR(nonisolated_wrapped_property,none,
4798+
"'nonisolated' is not supported on properties with property wrappers",
4799+
())
47974800

47984801
ERROR(actor_instance_property_wrapper,none,
47994802
"%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
@@ -5938,6 +5938,14 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
59385938
}
59395939
}
59405940

5941+
// Using 'nonisolated' with wrapped properties is unsupported, because
5942+
// backing storage is a stored 'var' that is part of the internal state
5943+
// of the actor which could only be accessed in actor's isolation context.
5944+
if (var->hasAttachedPropertyWrapper()) {
5945+
diagnoseAndRemoveAttr(attr, diag::nonisolated_wrapped_property);
5946+
return;
5947+
}
5948+
59415949
// nonisolated can not be applied to local properties.
59425950
if (dc->isLocalContext()) {
59435951
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
@@ -456,6 +456,23 @@ func testInferredFromWrapper(x: InferredFromPropertyWrapper) { // expected-note{
456456
_ = x.test() // expected-error{{call to global actor 'SomeGlobalActor'-isolated instance method 'test()' in a synchronous nonisolated context}}
457457
}
458458

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

460477

461478
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)