LuaLS / LuaLS/lua-language-server

Over-zealous 'Field `_field` is protected' warning in class-created closure functions

Open
#2,931 0 comments 3 reactions 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?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

Functions created from within class methods should be allowed to access protected (and private) members of that class.

Essentially, any functions inside a class method should inheret that method's access- this is a simple pattern that maintains encapsulation of exactly those private/protected fields, as the code is still clearly "owned" by the encapsulating class.

Actual Behaviour

In some scenarios, functions within class methods that access protected members of that class are being marked with the 'Field _field is protected' warning. See Repro Steps for code sample.

Reproduction steps
---@class DynamicDude
---@field _field string?
local DynamicDude = {}

function DynamicDude:demo()
    self:setup(function(self)
        -- This disable shouldn't be necessary
        ---@diagnostic disable-next-line: invisible
        self._field = 'test'
    end)
    ---@param self DynamicDude
    local function closure(self)
        -- This disable shouldn't be necessary
        ---@diagnostic disable-next-line: invisible
        self._field = 'test'
    end
    self.dynamic_fn = function(self)
        self._field = 'test'
    end
end

---@param fn fun(self:DynamicDude)
function DynamicDude:setup(fn)
    self.dynamic_fn = fn
end
Additional Notes

FEATURE REQUEST: I'd actually love to have a setting that just said "all variables named self are assumed to indicate valid access to private/protected members", to cover all my bases. Happy to dig up some examples / explain myself more if that's useful.

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 by reproducing the supplied DynamicDude example in the Visual Studio Code extension and inspect how diagnostics classify closures created inside class methods. Done means those closures can access the class's protected or private members without the invisible warning, while unrelated accesses remain diagnosed.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.