LuaLS / LuaLS/lua-language-server

Issue when using tables/class with generics?

Aperta
#2,176 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement
Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo lo snippet fornito nell'estensione Visual Studio Code, quindi esamina la definizione di pairs e i relativi tipi restituiti generici. La correzione è completa quando k e v vengono inferiti come string per i casi TestClass, tabella non inizializzata e tabella inizializzata, senza errori can not infer type.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.