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

bug in closure capture boxing logic #48889

Closed
uniment opened this issue Mar 4, 2023 · 2 comments · Fixed by #50742
Closed

bug in closure capture boxing logic #48889

uniment opened this issue Mar 4, 2023 · 2 comments · Fixed by #50742
Assignees
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) kind:bug Indicates an unexpected problem or unintended behavior
Milestone

Comments

@uniment
Copy link

uniment commented Mar 4, 2023

Notice that foo() returns 3.

julia> VERSION
v"1.9.0-beta4"

julia> foo=let j=0, f, i
           while j < 3
               i = j + 1
               println("i == ", i)
               if j==0;  f = ()->i  end
               j += 1
           end
           i
           f
       end;
i == 1
i == 2
i == 3

julia> foo()
3

Comment out the line that says i, and foo() instead returns 1 (and its capture is not in a Core.Box anymore).

(ref: Discourse thread)

@uniment
Copy link
Author

uniment commented Mar 6, 2023

Worth noting that this was first observed using @goto:

julia> baz = let j=0
           @label upper
           i = j + 1
           println("i == ", i)
           if j != 0; @goto lower end
           f = ()->i
           @label lower
           j += 1
           if j < 3; @goto upper end
           #i
           f
       end;
i == 1
i == 2
i == 3

julia> baz()
1

@vchuravy vchuravy added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Mar 6, 2023
@simeonschaub
Copy link
Member

3 is the correct answer here, since the binding i exists outside the scope of the while loop as well. What I'm slightly surprised by is that let i; ... end is not enough to make i an outer binding if you comment out the i line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants