LuaLS / LuaLS/lua-language-server

Lua Language Server Fails to Infer Type When Passing Multiple Return Values to Generic Function

Open
#3,007 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### How are you using the lua-language-server?

NeoVim

### Which OS are you using?

MacOS

### What is the issue affecting?

Type Checking

### Expected Behaviour

```lua
---@return integer
---@return string
local f = function()
return 1, "a"
end

---@generic T
---@generic U
---@param t T
---@param u U
---@return U
local g = function(t, u)
return u
end

local x = g(f())

```

When the function g is called with the multiple return values of f, the Lua Language Server should infer the type of x based on the second return value of f (string), corresponding to the second generic type parameter (U) in g.

### Actual Behaviour

The Lua Language Server fails to infer the type of x. Instead, the type is left unknown.

### Reproduction steps

1. Write the following Lua code in an editor with the Lua Language Server enabled:
```lua
---@return integer
---@return string
local f = function()
return 1, "a"
end

---@generic T
---@generic U
---@param t T
---@param u U
---@return U
local g = function(t, u)
return u
end

local x = g(f())
```
2. Observe the type inference for x.
3. Note that the type of x is not inferred as string as expected.

### Additional Notes

_No response_

### Log File

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Lua snippet in NeoVim with the Lua Language Server enabled and inspect the generic type inference path for calls receiving multiple return values. Done means the variable x is inferred as string, matching the second return value of f and generic parameter U.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.