LuaLS / LuaLS/lua-language-server
False-positive type error: `vim.cmd` callable table flagged as "table cannot match fun(...)"
还没有人认领这个 Issue。
- 主要语言
- Lua
- 星标
- 4.4k
- 派生
- 442
- PR 合并指标
- 30 天内没有已合并 PR
描述
How are you using the lua-language-server?
NeoVim
Which OS are you using?
Linux
What is the issue affecting?
Diagnostics/Syntax Checking
Expected Behaviour
lua-language-server should recognize callable tables (with a __call metamethod) as valid function values. Passing Neovim’s vim.cmd to pcall should not raise a type error.
Actual Behaviour
lua-language-server reports a false-positive type error when vim.cmd (a callable table in Neovim) is passed to pcall:
Cannot assign table to parameter fun(...):...unknown
Type table cannot match fun(...):...unknown
Reproduction steps
-
In Neovim, open a Lua buffer (e.g.,
:new | set ft=lua). -
Insert the following code:
-- Works at runtime, but LuaLS flags the next line: pcall(vim.cmd, "redrawtabline") -- Diagnostic shown by LuaLS: -- Cannot assign `table` to parameter `fun(...):...unknown` -
Observe the diagnostic from lua-language-server.
-
Execute the code in Neovim (e.g.,
:luafile %) to confirm it runs without runtime errors.
Additional Notes
-
vim.cmdis a callable table provided by Neovim (implemented via__callmetamethod). Direct calls likevim.cmd("redrawtabline")work at runtime. The false-positive only appears when passingvim.cmdas a function value (e.g., topcall). -
Workarounds:
-- Wrap in a function pcall(function() vim.cmd("redrawtabline") end) -- or use the typed API pcall(vim.api.nvim_cmd, { cmd = "redrawtabline" }, {}) -
Please treat callable tables as functions for the purpose of type checking / inference.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,在 Neovim Lua 缓冲区中使用 pcall(vim.cmd, "redrawtabline") 重现该诊断,然后跟踪带有 __call 元方法的可调用表的 LuaLS 诊断和类型推断。完成的标准是:在需要函数值的地方接受可调用表,同时不隐藏真正的类型错误,并且给出的重现不再产生诊断。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- lua
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100