table.unpack does not produce a line number on error

46 views
Skip to first unread message

Vadim (vadcx)

unread,
Mar 2, 2026, 12:14:02 PM (22 hours ago) Mar 2
to lua-l
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

Błażej Roszkowski

unread,
Mar 2, 2026, 1:18:44 PM (21 hours ago) Mar 2
to lu...@googlegroups.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.


--
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 lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/7a4bd943-1189-440e-a3a4-684f65c59d0e%40vad.cx.
Reply all
Reply to author
Forward
0 new messages