LuaLS / LuaLS/lua-language-server

Issue when using tables/class with generics?

Aberta
#2,176 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

enhancement
Linguagem predominante
Lua
Estrelas
4.4k
Forks
442
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece reproduzindo o snippet fornecido na extensão do Visual Studio Code e, em seguida, inspecione a definição de pairs e seus tipos de retorno genéricos. A correção estará concluída quando k e v forem inferidos como string nos casos de TestClass, tabela não inicializada e tabela inicializada, sem erros can not infer type.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
lua
Domínio
devtools
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
42/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.