[coro] Add @:coroutine.scope and @:coroutine.restrictedSuspension#12594
Open
Simn wants to merge 4 commits intodevelopmentfrom
Open
[coro] Add @:coroutine.scope and @:coroutine.restrictedSuspension#12594Simn wants to merge 4 commits intodevelopmentfrom
Simn wants to merge 4 commits intodevelopmentfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We need just a little bit more compiler support for hxcoro that relates to coroutine scopes. This gives special semantics to the first argument of a
@:coroutinefunction, which works like this:@:coroutine.scope
This now gives an error:
You can think of these scopes as some kind of "coroutine-this", where keeping an orderly structure is important. This is relevant for structured concurrency where we really don't want users to accidentally have children mess with their parent's scope. For example, I've had code like this before:
Since the child coro doesn't shadow
node, this accesses the parent's node in its body, which is not very structured. With this PR there's now a proper error.Note that this only restricts access if there exists a nested scope, so normal capturing in closures still works as expected, which includes coroutines that don't have a scope:
@:coroutine.restrictedSuspension
This meta implies
@:coroutine.scopeand restricts which coroutines we're allowed to call:In the current implementation it allows all
@:coroutinefield access calls on the current scope, as well as direct calls if the scope supports that:There might be more cases we want to allow, which can be looked into later. This feature is necessary in order to support generators which are not supposed to invoke arbitrary coroutines.