-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Concurrency] Allow 'nonisolated' to be applied to mutable storage of 'Sendable' type on a globally-isolated value type. #74958
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
Conversation
// 'nonisolated' can not be applied to mutable stored properties unless | ||
// qualified as 'unsafe', or is of a Sendable type on a | ||
// globally-isolated value type. | ||
bool canBeNonisolated = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably have a better name, for now I just put it in a scope.
@@ -7010,18 +7010,32 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) { | |||
|
|||
if (auto var = dyn_cast<VarDecl>(D)) { | |||
// stored properties have limitations as to when they can be nonisolated. | |||
auto type = var->getTypeInContext(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could test that we are checking a contextual vs. interface type with a type decl that conditionally conforms to Sendable
. For example:
@MainActor
struct S {
nonisolated var x: [Int]
}
func test_nonisolated_variable() { | ||
struct S: GloballyIsolatedProto { | ||
nonisolated var x: Int = 0 // okay | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any nonisolated(unsafe)
tests for stored var
?
@@ -138,8 +138,7 @@ struct InferredFromContext { | |||
get { [] } | |||
} | |||
|
|||
nonisolated var status: Bool = true // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}{{3-15=}}{{3-15=}}{{14-14=(unsafe)}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: This error message needs some love now that the rules have changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but it's not obvious to me in what way should we change it. This case of allowing nonisolated
on a mutable storage is too narrow, so updating the general case to include all conditions under which the attribute can be allowed seems too convoluted. I think I can see a possibility of updating this error to something like "'nonisolated' cannot be applied to this mutable stored property", and if it's declared on a globally-isolated value type, emit fix-its like "consider making the property type Sendable". Similarly, in the case where the property is of Sendable type and is on a value type, emit a fix-it saying "consider isolating the value type to the main actor."
Another possibility would be splitting this into 2 diagnostics, one for value type and another for reference types. In that case, we could add all the conditions in which this rule does not apply for the value type case.
In short, I'm just trying to think of a way to make this diagnostic make more sense from usability perspective (e.g. a programmer not familiar with subtleties in concurrency rules should get helpful fix-its)
… 'Sendable' type on a globally-isolated value type.
@swift-ci please smoke test |
rdar://130992526
Under SE-0434,
nonisolated
can be applied to mutable stored properties, as long as the property is on a globally-isolated value type and is of aSendable
type.Additionally, cross-module access should be allowed if such variable is marked
nonisolated
.