LuaLS / LuaLS/lua-language-server

Generics don't expand alias types/don't work with other parameters

Abierto
#2,355 3 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Lua
Estrellas
4.4k
Forks
442
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

With a code sample like this:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func T
local function testSecond(name, func) end

testSecond("TestFunction", function(abc) end)

I would expect abc to have a type of string, and the function to be type checked and require a return.

Actual Behaviour

Instead, abc has the any type and the function requires no return.
I further tested with a different code sample:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@return T
local function testSecond(name, func) return function(abc) end end

local cool = testSecond("TestFunction", function(abc) end)
cool()

This shows no warnings and does not require me to pass any arguments to cool(), which is incorrect. Hovering over cool provides a type of 'TestFunction' and does not show the full expanded type of the alias, unlike other variables with a type of an alias. That rings the same with this code sample I also tested:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
local function testSecond(name, func) end

testSecond("TestFunction", function(abc) end)

where hovering over abc provides me with a 'TestFunction' unexpanded type.
However, correct behavior is seen with the following code sample using the base string type;

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
---@return T
local function testSecond(name, func) end

local cool = testSecond("string", function(abc) abc.cool = "hi" end)
local testfind = cool:find("test")

Autocomplete is provided for :find() and abc.cool = "hi" throws the correct warning for field injection.
And most confusing of all, this code sample behaves in line with expected behavior, and the alias is fully expanded.

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
---@return T
local function testSecond(name, func) end

local cool = testSecond("TestFunction",
    ---@param abc TestFunction
    function(abc)
        abc()
    end
)

This also doesn't work, which I kind of expected. (no warnings)
image

Reproduction steps
  1. Paste code into project with Lua LSP on, see issue
Additional Notes

No response

Log File

No response

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza reproduciendo los ejemplos de la Visual Studio Code Extension con LuaLS, centrándote en la comprobación de tipos para parámetros genéricos y aliased function types. Compara los casos que usan TestFunction con los que usan el tipo base string. Se considera completado cuando los alias se expanden de forma coherente, los parámetros de los callbacks reciben los tipos esperados y las llamadas no válidas o los retornos ausentes producen advertencias.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
lua
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.