Re: table.unpack does not produce a line number on error

Błażej Roszkowski <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <CAOc0etHGdaf26K-nTP6a09h4sm2eVRS1eRNJm7dVSS5Gu7Wtjg@mail.gmail.com>
Between 5.2 the "Returns the elements from the given table." in
documentation at https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack
changed to "Returns the elements from the given list." at
https://www.lua.org/manual/5.3/manual.html#pdf-table.unpack

The code at https://www.lua.org/source/5.3/ltablib.c.html#unpack indeed
changed (other than other integer types)
from https://www.lua.org/source/5.2/ltablib.c.html#unpack by removing
`luaL_checktype(L, 1, LUA_TTABLE);`

And you're right in 5.1 (plain `unpack` back then) and 5.2
`table.unpack(nil)` produces an error in line number but in 5.3 and up
don't.

Root cause seems to be that in 5.2 it ultimately goes to `luaL_argerror`
which takes debug info, but in
5.3 and up `luaL_len` ends up in `luaV_objlen` which doesn't.

OTOH the 5.3+ seemingly can handle not just tables, but anything with __len
and __index implemented.

To solve this for your application, if you have access to either C or Lua
debug library, you can make own reimplementation
or wrapper and replace `table.unpack` with that to add line number and file
name.


On Mon, 2 Mar 2026 at 18:14, 'Vadim (vadcx)' via lua-l <
[email protected]> wrote:

> Hi all,
>
> When table.unpack encounters an error (such as the above or "attempt to
> get length of a number value"), it doesn't add/push the source and line
> number of the error.
>
> Compare the output of the following two functions producing an error at
> runtime:
>
> > print( pcall(function() table.unpack(nil) end) )
> false   attempt to get length of a nil value
>
> Expected:
> > print( pcall(function() string.gsub(nil) end) )
> false   stdin:1: bad argument #1 to 'gsub' (string expected, got nil)
>
>
> Lua 5.2.4: unpack adds error source
> Lua 5.3.6 and above, including 5.5.0: table.unpack doesn't add error source
>
> A host application (proprietary, with Lua 5.3), that I'm working with,
> struggles to produce an error message at all in case of table.unpack(123)
> (no line number or error message). Yet other errors show up fine in the
> logs.
>
> If I read it correctly, 5.2's unpack has had an explicit
> luaL_checktype(L, 1, LUA_TTABLE);  ?
>
> https://github.com/lua/lua/blob/4324904b60db5243ede68d0922c1bf3c0dd05986/ltablib.c#L140
>
> --
> Cheers,
> Vadim
>
> --
> You received this message because you are subscribed to the Google Groups
> "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/lua-l/7a4bd943-1189-440e-a3a4-684f65c59d0e%40vad.cx
> .
>

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAOc0etHGdaf26K-nTP6a09h4sm2eVRS1eRNJm7dVSS5Gu7Wtjg%40mail.gmail.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.