lua-users home
lua-l archive

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




On Wed, Mar 31, 2021 at 10:45 AM Xmilia Hermit <xmilia.hermit@gmail.com> wrote:
Hi,

I think I just found a bug regarding to-be-closed variables. For some
reason the forth to-be-closed variable of a for-in loop has problems to
close with a tailcall as can be seen in the following example.

 
local function test()
     for l in pairs_with_close() --[[io.lines("test.lua")]] do
         return tail()

The problem is that 'tail' is no longer a tail call. In the general case the file can only be closed after 'tail' returns, like in this convoluted example:

local f = assert(io.open("/usr/share/dict/words"))

local function read_next()
    return f:read("l")
end

for w in read_next, nil, nil, f do
    print(w)
    if (w == "cheese") then
        return tail(f)
    end
end

The program reads a dictionary until it finds the word "cheese", then calls a function 'tail' with the file.

(Would I code it like that? No, but it's legal.)

The simpler case:

local function test()
    local x <close> = some_object_with_a_close_metamethod()
    return tail()
end

Here 'tail' is not a tail call either, because the __close metamethod of 'x' has to be called after 'tail' returns.