LuaLS / LuaLS/lua-language-server

Proposal consider vararg returns as forced optional

Open
#3,036 1 comment 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?

Visual Studio Code Extension (sumneko.lua)

### Which OS are you using?

Windows

### What is the issue affecting?

Diagnostics/Syntax Checking

### Expected Behaviour

I suggest consider **vararg returns** as **optional** (can be `nil`) because no one guarantees that they will be filled in. The same goes for arguments, despite the fact that their type can also be optional or nilable.

If we return at least something, but we don't know the quantity, then the first argument won't be vararg - `any, any ...`.

Thus after that we do not need to additionally specify that the vararg returns is optional.

```lua
---@param a string
function test(a) end

---@param ... string
---@return string ...
function f(...) end
local a, b, c = f() --no warn
local a, b, c = f("1")

test(a) --no warn, expected optinal cannot match

---@param ... string?
---@return string? ...
function f(...) end
local a, b, c = f() --no warn
local a, b, c = f("1")

test(a) --warn optinal cannot match

---@param ... string|nil
---@return string|nil ...
function f(...) end
local a, b, c = f() --no warn
local a, b, c = f("1")

test(a) --warn nil cannot match
```

### Actual Behaviour

Vararg arguments are optional, but vararg returns are not.

![изображение](https://github.com/user-attachments/assets/e910ce6b-c4d3-4b7e-8a44-83e8725c4bb4)

### Reproduction steps

1. Make vararg return of some existing type.
2. It is guaranteed to be infinite - not optional or nil.

### Additional Notes

_No response_

### Log File

_No response_

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 with the Lua language server's diagnostics and type-inference handling for vararg arguments and returns, using the reproduction steps and examples as the behavioral guide. Verify that vararg returns are treated as optional or nilable as appropriate, while the provided calls produce the expected warnings and no warnings.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.