LuaLS / LuaLS/lua-language-server

Problem with methods of generic classes

未关闭
#3,339 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

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, Annotations

Expected Behaviour
  1. Define a generic base class Foo with a method function which returns the generic type
  2. Inherit from the base class and specify the generic type
  3. Use the method and expect the specified generic type on the output
---@class Foo<T>
---@field myValue T
local base = {}

---@return T
function base:get()
	return self.myValue
end

---@type Foo<number>
local ex1 = base --Use class directly
---@class Example2 : Foo<string>
local ex2 = ex1 --Start with a version with generic type specified
---@class Example3 : Foo<boolean>
local ex3 = base --Start with a version without the generic type specified

local I 	= ex1.myValue 	--Correct: number
local a 	= ex1:get() 	--Correct: number
local II 	= ex2.myValue 	--Correct: string
local b 	= ex2:get() 	--Correct: string
local III 	= ex3.myValue 	--Correct: boolean
local c 	= ex3:get() 	--Correct: boolean
Actual Behaviour

Attempting to expand the parent class directly displays a warning, (Example 3) but using a variable with specified type does not (Example 2).
The method defined in the parent class fails to infer the generic type from the child classes

---@class Foo<T>
---@field myValue T
local base = {}

---@return T
function base:get()
	return self.myValue
end

---@type Foo<number>
local ex1 = base
---@class Example2 : Foo<string>
local ex2 = ex1
---@class Example3 : Foo<boolean>
local ex3 = base --Cannot assign 'Foo' to 'Example3'

local I 	= ex1.myValue 	--Correct: number
local a 	= ex1:get() 	--Correct: number
local II 	= ex2.myValue 	--Correct: string
local b 	= ex2:get() 	--Incorrect: T
local III 	= ex3.myValue 	--Correct: boolean
local c 	= ex3:get() 	--Incorrect: T
Reproduction steps
  1. Define a generic base class with a method function which returns the generic type
  2. Inherit from the base class and specify the generic type
  3. Use the method and expect the specified generic type on the output
Additional Notes

No response

Log File

service.log

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在 Visual Studio Code 扩展中重现 issue 中的通用 Foo 继承示例,并检查 service.log 以查看类型检查诊断信息。跟踪继承的方法如何替换通用返回类型,然后验证对于 Example2 和 Example3,get() 分别解析为 string 和 boolean,且不会引入错误警告。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
developer-experience, devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。