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)

Aperta
#2,706 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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?

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

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 nell’editor l’esempio fornito di Vehicle e Plane e segui l’inferenza dei generics del costruttore e del tipo self per i metodi ereditati. Verifica che il prompt del codice mostri function Plane.new(self: Plane, o?: Plane) e che il tipo inferito di obj sia Plane invece di unknown.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.