Skip to content

string.gmatch and string.match have invalid return types #3357

@sewbacca

Description

@sewbacca

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Windows

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

The return type of string.match should be string|integer? and the return type of string.gmatch, should be a function which returns multiple string|integer ...

Actual Behaviour

The return type of string.match is any and the return type of string.gmatch is (fun(): string, ...), which causes #2487

See

---#DES 'string.match'
---@param s string|number
---@param pattern string|number
---@param init? integer
---@return any ...
---@nodiscard
function string.match(s, pattern, init) end

and
---#DES 'string.gmatch'
---#if VERSION <= 5.3 then
---@param s string|number
---@param pattern string|number
---@return fun():string, ...
---@nodiscard
function string.gmatch(s, pattern) end
---#else
---@param s string|number
---@param pattern string|number
---@param init? integer
---@return fun():string, ...
function string.gmatch(s, pattern, init) end
---#end

Reproduction steps

Try casting the first for loop variable from a :gmatch() into an integer (---@cast x integer), it throws an invalid warning; see no completion of return values from a :match().

Additional Notes

It seems to be currently not possible to specify types for ... return types using the fun() syntax. As a workaround at least define the first return value as string|integer instead of string.

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions