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

field function bug #3089

Open
iamhaiwen opened this issue Feb 23, 2025 · 1 comment
Open

field function bug #3089

iamhaiwen opened this issue Feb 23, 2025 · 1 comment

Comments

@iamhaiwen
Copy link

iamhaiwen commented Feb 23, 2025

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

Looking at this code, the variable local bb should be of type fun(x: number, y: number).
the variable local aa should be of type number.

`---@Class Person
---@field age? number
---@field foo? fun(x: number, y: number)

---@param person Person
local function test(person)
if (person.age ~= nil) then
local aa = person.age -- type is number?
end
if (person.foo ~= nil) then
local bb= person.foo -- type is unknown
end
end
`

Actual Behaviour

the variable local aa be of type number?
the variable local bb be of type unknown

Reproduction steps

  1. Go to '...'
  2. Click '...'
  3. See error '...'

Additional Notes

No response

Log File

No response

@tomlau10
Copy link
Contributor

this is the result of a git bisect 🤔

5c3086acd08765f9122ed5b7f806e2b2bf3737e3 is the first bad commit
commit 5c3086acd08765f9122ed5b7f806e2b2bf3737e3 (HEAD)
Author: Lewis Russell <[email protected]>
Date:   Sun Sep 22 17:19:12 2024 +0100

    feat: type narrow types with literal fields

 changelog.md                   |  1 +
 script/vm/tracer.lua           | 59 ++++++++++++++++++++++++++++++++++++++++++
 test/type_inference/common.lua | 50 +++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)

which is #2864 by @lewis6991


Some observation: by flipping the ~= with ==, the function type appears again:

---@class Person
---@field age? number
---@field foo fun(x: number, y: number)

---@param person Person
local function test(person)
    if (person.age ~= nil) then
        local aa = person.age -- type is number?
    end
    if (person.foo == nil) then --< flip this to `==`
        local bb = person.foo --> fun(x: number, y: number)
    end
end

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