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

overload doesn't seem to match correctly #2509

Closed
Frityet opened this issue Feb 2, 2024 · 1 comment · Fixed by #2838
Closed

overload doesn't seem to match correctly #2509

Frityet opened this issue Feb 2, 2024 · 1 comment · Fixed by #2838

Comments

@Frityet
Copy link
Contributor

Frityet commented Feb 2, 2024

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

---@overload fun(x: ffi.cdata*, type: "rvalue"): gccjit.RValue*
---@overload fun(x: ffi.cdata*, type: "lvalue"): gccjit.LValue*
---@overload fun(x: ffi.cdata*, type: "type"): gccjit.Type*
local function expect(x, type)
   ...
end

local rval = expect(data, "rvalue")
--typeof rval should be "gccjit.RValue*"

Actual Behaviour

Screenshot 2024-02-02 at 12 20 34 Type is a union of all return types

Reproduction steps

  1. use this function:
---@overload fun(type: "int"): integer
---@overload fun(type: "str"): string
local function f(type)
   ...
end
  1. use it
local i = f"int"
local s = f"str"

Additional Notes

No response

Log File

No response

@tomlau10
Copy link
Contributor

tomlau10 commented Aug 25, 2024

I originally thought this is solved because I cannot reproduce it while working on PR #2822, but finally I found out it's the reproduction code not complete enough:

Reproduction code

---@class ffi.cdata*
---@class gccjit.RValue*
---@class gccjit.LValue*
---@class gccjit.Type*

---@overload fun(x: ffi.cdata*, type: "rvalue"): gccjit.RValue*
---@overload fun(x: ffi.cdata*, type: "lvalue"): gccjit.LValue*
---@overload fun(x: ffi.cdata*, type: "type"): gccjit.Type*
local function expect(x, type)
    if type == "lvalue" then
        return {}   --[[@as gccjit.LValue*]]
    elseif type == "rvalue" then
        return {}   --[[@as gccjit.RValue*]]
    elseif type == "type" then
        return {}   --[[@as gccjit.Type*]]
    end
end

---@type ffi.cdata*
local data

local a = expect(data, "rvalue")
    --> gccjit.LValue*|gccjit.RValue*|gccjit.Type*, bad
local b = expect(data, "lvalue")
    --> gccjit.LValue*, ok
local c = expect(data, "type")
    --> gccjit.Type*, ok
  • thus when passing "rvalue" as param, it matches the base function
    => causing local rval to be the combined type gccjit.LValue*|gccjit.RValue*|gccjit.Type*
  • but "lvalue" and "type" are not affected, this is similar to what described here: Overload intellisense issue with type of parameters #2708 (comment)
    where when a function has no @param but only @overload, luals will auto infer the param type based on the 1st overload, causing the base function signature to match a more specific param specified in overload

(Unfortunately my #2822 cannot fix this ☹️)

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

Successfully merging a pull request may close this issue.

2 participants