lua-language-server
lua-language-server copied to clipboard
Inherited attributes reassignment not detected properly.
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":

This is a trivial example, but in other contexts it can completely break type checking.
To reproduce
- Create a class
MyClasswith an attributestrof a given type. - Create a subclass
MySubClassinheriting from that class. - Assign the attribute of
MySubClassto 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...