LuaLS / LuaLS/lua-language-server

Class inheriting a generic class with specified type not recognized as inherited class (assign-type-mismatch)

未关闭
#3,345 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

Expected Behaviour

The type of a class inheriting a generic class (when specifying the type) should be recognized as the same type as the class it inherits.
If that generic class inherits another class, that should also be recognized.
As such, the following code should have no problems.

--- @class Test_Base
--- @field Str_A string
local Test_Base = {}

--- @generic T
--- @class Test_Generic<T> : Test_Base
--- @field Gen_T T
local Test_Generic = {}

--- @class Test_Class : Test_Generic<string>
local Test_Class = {}

--- @type Test_Base[]
local testArray = {
    Test_Base,
    Test_Generic,
    Test_Class,
}
Actual Behaviour

"Test_Class" isn't recognized as either "Test_Generic" or "Test_Base", therefore showing as an "assign-type-mismatch" error.

Image

However, inheriting the generic class without specifying the type is completely fine.

--- @class Test_Base
--- @field Str_A string
local Test_Base = {}

--- @generic T
--- @class Test_Generic<T> : Test_Base
--- @field Gen_T T
local Test_Generic = {}

--- @class Test_Class : Test_Generic
local Test_Class = {}

--- @type Test_Base[]
local testArray = {
    Test_Base,
    Test_Generic,
    Test_Class,
}
Reproduction steps
--- @class Test_Base
--- @field Str_A string
local Test_Base = {}

--- @generic T
--- @class Test_Generic<T> : Test_Base
--- @field Gen_T T
local Test_Generic = {}

--- @class Test_Class1 : Test_Generic<string>
local Test_Class1 = {}

--- @class Test_Class2 : Test_Generic
local Test_Class2 = {}

--- @type Test_Base[]
local testArray = {
    Test_Base,
    Test_Generic,
    Test_Class1,
    Test_Class2,
}
Additional Notes

This problem showed up after the release of version 3.17.0

I know that the implementation of generics is complicated, and I've already found a workaround for my use-case by casting the inheriting classes to the type of the underlying base class. But that may not work for others.

Log File

No response

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 issue 中的 Lua 复现开始,将其通过 Visual Studio Code 扩展的类型检查,以观察 assign-type-mismatch 诊断。跟踪泛型类继承的处理,然后验证 Test_Class1 同时作为 Test_Generic 和 Test_Base 都能被接受,包括传递继承的情况,同时现有的 Test_Class2 行为仍保持有效。

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

评估

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

把新 issue 发到你的邮箱

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