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

Index an enum-value table bug #2921

Open
ChouUn opened this issue Oct 30, 2024 · 1 comment
Open

Index an enum-value table bug #2921

ChouUn opened this issue Oct 30, 2024 · 1 comment

Comments

@ChouUn
Copy link

ChouUn commented Oct 30, 2024

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

expected the value type of mapping when index

Actual Behaviour

got an unknown

Reproduction steps

---@enum(key) AttrName
local o = {
    ['hp_max'] = true,
}
local map = {
    ['hp_max'] = 114514,
}
---@param t table<AttrName, number>
local function test(t)
    --[[
        Usage 1: correct
    ]]
    local a = 'hp_max'
    local b = t[a]  -- expected: number, got: number
    print(b)

    --[[
        Usage 2: incorrect
    ]]
    local c = t['hp_max'] -- expected: number, got: unknown
    print(c)

    --[[
        Usage 3: correct
    ]]
    local d = "hp_grow"
    local e = t[d] -- expected: unknown, got: unknown
    print(e)
end
test(map)

Additional Notes

No response

Log File

No response

@tomlau10
Copy link
Contributor

similar issue: #2791


And as a bonus case for the code snippet above:

--[[
    Usage 4: incorrect
]]
local x
local y = t[x] -- expected: unknown, got: number

seems when using unknown key to index this table, it will incorrectly infer as number as well

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

No branches or pull requests

2 participants