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

Invalid fields injected warning

Open sewbacca opened this issue 2 years ago • 6 comments

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)

grafik

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

sewbacca avatar Sep 21 '23 08:09 sewbacca

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

carsakiller avatar Sep 24 '23 00:09 carsakiller

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?

sewbacca avatar Sep 25 '23 06:09 sewbacca

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.

carsakiller avatar Sep 29 '23 04:09 carsakiller

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.

sewbacca avatar Oct 01 '23 08:10 sewbacca

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

sewbacca avatar Oct 01 '23 09:10 sewbacca

+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

CodeGreen0386 avatar Feb 04 '24 05:02 CodeGreen0386