Open Bilal2453 opened 1 year ago
Just come across this issue in one of the older projects in my team, which heavily uses string.unpack
when decoding client data.
I guess there is no possible or logical way to specify this ---@return integer offset
, given that the position of this offset
depends on the actual unpack pattern. Also not all the time we will extract this offset
when doing string.unpack
, so even if assigning the last variable in the assignment statement to be an integer, it will not work either. 😕
I suggest removing the ---@return integer offset
all together, just let all return values be any
. And users explicitly cast the offset
variable to integer
if they want.
---@param fmt string
---@param s string
---@param pos? integer
---@return any ...
---@nodiscard
function string.unpack(fmt, s, pos) end
Making it any
is a good first step. It is actually possible to extract the types from the pattern, similar to how we extract the types from FFI definitions, etc.
It can also be kept as it is, and the user casts it later, it should still be possible to override this with a cast.
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Linux
What is the issue affecting?
Annotations, Type Checking, Completion
Expected Behaviour
The current
string.unpack
annotations (for the returns) are as following:The function will indeed return any number of
any
returns followed by a single integer return for the new offset. As such you would expect that in the following example code, all of the returns areany
except the last return beinteger
:Actual Behaviour
Instead of the
a, b, c, d
being typed asany
andoffset
being typed asinteger
, we instead see thata
is typed asany
,b
is typed asinteger
, andc, d, offset
are typed asnil
.This happen because it is not supported for a function to return a vararg followed by any return; it is always that vararg returns are the last return. Because of that, only the first return is typed as
any
and from there the second@return
takes.Reproduction steps
Additional Notes
No response
Log File
No response