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

Inherited attributes reassignment not detected properly.

Open kenzocarneiro opened this issue 3 years ago • 0 comments

Describe the bug I have a class with a property named str. I also have a subclass which inherits from that class. When reassigning the property of the subclass to its own value, it breaks the type detection. This bug only happens on inherited properties.

Here is an example with code:

--- MyClass
--- @class MyClass
--- @field str string
MyClass = {}
MyClass.str = "My str"

--- Constructor of MyClass.
--- @return MyClass
function MyClass:new()
    local e = {}
    setmetatable(e, self)
    self.__index = self
    return e
end

--- MySubClass
--- @class MySubClass : MyClass
MySubClass = MyClass:new()
MySubClass.str2 = "My str2"

-- This works:
MyClass.str = MyClass.str
MySubClass.str2 = MySubClass.str2
-- This doesn't work:
MySubClass.str = MySubClass.str

print(MySubClass.str) -- Prints "My str"

The value of MySubClass.str is shown as "unknown": image

This is a trivial example, but in other contexts it can completely break type checking.

To reproduce

  • Create a class MyClass with an attribute str of a given type.
  • Create a subclass MySubClass inheriting from that class.
  • Assign the attribute of MySubClass to itself: MySubClass.str = MySubClass.str

Expected behavior MySubClass.str should still be recognized as a string after a reassignment to itself.

Environment:

  • OS: Windows 10 x64
  • Is WSL remote? No
  • Client: VSCode

Temporary Fix I also found a temporary fix for this issue: repeating @field str string in every subclass:

--- MySubClass
--- @class MySubClass : MyClass
--- @field str string
MySubClass = MyClass:new()

But this isn't ideal...

kenzocarneiro avatar Aug 02 '22 19:08 kenzocarneiro