LuaLS / LuaLS/lua-language-server

[Annotations] false positive param-type-mismatch on a class member function

未关闭
#2,787 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Annotations

Expected Behaviour

I believe there should be no param-type-mismatch error or such cases should have better documentation if there's a workaround available.

Actual Behaviour

I have a following class structure:

  1. base class BaseWidget
  2. BaseContainer which inherits from BaseWidget
  3. DialogWindow which inherits from BaseContainer

Creating a member function in DialogWindow and calling it results in an param-type-mismatch error.

Reproduction steps

Issue can be reproduced with the following simplified code snippet:

---@class BaseWidget
---@field _config table
---@field uiObject table
---@field build fun(BaseWidget, boolean)
local BaseWidget = {}
BaseWidget.__index = BaseWidget

---@return BaseWidget
function BaseWidget.new()
    local instance = setmetatable({}, BaseWidget)
    instance._config = {}
    instance.uiObject = nil
    instance:setAnchor(0, 0)
    return instance
end

function BaseWidget:setAnchor(left, top, right, bottom)
    print("Base setAnchor")
    return self
end

---@class BaseContainer: BaseWidget
local BaseContainer = BaseWidget.new()
BaseContainer.__index = BaseContainer

---@return BaseContainer|BaseWidget
function BaseContainer.new()
    ---@type BaseContainer|BaseWidget
    local instance = setmetatable(BaseWidget.new(), BaseContainer)
    instance.uiObject = nil
    return instance
end

function BaseWidget:addRow()
    print("BaseContainer addRow")
    return self
end

---@class Button: BaseWidget
---@field uiObject table
---@field parent table
local Button = BaseWidget.new()
Button.__index = Button

function Button.new(label, icon)
    local button = setmetatable(BaseWidget.new(), Button)
    return button
end

function Button:build(parent)
    print("btn build")
end

---@class DialogWindow: BaseContainer
---@field baseInput table
---@field frame table
local DialogWindow = BaseContainer.new()
DialogWindow.__index = DialogWindow

function DialogWindow.new(dialogWidth)
    local window = setmetatable(BaseContainer.new(), DialogWindow)

    window:append(Button.new("Test00", nil):setAnchor(0, 0)) -- param-type-mismatch error shows up here

    return window
end

---@param uiObj BaseWidget
function DialogWindow:append(uiObj)
    uiObj:build(self.frame)
end

image

Additional Notes

No response

Log File

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从简化的 Lua 复现开始,检查继承的类成员函数是如何处理注释和 param-type-mismatch 诊断的。在 Button.new(...):setAnchor(...) 调用处确认该诊断,然后验证修正后的行为涵盖这种继承模式,且不会引入新的不匹配。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。