LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.33k stars 313 forks source link

Unknown Vararg indices when itterated #2906

Open PennyJim opened 1 day ago

PennyJim commented 1 day ago

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?

Type Checking

Expected Behaviour

This is similar to #1355

When I try and itterate over a Vararg table, it'll be unable to determine the indexing value.

Actual Behaviour

My enabling of no-unknowns comes and bites me as the key is unknown.

Reproduction steps

---@param ... AnyType
function any(...)
  for key, value in pairs{...} do
    -- key is typed as an unknown instead of integer
    -- value is typed as AnyType
  end
end

Additional Notes

I've been told that there might be edge cases that are why this isn't how it currently works, but I thought I'd ask to at least find them out and learn more.

And if you are questioning why I'm using pairs instead of ipairs, that's because I'm modding in factorio with a deterministic pairs. It is actually faster than ipairs with functionally the same result, but with support for sparse arrays (this makes other Lua environments so sad to work with...).

Finally, I'm writing this at work in the morning before I forget, so I'm not being as thorough as I would prefer. I can absolutely get more information if requested.

Log File

No response

tomlau10 commented 13 hours ago

After some testing and reading the codebase, I think I have found the root cause:

Analysis

The related logic is here: https://github.com/LuaLS/lua-language-server/blob/b815469b3df0655b59a8d2eae2770a3b85c98ad8/script/vm/sign.lua#L114-L117

Solution

I changed line 724 to include varargs type

if field.type == 'tableexp' or field.type == 'varargs' then

=> then it works 🎉

maybe you can try open a PR on this after more testing 😄 @PennyJim