LuaLS / LuaLS/lua-language-server

Issue when using tables/class with generics?

オープン
#2,176 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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?

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.

Screenshot_1

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
  1. 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
  1. See errors
Additional Notes

No response

Log File

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供されたスニペットを Visual Studio Code 拡張機能で再現し、次に pairs の定義とそのジェネリックな戻り値の型を調べます。TestClass、未初期化テーブル、初期化済みテーブルのケースで、can not infer type エラーなしに kv が string として推論されれば、修正は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。