LuaLS / LuaLS/lua-language-server

Problem with methods of generic classes

Open
#3,339 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the generic Foo inheritance example from the issue in the Visual Studio Code extension and inspect service.log for the type-checking diagnostics. Trace how inherited methods substitute generic return types, then verify that get() resolves to string and boolean for Example2 and Example3 without introducing incorrect warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
developer-experience, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.