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

If clause overwrites number like class type on variable #1679

Closed
muppet9010 opened this issue Nov 8, 2022 · 2 comments
Closed

If clause overwrites number like class type on variable #1679

muppet9010 opened this issue Nov 8, 2022 · 2 comments
Labels
Info Needed More information is required

Comments

@muppet9010
Copy link

muppet9010 commented Nov 8, 2022

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Type Checking

Expected Behaviour

I wouldn't expect doing an if check on a variable to change its type.

I am using this with the Factorio game API which includes a lot of number or integer like types for subsets of these. I have included an example typedef for one of these.

In 3.5.x this code wasn't warned about, but in 3.6.1 it is.

Actual Behaviour

The type of my variables are being overwritten by the type that they are compared to in an if statement. The work around I found was to type all of these comparison values, but it feels a bit clunky.

Reproduction steps

---@class ASmallNumber # A double type number between 0 and 1 inclusive.

---@param aSmallNumber1 ASmallNumber
---@param aSmallNumber2 ASmallNumber
local aFunc = function(aSmallNumber1, aSmallNumber2)
    ---@type ASmallNumber, ASmallNumber, ASmallNumber
    local aNumber1, aNumber2, aNumber3

    if aSmallNumber1 == 0.25 or aSmallNumber1 == 0.5 then
        aNumber2 = aSmallNumber1 -- This is complained about: This variable is defined as type `ASmallNumber`. Cannot convert         its type to `number`.Lua Diagnostics.(cast-local-type)
        -- aSmallerNumber1 has a type of `number` here, but I explicitly defined it in the function param as being type `ASmallNumber`.
    end

    if aSmallNumber2 == (0.25) --[[@as ASmallNumber]] or aSmallNumber2 == (0.5) --[[@as ASmallNumber]] then
        aNumber3 = aSmallNumber2
        -- As I typed the `if` comparison values this is fine as the `aSmallNumber2` variable still has a type of `ASmallNumber`.
    end
end

The same occurs with just a locally defined variable that has its type and value set as type ASmallNumber. When comparing with an equivilent if the explicitly defined type is overridden.

local aSmallNumber = 0.5 --[[@as ASmallNumber]] ---@type ASmallNumber

Additional Notes

Fuller local variable example:

---@type ASmallNumber
local aNumber = 0.5 --[[@as ASmallNumber]]
---@type ASmallNumber
local aSecondNumber = 0.5 --[[@as ASmallNumber]]

---@param smallNumberValue ASmallNumber
local funcRequiringASmallNumber = function(smallNumberValue) end

if aNumber == 0.25 or aNumber == 0.5 then
    funcRequiringASmallNumber(aNumber)
end

if aSecondNumber == (0.25) --[[@as ASmallNumber]] or aSecondNumber == (0.5) --[[@as ASmallNumber]] then
    funcRequiringASmallNumber(aSecondNumber)
end

Log File

No response

@sumneko
Copy link
Collaborator

sumneko commented Nov 9, 2022

If your class is defined by ---@class ASmallNumber: number?

@sumneko sumneko added the Info Needed More information is required label Nov 9, 2022
@sumneko
Copy link
Collaborator

sumneko commented Nov 9, 2022

---@class SmallNumber: number
---@class Integer: number

---@type SmallNumber, Integer
local x, y

if x == 0.1 and y == 0.1 then
    print(x, y) --> What are the types for x and y here?
end

@sumneko sumneko closed this as completed in 2a9570c Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Info Needed More information is required
Projects
None yet
Development

No branches or pull requests

2 participants