Skip to content

enforce under strict concurrency that globals and statics be either… #67974

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

Conversation

sophiapoirier
Copy link
Contributor

…isolated to a global actor or Sendable plus immutable

rdar://81629027 Global and static variable data-race safety

break;
}
}
auto inferredIsolation = [&](ActorIsolation inferred,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inlining an immediately invoked lambda added an extra level of indentation to the body of inferredIsolation, so if you filter white space from the diff, it should reduce it significantly.

var->isStatic() || var->getDeclContext()->isModuleScopeContext() ||
(var->getDeclContext()->isTypeContext() && !var->isInstanceMember());
auto *classDecl = var->getDeclContext()->getSelfClassDecl();
const bool isActorType = classDecl && classDecl->isActor();
Copy link
Contributor

@ktoso ktoso Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using isAnyActor I think?

edit: Hmmm, ah no. Since this is just about global actors -- could use a comment why we don't care about distributed actor here though, mind adding one? I.e. because distributed actors cannot be global (at least not today)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I don't think it would hurt to make it isAnyActor. Distributed actors won't reach this code path at this point anyway, since they can't be global, but if someday they can, then things will continue to work (and no explanatory comment required).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah maybe can use the isAnyActor right away, your call :)

Thank you!

@@ -5243,6 +5243,12 @@ NOTE(actor_mutable_state,none,
WARNING(shared_mutable_state_access,none,
"reference to %kind0 is not concurrency-safe because it involves "
"shared mutable state", (const ValueDecl *))
ERROR(shared_mutable_state_decl,none,
"%kind0 is not concurrency-safe because it is involves shared mutable "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"%kind0 is not concurrency-safe because it is involves shared mutable "
"%kind0 is not concurrency-safe because it involves shared mutable "

@TestGlobalActor
var mutableIsolatedGlobal = 1

var mutableNonisolatedGlobal = 1 // expected-error{{var 'mutableNonisolatedGlobal' is not concurrency-safe because it is involves shared mutable state}}
Copy link
Contributor

@ktoso ktoso Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd nitpick the error a bit to be honest "involves" is not quite right here.

  • maybe "variable 'mutableNonisolatedGlobal' is not concurrency-safe because it is global shared mutable state"
  • consider "variable" vs "var" I don't think we use short names like that in messages much
  • consider adding a note that can explain what people can do about it?
    • maybe "note: isolate this property using a global actor, or convert it to a constant"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The var part is generated via %kind0 however otherwise making these changes, thanks for the improvement suggestions.

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message wording nitpick but LGTM otherwise 👍
Nice to hide under a flag for now 👍

@sophiapoirier sophiapoirier force-pushed the global-static-data-race-safety branch 3 times, most recently from 0dc2b14 to c4313ea Compare August 17, 2023 02:02
@swiftlang swiftlang deleted a comment from tshortli Aug 17, 2023
@sophiapoirier sophiapoirier force-pushed the global-static-data-race-safety branch from 4fedd08 to 30435bb Compare August 17, 2023 16:17
// Declarations inside types are handled in checkConformancesInContext() to
// avoid cycles involving associated type inference.
if (!VD->getDeclContext()->isTypeContext()) {
(void)getActorIsolation(VD);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually just realized that this invocation of getActorIsolation() is redundant since it is already called on all ValueDecl instances during DeclChecker::visit() (from which this method is called). About to push a commit removing this...

@sophiapoirier sophiapoirier force-pushed the global-static-data-race-safety branch from 807ca8d to 137a6d8 Compare August 17, 2023 21:29
var mutableIsolatedGlobal = 1

var mutableNonisolatedGlobal = 1 // expected-error{{var 'mutableNonisolatedGlobal' is not concurrency-safe because it is global shared mutable state}}
// expected-note@-1{{isolate 'mutableNonisolatedGlobal' to a global actor, or convert it to a constant}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be useful to produce a fix-it to make var into let?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your feedback is making me realize that the diagnostics could be improved by being more detailed, and I just completed some rewordings. I think however offering this fixit is complicated by the fact that there are two potential solutions here (either isolating to a main actor, or making a constant that is also 'Sendable', and Sensibility was something that you made me realize is missing from this note), and we don't automatically know which would be most suitable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, yeah it makes sense and new diagnostic with Sendable bit sounds good =]

…solated to a global actor or Sendable plus immutable

rdar://81629027 Global and static variable data-race safety
@sophiapoirier sophiapoirier force-pushed the global-static-data-race-safety branch from dd0efd0 to 936ab20 Compare August 18, 2023 22:08
@sophiapoirier
Copy link
Contributor Author

@swift-ci please test

@sophiapoirier sophiapoirier merged commit aec59f2 into swiftlang:main Aug 19, 2023
@sophiapoirier sophiapoirier deleted the global-static-data-race-safety branch August 19, 2023 15:28
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.

4 participants