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

Issue 1341: Elevate statevars to enclosing block when wrapped #1467

Merged
merged 1 commit into from Feb 2, 2018

Conversation

jstuder-gh
Copy link
Contributor

Previously in some blocks following the "do" statement prefix, statevars
would be seemingly reset after each iteration of the loop.

Given the statement:

say do loop { state $x = 0; last if ++$x > 2; $x }

This statement would result in multiple thunk refs being produced, and
ultimately, would be wrapped in a block that is used as a cloning target
in looping and a new container would be declared and initialized for
each iteration.

The QAST would look like this (trimmed to relevant parts):

- QAST::Block(:cuid(3) :blocktype(declaration_static))
  - QAST::Stmts  { state $x = 0; last if ++$x > 2; $x }
  - QAST::Stmts
    - QAST::Block(:cuid(1) :blocktype(immediate)) <wanted>
      - QAST::Stmts
        - QAST::Var(lexical $x :decl(statevar))
        - QAST::Var(lexical $_ :decl(var))

After this commit the QAST looks like this:

- QAST::Block(:cuid(3) :blocktype(declaration_static))
  - QAST::Stmts  { state $x = 0; last if ++$x > 2; $x }
    - QAST::Var(lexical $x :decl(statevar))
  - QAST::Stmts
    - QAST::Block(:cuid(1) :blocktype(immediate)) <wanted>
      - QAST::Stmts
        - QAST::Op(null)
        - QAST::Var(lexical $_ :decl(var))

This way the statevar is declared in the block being cloned and thus the
same container is used in each iteration.

Note the enclosing block is a block newly created via the make_thunk_ref sub,
and contains no child nodes other than the first Stmts (that has no children prior
to this commit) and the second Stmts containing the cuid(1) subtree.

See Github Issue 1341

Previously in blocks following the "do" statement prefix, statevars
would be seemingly reset after each iteration of the loop.

Given the statement:

    say do loop { state $x = 0; last if ++$x > 2; $x }

This statement would result in multiple thunk refs being produced, and
ultimately, would be wrapped in a block that is used as a cloning target
in looping and a new container would be declared and initialized for
each iteration.

The QAST would look like this (trimmed to relevant parts):

    - QAST::Block(:cuid(3) :blocktype(declaration_static))
      - QAST::Stmts  { state $x = 0; last if ++$x > 2; $x }
      - QAST::Stmts
        - QAST::Block(:cuid(1) :blocktype(immediate)) <wanted>
          - QAST::Stmts
            - QAST::Var(lexical $x :decl(statevar))
            - QAST::Var(lexical $_ :decl(var))

After this commit the QAST looks like this:

    - QAST::Block(:cuid(3) :blocktype(declaration_static))
      - QAST::Stmts  { state $x = 0; last if ++$x > 2; $x }
        - QAST::Var(lexical $x :decl(statevar))
      - QAST::Stmts
        - QAST::Block(:cuid(1) :blocktype(immediate)) <wanted>
          - QAST::Stmts
            - QAST::Op(null)
            - QAST::Var(lexical $_ :decl(var))

This way the statevar is declared in the block being cloned and thus the
same container is used in each iteration.

See [Github Issue 1341](rakudo#1341)
@zoffixznet
Copy link
Contributor

👍 Thanks!

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.

None yet

2 participants