LuaLS / lua-language-server

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

lua-language-server not detecting types when destructuring Tuple types with ~table.unpack~ #2873

Open 4lph4-Ph4un opened 2 days ago

4lph4-Ph4un commented 2 days ago

How are you using the lua-language-server?

Other

Which OS are you using?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

I might be wrong here, but I assume this should work? Language server should detect the types from the defined tuple:

---@alias my_tuple { [1]: integer, [2]: boolean }

---@type my_tuple
local tuple = {1, true}

--- This works!
local a, b = tuple[1], tuple[2]

--- This gets "integer" for "c", but "unknown" for "d:
local c, d = table.unpack(tuple)

Actual Behaviour

---@alias my_tuple { [1]: integer, [2]: boolean }

---@type my_tuple
local tuple = {1, true}

---  "c" should be "integer" and "d" "boolean":
local c, d = table.unpack(tuple)

Reproduction steps

  1. Define tuple like I did
  2. Create such tuple
  3. unpack to two variables

Additional Notes

I'm not sure if this is a feature that is even considered, but since I do use unpack sometimes to destructure tuples into their distinct variables, I feel this would be a good feature to have.

Log File

No response

tomlau10 commented 2 days ago

With your code, I am getting c: integer|boolean and d: unknown

Annotation of table.unpack

I guess this is due to how table.unpack is annotated: https://github.com/LuaLS/lua-language-server/blob/6ba0c9362ebfec8a668d452b0a752216fd52d543/meta/template/table.lua#L58-L66

Hack annotation?

In this PR https://github.com/LuaLS/lua-language-server/pull/2587, @lewis6991 tried with with a "hack" to annotate unpack

---@alias my_tuple { [1]: integer, [2]: boolean }

---@type my_tuple local tuple = {1, true}

local a, b, c = table.unpack(tuple) -- a: boolean|integer, b: boolean|integer, c: integer (extra one??)

local a, b, c = unpack(tuple) -- same result as above


- although it can provide types for the return value, none of them are actually correct 😇
- I am using v3.10.6, maybe this hack doesn't work anymore after some recent change in luals ☹️ 

---

> I'm not sure if this is a feature that is even considered

I don't know if it has been considered, but definitely it is currently not supported.