LuaLS / LuaLS/lua-language-server

Ipairs with generic

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

还没有人认领这个 Issue。

主要语言
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?

Windows

What is the issue affecting?

Annotations

Expected Behaviour

Should have got a correct type inference in a generic function.

Actual Behaviour
  1. could not specify a V-related type for result in the generic function
  2. ipairs will be inferred as unknown
  3. returns of the generic function will be inferred as a strange type
Reproduction steps
---@class Test
---@field name string

---@type Test[]
local tests = {
    { name = "test1" },
    { name = "test2" },
    { name = "test3" },
}

--[[
CORRECT:
    function group_by(rows: <V>[], key: string)
    -> table<string, <V>[]>
]]
---@generic V
---@param rows V[]
---@param key string
---@return table<string, V[]>
local function group_by(rows, key)
    --[[
        ---@type table<string, V[]>
        GOT: Undefined type or alias `V`.
    ]]
    local result = {}
    -- WRONG: local v: unknown
    for i, v in ipairs(rows) do
        local k = v[key]
        if not result[k] then
            result[k] = {}
        end
        table.insert(result[k], v)
    end
    return result
end

--[[
WRONG:
    local groups: table<string, <V>[]> | { [<K>]: Test }
EXPECTED:
    local groups: table<string, Test[]>
]]
local groups = group_by(tests, "name")

Additional Notes

No response

Log File

No response

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先复现 issue 中的 Lua 代码片段,并检查 language server 的泛型注解、索引访问和 ipairs 类型推断路径。完成标准是:V 在本地结果注解中被接受,ipairs 产生泛型元素类型,并且 group_by(tests, "name") 被推断为 table<string, Test[]>。

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

评估

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

把新 issue 发到你的邮箱

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