Skip to content
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

Initial state capture for closures #1611

Merged
merged 5 commits into from
Dec 4, 2022
Merged

Initial state capture for closures #1611

merged 5 commits into from
Dec 4, 2022

Conversation

philberty
Copy link
Member

@philberty philberty commented Oct 21, 2022

This patch set adds the initial support closure captures, move semantics are not
handled here. We track what variables are being captured by a closure during
name resolution so that when a VAR_DECL is resolved, we check if we are inside
a closure context node_id which is the same id as its associated rib id. So when
we resolve a name that resides in an outermost rib we can add this to set of
node-id's that are captured by this closure.

There is a gap here for the case where we need to check if it is inside a nested
function and that function contains closures which could wrongly capture variables
in the enclosing function. This will also be a problem for nested functions in general.

Fixes #195

@philberty philberty added this to the Initial upstream patches milestone Oct 21, 2022
@philberty philberty requested a review from CohenArthur October 21, 2022 14:53
@philberty philberty self-assigned this Oct 21, 2022
@philberty philberty removed their assignment Oct 31, 2022
@philberty philberty force-pushed the phil/argument-capture branch from 8828d9f to 36a433a Compare November 7, 2022 12:10
@philberty philberty force-pushed the phil/argument-capture branch from 36a433a to 9de17ad Compare December 1, 2022 22:43
This macro is a mess and a helper method is much better for this case.
This allows us to track the type of declaration that is stored within a
Rib.
When we have a closure block referencing variables in a parent function,
we must track what these are. We do this by having a context of closures
so if we have a variable reference and its declared in a rib whose node id
is less than the node id of the closure's node id we know it must be a
captured variable. We also need to iterate all possible closure contexts
as we might be in the case of a nested closure.

Addresses #195
When we have a closure expression that captures a parent function's
variable we must setup the closure data to contain this. Ignoring
moveability and mutability requires for now, this patch creates the closure
structure with fields for each of the captured variables. When it comes to
compilation of the closure expression in order to support nested closures
we must setup a context of implicit mappings so that for all path
resolution we hit this implicit closure mappings lookups code before any
lookup_var_decl as this decl will not exist so the order here is important
during path resolution which is a similar problem to match expression
destructuring.

Fixes #195
@philberty philberty force-pushed the phil/argument-capture branch from 9de17ad to 3053ec3 Compare December 4, 2022 21:00
@philberty
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 4, 2022

Build succeeded:

@bors bors bot merged commit e39fadc into master Dec 4, 2022
@philberty philberty deleted the phil/argument-capture branch December 4, 2022 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Closure types
2 participants