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