LuaLS / LuaLS/lua-language-server

Problem with methods of generic classes

オープン
#3,339 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。