lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Aug 26, 2013 at 7:03 PM, Coda Highland <chighland@gmail.com> wrote:
> I think it makes perfect sense to handle such a check at compile-time,
> since the compiler can easily identify when the same name is used a
> second time in the same scope -- it has to map names to stack slots,
> after all.

Indeed! consider shadow.lua

local a = 1
do
    local a = 2
end
print(a)

$ luac -l -l shadow.lua
main <shadow.lua:0,0> (6 instructions, 24 bytes at 0000000000496FD0)
0+ params, 3 slots, 0 upvalues, 2 locals, 3 constants, 0 functions
        1       [1]     LOADK           0 -1    ; 1
        2       [3]     LOADK           1 -2    ; 2
        3       [5]     GETGLOBAL       1 -3    ; print
        4       [5]     MOVE            2 0
        5       [5]     CALL            1 2 1
        6       [5]     RETURN          0 1
constants (3) for 0000000000496FD0:
,,,
locals (2) for 0000000000496FD0:
        0       a       2       6
        1       a       3       3
upvalues (0) for 0000000000496FD0:

When you get both locals called 'a'  with the same end instruction,
then you have the 'shadow in same scope' issue.

It should not be too difficult to teach something like lglob to do
this, since it's already parsing the local information for each
function.