-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Restrict uses of 'self' in actor initializers #37075
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
50e9e5f
to
d3a7ea4
Compare
d3a7ea4
to
61e7f76
Compare
@swift-ci please smoke test |
1efbe77
to
b2db522
Compare
351bcf7
to
c696312
Compare
@swift-ci please smoke test |
c696312
to
cdba933
Compare
@swift-ci please smoke test |
cdba933
to
af36655
Compare
@swift-ci please smoke test |
if (auto decl = | ||
dyn_cast_or_null<ClassDecl>(getASTType()->getAnyNominal())) | ||
// is it for an actor? | ||
if (decl->isActor() && !decl->isDistributedActor()) // FIXME(78484431) skip distributed actors for now, until their initializers are fixed! |
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.
ok :)
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.
LGTM AFAICS :-)
Turns out that I missed one case of escaping:
Working on getting the above rejected as an error on that store of |
A uniqueness rule for 'self' is enforced throughout the following types of actor initializers: 1. synchronous 2. global-actor isolated This means that 'self' cannot be used for anything, even after 'self' is fully initialized, other than for access to stored properties. Method calls are considered escaping uses of 'self', since it is passed implicitly to the method of computed property when invoked. Also, an actor's convenience init now treats self as nonisolated. This provides a way to perform follow-up work after self is fully initialized, when creating the actor from a synchronous context. Resolves rdar://78790369
2280fcd
to
f722ccd
Compare
@swift-ci please smoke test and merge |
Fixes the following:
Actors permit synchronous initializers, and initializers isolated to a global-actor, which makes it difficult to both create the actor-instance and switch to it within the initializer function. Thus, data races can be created with the initializer. This PR fixes this by broadly restricting the uses of
self
in such initializers to ensure that one unique reference toself
exists throughout theinit
.When I added support for async initializers, I deferred the appropriate insertion ofDeferred to a later PRhop_to_executor
in actor initializers because it's not so simple to determine where that should take place. This PR fixes that and emits the hop.Resolves rdar://76620928