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

Expressions with union types #1996

Closed
AndreasMatthias opened this issue Mar 10, 2023 · 1 comment
Closed

Expressions with union types #1996

AndreasMatthias opened this issue Mar 10, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@AndreasMatthias
Copy link
Contributor

How are you using the lua-language-server?

Other

Which OS are you using?

Linux

What is the issue affecting?

Type Checking

Expected Behaviour

See "Reproduction steps".

Actual Behaviour

See "Reproduction steps".

Reproduction steps

Detecting the type of an expression (e.g. operator mul()) fails, if the second operand is a union type.
Interestingly, the type is identified correctly if the first operand is a union type.

Here is an example. Lines are marked 'wrong' if detection of the correct type fails.

---@class Foo
---@operator mul(Foo): Foo
---@operator mul(Bar): Foo
---@class Bar
---@operator mul(Foo): Foo
---@operator mul(Bar): Foo

---@param foo Foo
---@param bar Bar
local function func1(foo, bar)
   local a = foo * foo  -- ok: Foo
   local b = foo * bar  -- ok: Foo
   local c = bar * foo  -- ok: Foo
   local d = bar * bar  -- ok: Foo
end

---@param foo Foo
---@param bar Bar|Foo
local function func2(foo, bar)
   local a = foo * foo  -- ok: Foo
   local b = foo * bar  -- wrong: number
   local c = bar * foo  -- ok: Foo
   local d = bar * bar  -- wrong: number
end

---@param foo Foo|Bar
---@param bar Bar
local function func3(foo, bar)
    local a = foo * foo -- wrong: number
    local b = foo * bar -- ok: Foo
    local c = bar * foo -- wrong: number
    local d = bar * bar -- ok: Foo
end

This can quickly become disturbing if you get a warning for each function saying that it returns a "wrong" type:

---@param foo Foo|Bar
---@param bar Bar
---@return Foo
local function func4(foo, bar)
   local ret = bar * foo
   return ret  -- incorrectly warning about wrong type
end

I understand that it can get quite difficult to determine the correct type. Would it be reasonable to give an expression a special "undefined type" if calculation of the correct type is too cumbersome? And then add a user switch stating whether or not a warning should be issued if an expression has "undefined type"?

Additional Notes

No response

Log File

No response

@sumneko sumneko added the bug Something isn't working label Mar 13, 2023
sumneko added a commit that referenced this issue Mar 13, 2023
@AndreasMatthias
Copy link
Contributor Author

Thank you! This is great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants