Skip to content

[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

Merged
merged 1 commit into from
Jul 5, 2024

Conversation

simanerush
Copy link
Member

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 a Sendable type.

Additionally, cross-module access should be allowed if such variable is marked nonisolated.

@simanerush simanerush requested review from xedin, dduan and hborla July 4, 2024 00:31
// '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;
Copy link
Member Author

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();
Copy link
Collaborator

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]
}

Comment on lines +1245 to +1248
func test_nonisolated_variable() {
struct S: GloballyIsolatedProto {
nonisolated var x: Int = 0 // okay
}
Copy link
Collaborator

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)}}
Copy link
Collaborator

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.

Copy link
Member Author

@simanerush simanerush Jul 4, 2024

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.
@simanerush
Copy link
Member Author

@swift-ci please smoke test

@simanerush simanerush merged commit 549566e into swiftlang:main Jul 5, 2024
3 checks passed
@simanerush simanerush deleted the 130992526 branch July 5, 2024 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants