Skip to content

[Concurrency] tighten-up rules about isolated generic parameters #68612

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
Sep 19, 2023

Conversation

kavon
Copy link
Member

@kavon kavon commented Sep 19, 2023

[Concurrency] tighten-up rules about isolated generic parameters

We were missing a check for conformance to Actor or DistributedActor
when an isolated parameter's type is a generic parameter.

Previously, if you used a generic parameter constrained to just AnyActor,
you'd crash the compiler in LowerHopToExecutor because it doesn't know
how to obtain the executor for such a value. Since AnyActor has no
unownedExecutor requirement, there's no way to get the executor without
emitting code to do dynamic casts down to Actor or DistributedActor.

Rather than have the compiler silently emit dynamic casting, I figured
it's best to ban it. This forces people to either do the dynamic casts
themselves, or use one of the more specific types to constrain their
parameter.

For other generic parameters, we would silently treat the function
as though it is nonisolated (i.e., as if the isolated wasn't written
on the parameter at all).

resolves rdar://109059544

We were missing a check for conformance to `Actor` or `DistributedActor`
when an isolated parameter's type is a generic parameter.

Previously, if you used a generic parameter constrained to just `AnyActor`,
you'd crash the compiler in LowerHopToExecutor because it doesn't know
how to obtain the executor for such a value. Since `AnyActor` has no
`unownedExecutor` requirement, there's no way to get the executor without
emitting code to do dynamic casts down to `Actor` or `DistributedActor`.

Rather than have the compiler silently emit dynamic casting, I figured
it's best to ban it. This forces people to either do the dynamic casts
themselves, or use one of the more specific types to constrain their
parameter.

For other generic parameters, we would silently treat the function
as though it is nonisolated (i.e., as if the `isolated` wasn't written
on the parameter at all).

resolves rdar://109059544
@kavon kavon force-pushed the generic-params-isolated branch from 11a7952 to 16af507 Compare September 19, 2023 03:28
@kavon
Copy link
Member Author

kavon commented Sep 19, 2023

@swift-ci please test

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.

LGTM, makes sense.

It's pretty unfortunate that AnyActor can't have protocol requirements since it is just a @_marker protocol.

@kavon
Copy link
Member Author

kavon commented Sep 19, 2023

Oh right, I guess dynamic casts aren't even permitted on an AnyActor then.

@kavon kavon merged commit 1d64558 into swiftlang:main Sep 19, 2023
@kavon kavon deleted the generic-params-isolated branch September 19, 2023 18:26
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.

2 participants