Skip to content

[6.0] Teach #isolation to respect the flow-sensitive nature of actor initializers #74245

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

DougGregor
Copy link
Member

Explanation: Perform flow-sensitive lowering of #isolation within actor initializers so that it lowers to nil before the actor is fully-initialized and self (or self.asLocalActor in the distributed case) after the actor is fully-initialized.
Original PR: #74225
Radar/issue: rdar://127080037
Reviewed by: @ktoso
Risk: Low. Only affects actor initializers that use #isolation or the asynchronous for-in loop on new-enough platforms (where the new AsyncIteratorProtocol.next(isolation:) is present).

…ializers

Actor initializers have a flow-sensitive property where they are isolated
to the actor being initialized only after the actor instance itself is
fully-initialized. However, this behavior was not being reflected in
the expansion of `#isolation`, which was always expanding to `self`,
even before `self` is fully formed.

This led to a source compatibility issue with code that used the async
for..in loop within an actor initializer *prior* to the point where the
actor was fully initialized, because the type checker is introducing
the `#isolation` (SE-0421) but Definite Initialization properly rejects
the use of `self` before it is initialized.

Address this issue by delaying the expansion of `#isolation` until
after the actor is fully initialized. In SILGen, we introduce a new
builtin for this case (and *just* this case) called
`flowSensitiveSelfIsolation`, which takes in `self` as its argument
and produces an `(any Actor)?`. Definite initialization does not treat
this as a use of `self`. Rather, it tracks these builtins and
replaces them either with `self` (if it is fully-initialized at this
point) or `nil` (if it is not fully-initialized at this point),
mirroring the flow-sensitive isolation semantics described in SE-0327.

Fixes rdar://127080037.
Distributed actors can be treated as actors by accessing the `asLocalActor`
property. When lowering `#isolation` in a distributed actor initializer,
use a separate builtin `flowSensitiveDistributedSelfIsolation` to
capture the conformance to `DistributedActor`, and have Definite
Initialization introduce the call to the `asLocalActor` getter when
needed.
@DougGregor DougGregor requested a review from a team as a code owner June 10, 2024 05:57
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

Hmph, failures are unrelated to this change.

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test macOS

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor enabled auto-merge June 11, 2024 20:39
@DougGregor DougGregor merged commit e259c7b into swiftlang:release/6.0 Jun 12, 2024
5 checks passed
@DougGregor DougGregor deleted the flow-sensitive-actor-init-isolation-6.0 branch June 12, 2024 05:41
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