LuaLS / LuaLS/lua-language-server

`@overload` return union unexpected behaviour

Open
#3,078 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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?

Windows

### What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

### Expected Behaviour

Type of `r1` = `EA|0`
Not warning: probably `nil`

### Actual Behaviour

Type of `r1` = `integer|EA|0`
Warning: probably `nil`

### Reproduction steps

1. Create file `test.lua`
2. Fill
```lua
---@enum EA
local EA = {
a = 1,
b = 2,
}
local ab = 0
---@overload fun(): 0
---@overload fun(): EA, [string]
local function fua()
if ab == 0 then
return 0
else
return EA[ab] or EA.a, { "aa" }
end
end

local r1, r2 = fua()

if r1 == 0 then
print(0)
else
print(r2[1])
end
```
4. See type of `r1`
5. See `Need check nil` at line 22

### Additional Notes

Maybe duplicate of #1456

![Image](https://github.com/user-attachments/assets/476a6f1b-4295-4f1c-9e48-7bee3b25cc92)

### Log File

[Log file](https://github.com/user-attachments/files/18804669/file_F_.23utils_lua_vie.nlisp.log)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the report in test.lua with the supplied @overload example and inspect the inferred types and diagnostic at line 22. Done means r1 is inferred as EA|0 and the nil warning is no longer reported for the shown control flow.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.