LuaLS / LuaLS/lua-language-server

string.unpack vararg returns

Open
#2,261 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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:

---@param fmt  string
---@param s    string
---@param pos? integer
---@return any ...
---@return integer offset
---@nodiscard
function string.unpack(fmt, s, pos) end

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 are any except the last return be integer:

local a, b, c, d, offset = string.unpack('I2I2I2I2', "some binary data")
Actual Behaviour

Instead of the a, b, c, d being typed as any and offset being typed as integer, we instead see that a is typed as any, b is typed as integer, and c, d, offset are typed as nil.

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
  1. Copy the example code.
  2. Hover over each return variable to notice the type.
Additional Notes

No response

Log File

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the string.unpack annotation and reproduce the issue in the Visual Studio Code extension by hovering over each returned variable. Trace how the type checker handles a vararg followed by the offset return. Done means the unpack values are typed as any and the final offset is typed as integer.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.