Skip to content

Infer pcall's own return when the called function has none - #3440

Open
RomanSpector wants to merge 2 commits into
LuaLS:masterfrom
RomanSpector:fix/pcall-own-return
Open

Infer pcall's own return when the called function has none#3440
RomanSpector wants to merge 2 commits into
LuaLS:masterfrom
RomanSpector:fix/pcall-own-return

Conversation

@RomanSpector

Copy link
Copy Markdown
Contributor

pcall returns false plus the error value when the call fails, and the meta declares that second result as any. For a function declared as fun() it came out as unknown instead:

---@type fun()
local task

local ok, err = pcall(task)   -- err: unknown, expected any

The compiler takes the called function's return under the same index and stops there. getReturn always yields a node — an empty one when there is nothing to take — so the if node then guard passed even when the function returns nothing, and that empty node became the result.

An empty node is no longer accepted as an answer: compilation falls through to pcall's own declaration. The same applies to xpcall, where the message handler's result takes the place of the missing return.

Functions that do return a value are unaffected — the existing tests cover that, and two new ones cover the case above.

`getReturn` always yields a node — an empty one when there is nothing to take —
so `if node then` passed even for a function declared as `fun()`. That empty
node became the result and the type stayed unknown, although `pcall` declares
its second result as `any`: on failure it carries the error value.

An empty node is no longer accepted as an answer; compilation falls through to
`pcall`'s own declaration instead. The same applies to `xpcall`, where the
message handler's result takes the place of the missing return.

Functions that do return a value are unaffected, as the existing tests cover.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant