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 摘要。