Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with booleans #1294

Closed
firas-assaad opened this issue Jul 7, 2022 · 0 comments
Closed

Issues with booleans #1294

firas-assaad opened this issue Jul 7, 2022 · 0 comments
Labels
bug Something isn't working good issue Art of asking
Milestone

Comments

@firas-assaad
Copy link
Contributor

Describe the bug
In certain cases, boolean inferred values don't work as expected. This usually happens when calling functions that don't have explicit @return annotations and that return true or false explicitly (e.g. return false).

To Reproduce
Example 1:

---@param int integer
local function x(int)
end

local function y()
    if math.random() > 0.5 then
        return true
    else
        return false
    end
end

local bool = y()
local should_be_int = bool and 300 or 500
x(should_be_int) 
-- Warning: Cannot assign `integer|true` to parameter `integer`.Lua Diagnostics.(param-type-mismatch)

should_be_int is inferred as integer|true. If I annotate the function y with @return boolean or if function y is rewritten as return math.random() > 0.5, then should_be_int is correctly deduced as integer.

Example 2:

local function y()
    if math.random() < 0.5 then
        return true
    else
        return false
    end
end

---@return boolean
local function x()
    local bool = true

    bool = y()

    bool = bool and y()

    if bool then
        print(1)
    end

    return bool
    -- Warning: The type of the 1 return value is `boolean`, but the actual return is `boolean|true|false`
end

The variable bool is inferred as boolean|true|false which is somehow incompatible with the specified @return. If I remove the @return annotation, or if the y function doesn't explicitly return true/false, or if you change the body of the function x, the warning does not happen.

Expected behavior
The boolean return types should be treated the same in all cases.

Screenshots
I wrote down the warnings in the code snippets, but if you want screenshots let me know.

Environment (please complete the following information):

  • OS: Windows 10
  • Is WSL remote? No
  • Client: VSCode 1.68.1
  • lua-language-server version v3.4.2
@sumneko sumneko added the bug Something isn't working label Jul 7, 2022
@sumneko sumneko added this to the 3.5.0 milestone Jul 7, 2022
@sumneko sumneko added the good issue Art of asking label Jul 7, 2022
sumneko added a commit that referenced this issue Jul 7, 2022
@sumneko sumneko closed this as completed in 3150962 Jul 7, 2022
@sumneko sumneko mentioned this issue Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good issue Art of asking
Projects
None yet
Development

No branches or pull requests

2 participants