[Swift 6] use isolated(any) for factory closures #237
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
2.4
update addedSendable
conformance to the factory closures which breaks using GAIT types inContainer
and elsewhere. For example:By adding
isolated(any)
to the factory closures we can explicitly mark which global actor the closure should be isolated on. The above can now be written without any errors or intermediate wrapper types:Some important details to consider. From the 431 evolution proposal it mentions that functions that use
isolated(any)
are assumed to cross an isolation boundary and are thus implicitlyasync
and must beawait
ed. It seems the compiler allows these functions to be assigned to properties whereisolated(any)
is not used and then called without usingawait
. Not sure if this is intended or possibly a compiler bug. The proposal also mentions that when the compiler can detect that an isolation region is not crossed then it seems the function wouldn't need to beawait
ed. All of those conditions would be satisfied if the getter inContainer
is using the GAIT (e.g.@MainActor
) and the factory closure is also using the GAIT. Then if no isolation region is crossed noawait
is needed otherwise anawait
will be required by the compiler.Currently it's possible to write something like this:
and then use it like this:
Which causes a quick quit easter egg. The only way I can think of avoiding this situation is if the calls to the factory closure were always
await
ed on which sort of defeats the purpose of having everything assigned to the same global actor. I'm open to ideas if anyone has any clues of how to force the compiler to recognize the isolation context. It would be nice if this worked:From the 431 proposal:
Perhaps there's something I'm not understanding there or the feature isn't working the way I think that it says it's supposed to.