Open aviatesk opened 2 weeks ago
/cc @JeffBezanson Could I hear your thought on this? I'm sorry if this is a known issue.
I think the challenge is that lowering needs to prove that no re-assignments occur after closure creation, because if any do, then the closure's captured variable needs to be able to be changed. Ref https://github.com/JuliaLang/julia/issues/15276#issuecomment-248068935
Functions called with the do
syntax typically immediately evaluate and discard the anonymous function, but it might escape — possibly even to a global. There's no way lowering could know this.
julia> function demo_box(a)
x = sin(a)
f = identity() do a
a + x
end
@show f(1)
x = 123123
@show f(1)
end
demo_box (generic function with 1 method)
julia> demo_box(1.23)
f(1) = 1.9424888019316975
f(1) = 123124
In cases like the following code, where a variable defined in the parent scope of a closure is used inside the closure and could potentially be reassigned in the parent scope, it seems to get boxed even if it is never reassigned within the closure itself.
This boxing appears completely unnecessary to me. Would it be possible to make a simple change to lowering to fix this issue?