moonscript
moonscript copied to clipboard
new local not hoisted properly
ok, err = pcall ->
ok, err = pcall foo
return err
compiles to:
local ok, err = pcall(function()
ok, err = pcall(foo)
return err
end)
instead of:
local ok, err
ok, err = pcall(function()
ok, err = pcall(foo)
return err
end)
or even:
local ok, err = pcall(function()
local ok, err = pcall(foo)
return err
end)
possible fix: deoptimize the removal of the break between local and assignment if a function is detected on the inside?
side note: i think that only one local assignment should be used because it more realistically matches other code, like:
local ok, err
->
-- if you want new ones in this scope, use `local ok, err`
ok, err = pcall foo
not that it matters in this case too much?
The double local may be the expected behavior if you consider that the following snippet fails:
a, b = 1, a + 1
Also, using local a or even local * are easy workarounds.
I think the expected output would be
local ok, err = pcall(function()
local ok, err = pcall(foo)
return err
end)
Right now lifting local to the line before an assignment only happens when an assignment is creating a plain function. I'm open to making it work for any expressions that contain a function.
The bug is from the scope prematurely storing the creation of the variables during the compilation of the right hand values. The values should be not be made active until after the entire statement is processed.
In any case, I'm slowly working on some new stuff to rebuild the moonscript transformer and compiler, so fixes for stuff like this probably won't come out until then. In the meantime I recommend manually using local