luajit-lang-toolkit
luajit-lang-toolkit copied to clipboard
goto jumping into the scope of an unused local
Code like this
goto skip
local x = 1
::skip::
print(x)
causes both LuaJIT and luajit-lang-toolkit to throw an error: <goto skip> jumps into the scope of local 'x'
. However, LuaJIT stops complaining if we remove the use of x
like so:
goto skip
local x = 1
::skip::
However, luajit-lang-toolkit is not so lenient and still throws the same error.
I've seen people implement continue
by placing a label at the very end of the loop, so maybe it's worthwhile to imitate this quirk of LuaJIT's?
Thank you for reporting that. I think it is definitely worth implementing the same behavior of LuaJIT for the reason you mentioned.
I was just wondering what the exact rule is. I guess that it is ok to jump if the label is just before the end of the block. That would means that, in your example, if you write print(1)
after the label luajit will still complain even if you don't refer to x
.
It seems you are right! However, I found a quirk. This code works in LuaJIT:
local i = 1
while i < 10 do
if i == 5 then
i = i + 2
goto continue
end
local j = i
i = i + 1
::continue::
end
But this doesn't:
local i = 1
repeat
if i == 5 then
i = i + 2
goto continue
end
local j = i
i = i + 1
::continue::
until i >= 10
(For the reference, Lua 5.2 behaves the same way regarding goto
, so maybe there's something in their docs regarding all this?)
Ok, now I remember now. This is how it is supposed to work, I remember the discussions from the Lua's mailing list. The logic is that in the repeat-until the local variables remains visible in the until clause so the goto is forbidden.
Now I need to found a reasonable way to implement this goto's rule. It does not fit naturally into the logic I have used for the implementation.