Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sema] Diagnose 'nonisolated' attribute on wrapped properties #60475

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions test/Concurrency/global_actor_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
}


// ----------------------------------------------------------------------
Expand Down