lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

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

Open Bart97 opened this issue 1 year ago • 5 comments

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

Bart97 avatar Aug 07 '24 21:08 Bart97