lua-users home
lua-l archive

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


> I think there is either an error in the manual or a bug in the
> implementation regarding the next function.
> The manual state for the lua function next:
> "The behavior of next is undefined if, during the traversal, you assign any
> value to a non-existent field in the table. You may however modify existing
> fields. In particular, you may set existing fields to nil."
> https://www.lua.org/manual/5.4/manual.html#pdf-next
> 
> However, in the following example during the traversal all elements are set
> to nil. This is allowed by the manual but it throws an error if the
> collectgarbage call isn't commented out.
> 
> function LoopAndEmpty(t)
>     for k, v in pairs(t) do
>         t[k] = nil
>         coroutine.yield(k, v)
>     end
> end
> 
> local c = coroutine.create(LoopAndEmpty)
> local t = {}
> t["no" .. "ref1"] = 1
> t["no" .. "ref2"] = 2
> print(coroutine.resume(c, t))
> collectgarbage("collect")
> print(coroutine.resume(c))
> 
> The problem seems to be that the key "noref1" or "noref2" is white when the
> table t is travered and then in traversestrongtable in
> https://github.com/lua/lua/blob/master/lgc.c#L526 the key is cleared. This
> should not be the case since it could be marked later.

I guess you are right.

A curiosity: How did you realise that you had to use a coroutine in your
example? (I am still trying to produce the bug without coroutines, but
the key is always traversed before the table.)

-- Roberto