Skip to content

Return value of overloaded function is not detected correctly #2050

@lukasbestle

Description

@lukasbestle

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

MacOS

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

I expect the following code to be accepted (as it was in the past):

---@param str string
---@param multiple? boolean If `true`, all matches are returned as a list
---@return string[] results Single or multiple matches
---@overload fun(str: string): string
local function someFunction(str, multiple)
    if multiple == true then
        return { "a match" }
    end

    return "a match"
end

local result = someFunction("some string")

print(result:match("some regex"))

There should be no warning from the call to result:match().

Actual Behaviour

LLS isn't able to determine that the overloaded function definition needs to be used for this call. So it sees both possible return values (string[] and string). Of course string[] doesn't have a match method, so the following warning is emitted:

Cannot assign `string|string[]` to parameter `string|number`.
- `string[]` cannot match `string|number`
- `string[]` cannot match any subtypes in `string|number`
- Type `string[]` cannot match `number`
- Type `string[]` cannot match `string`

Bildschirmfoto 2023-04-02 um 13 23 24

Reproduction steps

Try the example above with the latest VS Code extension

Additional Notes

Using version 3.6.18. It has worked in a previous version.

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionUser has a question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions