LuaLS / LuaLS/lua-language-server

string.unpack vararg returns

未关闭
#2,261 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 string.unpack 注解开始,并在 Visual Studio Code 扩展中将鼠标悬停在每个返回的变量上,以重现该问题。跟踪类型检查器如何处理后跟 offset 返回值的 vararg。完成的标准是 unpack 值的类型为 any,最终 offset 的类型为 integer。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。