Skip to content

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

@zhu-rengong

Description

@zhu-rengong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions