lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Enums are not repsected correctly when used as params or return values

Open projecteon opened this issue 3 years ago • 4 comments

This should not be valid

---@param ... ImGuiWindowFlags Flags to xor
---@return ImGuiWindowFlags
function bit32.bor(...) end

local parsesAsValid= bit32.bor(ImGuiStyleVar.FramePadding)

I get the feeling the enum gets parsed as its inner type (which is integer/number), and since both types have that it thinks its valid.

projecteon avatar Sep 11 '22 13:09 projecteon

Are you able to provide a simple example of the issue where all the types are defined in it? I am unfamiliar with the aliases/enums you are using here.

carsakiller avatar Sep 13 '22 02:09 carsakiller

---@enum MyEnum
MyEnum = {
  All = 0,
  None = 0
}

---@enum MyOtherEnum
MyOtherEnum = {
  Some = 0,
  Filter = 0
}

---@param ... MyEnum
---@return MyEnum
function bit32.bor(...) end

---- This gives correct autocompletion but shows no error if I enter an "invalid" enum
bit32.bor(MyEnum.All, MyOtherEnum.Filter)


---@param ... MyEnum|MyOtherEnum
---@return MyEnum|MyOtherEnum
function bit32.bor2(...) end

---- I expect this to give an error if I mix enums, should only alof one or the other
bit32.bor3(MyEnum.All, MyOtherEnum.Filter)

---@vararg MyEnum
---@return MyEnum
function bit32.bor3(...) end

---- Same as the first but using vararg
bit32.bor3(MyEnum.All, MyOtherEnum.Filter)

Would love to be able to do

---@generic T : MyEnum|MyOtherEnum
---@param ... T
---@return T
function bit32.bor2(...) end

projecteon avatar Sep 13 '22 08:09 projecteon

Ah okay, I see. The enums are being validated based on their value but you would like to ensure that the entered enum matches the requested enum?

In this example, they all do have a value of 0 so there is no harm regardless of which enum they use. I could see a library wanting to enforce the use of the correct enum though.

carsakiller avatar Sep 13 '22 16:09 carsakiller

The whole point of an enum is to ensure correct enums are used in my honest opinion. Or I might just make constants.

I hope this can be fixed :)

projecteon avatar Sep 14 '22 10:09 projecteon