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