lua-language-server
lua-language-server copied to clipboard
Invalid fields injected warning
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?
Type Checking, Diagnostics/Syntax Checking
Expected Behaviour
No warning.
Actual Behaviour
Fields cannot be injected into the reference of `AClass` for `a`. To do so, use `---@class` for `o`.Lua Diagnostics.(inject-field)
Reproduction steps
local function new()
---@class AClass
local obj = {
a = 1
}
obj.b = 2
return obj
end
local o = new()
o.a = 2 -- This gives a warning
o.b = 3
Additional Notes
When initializing a class with a table constructor, the used fields are no longer mutable, they seem to be semi existent. Existent enough for autocompletion and usage, but not for modifications.
Log File
No response
I've been away for a while... but I think class references were becoming more strict. You may have to define the field in order for it to be mutable:
---@field a integer
I think class references were becoming more strict.
Is somewhere documented that fields, created via a table constructor are less mutable then fields created via the assignment operator?
There is a very long thread where more strict classes are discussed. I personally got lost in all the suggestions, and I have been gone for a few weeks, so I'm not really sure what was implemented and what wasn't.
Thanks for pointing it out. The injection warning likely is cause by some of the new changes. However I still think this warning is falsly generated: Both fields are defined even for the language server, the one defined in the table is just half existent. It won't allow any modifications, as it thinks the field inside the table doesn't really exist.
There seems to be a few issues with the new warning, now a warning is falsly not generated:
---@class AnotherClass
local cl = {}
---@type AnotherClass
local o = {
x = true -- No Warning
}
o.x = false -- Correctly warned
+1, running into this while modding Factorio, just in a slightly different configuration with @param instead of @type:
---@class Foo
local foo = {
bar = 1
}
---@param param Foo
local function fun(param)
param.bar = 2 -- inject-field warning, expected none
end