LuaLS / LuaLS/lua-language-server
Issue when using tables/class with generics?
まだ誰も着手していません。
- 主要言語
- 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?
Type Checking
Expected Behaviour
In the following code:
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
I would hope that the inferred type of k and v would work the same in each instance where k is inferred to be a string and v is inferred to be a string.
Actual Behaviour
In the following code:
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
the k and in line: for k, v in pairs(test) do end and the k in line for k, v in pairs(test_table) do end get the error can not infer type and are typed as unknown.
I assume there is some issue here with generics as the pairs definition uses them heavily
---@generic T: table, K, V
---@param t T
---@return fun(table: table<K, V>, index?: K):K, V
---@return T
but this is only a guess.
Reproduction steps
- Copy this code:
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
- See errors
Additional Notes
No response
Log File
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されたスニペットを Visual Studio Code 拡張機能で再現し、次に pairs の定義とそのジェネリックな戻り値の型を調べます。TestClass、未初期化テーブル、初期化済みテーブルのケースで、can not infer type エラーなしに k と v が string として推論されれば、修正は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- lua
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100