LuaLS / LuaLS/lua-language-server
overload type narrowing for returns
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?
Linux
What is the issue affecting?
Type Checking, Completion
Expected Behaviour
I am not sure this is a duplicate issue considering how common it probably is, but just to make sure.
Possibly a duplicate of https://github.com/LuaLS/lua-language-server/issues/1583.
This is similar to https://github.com/LuaLS/lua-language-server/issues/1146, but with returns instead of params.
It is a common pattern in Lua that on errors you might instead return a nil value to imply failure, along side an error message.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3)) -- fail type here is expected to be "string"
end
Actual Behaviour
You get a warning on fail that it might possibly be nil. Because its type is string|nil.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3) )-- fail type here is actually "string|nil"
end
Reproduction steps
- Copy the setup code
- follow along with the comments
Additional Notes
I believe narrowing down the types in this case is still possible? It seems to me like https://github.com/LuaLS/lua-language-server/issues/1583 was discussing the possibility of adding tuples, but shouldn't this be doable even without tuples support?
Log File
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the overload-return example in the issue and inspect the language server's type-checking and completion behavior for conditional narrowing. The payload names no repository files or tests, so locate the relevant overload and return-type analysis entry points before making changes. Done means fail narrows to string after checking that resource is nil, without the existing warning.
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