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

Do not shadow/re-inject fields in child-classes

Open Luke100000 opened this issue 11 months ago • 2 comments

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Linux

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

When assigning to a field which is already defined in one of it's supers, no injection should be used.

Actual Behaviour

Assigning to existing field is not possible as I effectively end up with two fields A.test and B.test.

The only valid workaround I know would be using setters in A.

Just disabling (exact) does not solve the issue, just disable the warning.

Reproduction steps

---@class (exact) A
---@field test number
local a = {}

function a:init()
    --This is fine
    self.test = 0
end

---@class (exact) B : A
local b = {}

function b:init()
    --This is an injection and thus invalid
    self.test = 0
end

Additional Notes

If shadowing a field is explicitly wanted, one can still define/type explicitly. But I think by default, no additional field should get injected.

Issue is a bit related to @override (#2569).

Log File

No response

Luke100000 avatar Mar 17 '24 15:03 Luke100000

I added a fix for this, but because I have honestly no idea what I'm doing its probably not a proper solution. It works for me tho: https://github.com/Luke100000/lua-language-server/commit/ad80ae9041118d7ab8eae59e7ae81f8d33c0214a

It effectively disables looking for local field definitions for exact classes. They are invalid anyways afaik.

Luke100000 avatar Mar 19 '24 08:03 Luke100000

Confirmed fix on 3.10.5.

Inherited fields and types are correctly identified.

Appreciate your work @Luke100000 - I'd me most grateful if you raised a pull request for the fix.

20240908_141729

alex-courtis avatar Sep 08 '24 04:09 alex-courtis