Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` Infer `pcall`/`xpcall` second result from their own declaration when the called function returns nothing [#3440](https://github.com/LuaLS/lua-language-server/pull/3440)
* `NEW` Support type inference for `@field` and `@type` function declarations in method overrides [#3367](https://github.com/LuaLS/lua-language-server/issues/3367)
* `FIX` Deduplicate documentation bindings for parameters
* `FIX` Correct `math.type` meta return annotation to use `nil` instead of the string literal `'nil'`
Expand Down
16 changes: 10 additions & 6 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2191,12 +2191,14 @@ local compilerSwitch = util.switch()
newArgs[#newArgs+1] = args[i]
end
local node = getReturn(args[1], index - 1, newArgs)
if node then
if node and not node:isEmpty() then
vm.setNode(source, node)
return
end
return
end
if func.special == 'xpcall' and index > 1 then
-- The called function has no such return, but `pcall` still does: on failure
-- the second result is the error value. Fall through to its own declaration,
-- otherwise the result stays unknown.
elseif func.special == 'xpcall' and index > 1 then
if not args then
return
end
Expand All @@ -2205,10 +2207,12 @@ local compilerSwitch = util.switch()
newArgs[#newArgs+1] = args[i]
end
local node = getReturn(args[1], index - 1, newArgs)
if node then
if node and not node:isEmpty() then
vm.setNode(source, node)
return
end
return
-- Same as `pcall`: the message handler's result comes in place of the missing
-- return, and its type is declared on `xpcall` itself.
end
if func.special == 'require' then
if index == 2 then
Expand Down
14 changes: 14 additions & 0 deletions test/type_inference/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ end
_, <?y?> = xpcall(x)
]]

-- A declared function without returns leaves nothing to take the second result from, but
-- `pcall` still has one of its own: the error value.
TEST 'any' [[
---@type fun()
local x
_, <?y?> = pcall(x)
]]

TEST 'any' [[
---@type fun()
local x
_, <?y?> = xpcall(x, debug.traceback)
]]

TEST 'A' [[
---@class A

Expand Down
Loading