LuaLS / LuaLS/lua-language-server

泛型类的成员函数通过@param注解的类泛型参数无法与调用时传入的实参正确匹配

Open
#3,398 1 comment 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?

Diagnostics/Syntax Checking

Expected Behaviour

如题,下面的 stringBox.combine(text, "true") 应该不会显示任何诊断问题:

---@generic T
---@class Box<T>
---@field storedValue T
---@field combine2 fun(first: T, second: T): T
local Box = {}

---@param first T
---@param second T
---@return T
function Box.combine(first, second) end

---@type Box<string>
local stringBox

local text = "123"
local r1 = stringBox.combine(text, "true")
local r2 = stringBox.combine2(text, "true")
Actual Behaviour

stringBox.combine(text, "true") 的两个参数均被诊断为:

Cannot assign `string` to parameter `T`.
- `string` cannot match `T`
- Type `string` cannot match `T`

而用@field声明的同类型函数combine2却能正常识别实参

Image
Reproduction steps
  1. 复制我提供的复现代码,检查诊断结果
Additional Notes

No response

Log File

No response

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

Start by reproducing the diagnostics in the provided Lua example through the Visual Studio Code extension, comparing the method declared with @param annotations against the equivalent @field function. Trace the generic type matching used for stringBox.combine; done means both arguments no longer produce diagnostics while combine2 remains correctly handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.