Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid fields injected warning #2341

Closed
sewbacca opened this issue Sep 21, 2023 · 6 comments · Fixed by #2747
Closed

Invalid fields injected warning #2341

sewbacca opened this issue Sep 21, 2023 · 6 comments · Fixed by #2747

Comments

@sewbacca
Copy link
Contributor

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

@carsakiller
Copy link
Collaborator

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

@sewbacca
Copy link
Contributor Author

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?

@carsakiller
Copy link
Collaborator

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.

@sewbacca
Copy link
Contributor Author

sewbacca commented Oct 1, 2023

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
Copy link
Contributor Author

sewbacca commented Oct 1, 2023

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

@CodeGreen0386
Copy link

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants