LuaLS / LuaLS/lua-language-server

When the subclass inherits the constructor of the parent class, generics do not work (the type of `self` cannot be used for annotation)

Abierto
#2,706 1 comentario 0 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?

Other

Which OS are you using?

Windows

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

---@class Vehicle
Vehicle = {}

---comment
---@generic T
---@param self `T`
---@param o? T
---@return T
function Vehicle:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

---@class Plane: Vehicle
Plane = {}

setmetatable(Plane, { __index__ = Vehicle })

local obj = Plane.new()

图片

图片

The code prompt given when calling a constructor annotated with generic parameters that is inherited by a subclass is incorrect.

The prompt message should be

 function Plane.new(self: Plane, o?: Plane)

And the type of the obj variable in the code hint should be Plane instead of unknown

Actual Behaviour
---@param o? self
---@return self
function Vehicle:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

self should be used to refer to the type of the method caller. And the self method should replace the generic method to annotate the type in this use case

Such type annotations should work, and code completion should show self as the caller's type

Reproduction steps
  1. Copy example to editor
  2. Call code prompt
  3. The prompt result is not as expected
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 el ejemplo proporcionado de Vehicle y Plane en el editor y sigue la inferencia de genéricos del constructor y del tipo self para los métodos heredados. Verifica que el prompt de código muestre function Plane.new(self: Plane, o?: Plane) y que el tipo inferido de obj sea Plane en lugar de unknown.

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
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.