LuaLS / LuaLS/lua-language-server

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

Open
#2,573 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Lua reproduction in the issue and trace the diagnostics/type-checking path for assignments to fields inherited through B : A. The fix is done when self.test = 0 in B:init() does not inject a second field for the inherited A.test, while explicitly requested shadowing remains possible.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.