diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 4581effdbc4d6..eed4a177a1fa3 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -4788,6 +4788,9 @@ ERROR(nonisolated_local_var,none, ERROR(nonisolated_actor_sync_init,none, "'nonisolated' on an actor's synchronous initializer is invalid", ()) +ERROR(nonisolated_wrapped_property,none, + "'nonisolated' is not supported on properties with property wrappers", + ()) ERROR(actor_instance_property_wrapper,none, "%0 property in property wrapper type %1 cannot be isolated to " diff --git a/lib/Sema/TypeCheckAttr.cpp b/lib/Sema/TypeCheckAttr.cpp index bb2244bf8c566..8061f84362915 100644 --- a/lib/Sema/TypeCheckAttr.cpp +++ b/lib/Sema/TypeCheckAttr.cpp @@ -6026,6 +6026,14 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) { } } + // Using 'nonisolated' with wrapped properties is unsupported, because + // backing storage is a stored 'var' that is part of the internal state + // of the actor which could only be accessed in actor's isolation context. + if (var->hasAttachedPropertyWrapper()) { + diagnoseAndRemoveAttr(attr, diag::nonisolated_wrapped_property); + return; + } + // nonisolated can not be applied to local properties. if (dc->isLocalContext()) { diagnoseAndRemoveAttr(attr, diag::nonisolated_local_var); diff --git a/test/Concurrency/global_actor_inference.swift b/test/Concurrency/global_actor_inference.swift index 9fbe0dc0dfc11..7e9e81f744f41 100644 --- a/test/Concurrency/global_actor_inference.swift +++ b/test/Concurrency/global_actor_inference.swift @@ -457,6 +457,23 @@ func testInferredFromWrapper(x: InferredFromPropertyWrapper) { // expected-note{ _ = x.test() // expected-error{{call to global actor 'SomeGlobalActor'-isolated instance method 'test()' in a synchronous nonisolated context}} } +@propertyWrapper +struct SimplePropertyWrapper { + var wrappedValue: Int { .zero } + var projectedValue: Int { .max } +} + +@MainActor +class WrappedContainsNonisolatedAttr { + @SimplePropertyWrapper nonisolated var value + // expected-error@-1 {{'nonisolated' is not supported on properties with property wrappers}} + // expected-note@-2 2{{property declared here}} + + nonisolated func test() { + _ = value // expected-error {{main actor-isolated property 'value' can not be referenced from a non-isolated context}} + _ = $value // expected-error {{main actor-isolated property '$value' can not be referenced from a non-isolated context}} + } +} // ----------------------------------------------------------------------