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)
還沒有人認領這個 Issue。
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 442
- PR 合併指標
- 30 天內沒有已合併 PR
描述
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
- Copy example to editor
- Call code prompt
- The prompt result is not as expected
Additional Notes
No response
Log File
No response
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先在編輯器中重現提供的 Vehicle 和 Plane 範例,並追蹤繼承方法的建構函式泛型和 self 型別推斷。確認程式碼提示顯示 function Plane.new(self: Plane, o?: Plane),而且 obj 的推斷型別是 Plane 而不是 unknown。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- devtools
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100