-
-
Notifications
You must be signed in to change notification settings - Fork 343
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
Enum type is lost when given as union #3104
Comments
AFAIK, when you define
---@param unitID integer
---@param unitDefID integer
---@param action
--- | -1 # Build
--- | CMD # all command enums
---@return boolean actionAllowed
function SyncedCallins:AllowBuilderHoldFire(unitID, unitDefID, action) end => then type checking can be done correctly
Additional noteI know that some users even make use of the unintended enum type behavior mentioned above, to create opaque enum:
inside the gist link of this comment: #2758 (comment) and #2758 (comment) |
The problem here is that this function doesn't accept all of
Right, so essentially manually implementing my own ---Command constants.
---@enum CMD
CMD = {
---@alias CMD.REPAIR 40
---@type CMD.REPAIR
REPAIR = nil,
---@alias CMD.RECLAIM 90
---@type CMD.RECLAIM
RECLAIM = nil,
---@alias CMD.RESTORE 110
---@type CMD.RESTORE
RESTORE = nil,
---@alias CMD.RESURRECT 125
---@type CMD.RESURRECT
RESURRECT = nil,
---@alias CMD.CAPTURE 130
---@type CMD.CAPTURE
CAPTURE = nil,
}
---@param cmd CMD.REPAIR|CMD.CAPTURE
function captureOrRepair(cmd)
end
captureOrRepair(CMD.REPAIR)
captureOrRepair(CMD.RECLAIM)
---@param cmd CMD
function anyCmd(cmd)
end
anyCmd(CMD.RESTORE)
anyCmd(1337) The problem with this approach:
Would it be difficult to change the implementation so the It seems as though something similar to the above could be done implicitly to each enum value to make it a fully-fledged type/alias. |
I think the root issue here is that those internal enum subtype have undefined behaviour.
I am not familiar with this part of the codebase, so I can't tell 🙁
Just a few observations here:
So conceptually, you are now using ---@class MyClass
---@field a integer
---@param p MyClass.a # this is invalid, and conceptually incorrect
function test(p) end |
I appreciate the explanation of how it works and the problems, but I think I might wait for a developer to respond then so I can work out how to move forward. I think I have two best choices to address this problem in the immediate: Option 1 Respect current limitations and rely on param description for usage: ---Called when a construction unit wants to "use his nano beams".
---
---@param unitID integer
---@param unitDefID integer
---@param action (-1| CMD) Will be -1 when this is a build action. Otherwise will be `CMD.RECLAIM`, `CMD....`.
---@return boolean actionAllowed
function SyncedCallins:AllowBuilderHoldFire(unitID, unitDefID, action) end Option 2 Export two versions of the library, one with the hacky |
By experimenting with your recent example, I might have found another workaround 🤔
---Command constants.
---@enum CMD
CMD = {
---@type 40|CMD.REPAIR
REPAIR = nil,
---@type 90|CMD.RECLAIM
RECLAIM = nil,
---@type 110|CMD.RESTORE
RESTORE = nil,
---@type 125|CMD.RESURRECT
RESURRECT = nil,
---@type 130|CMD.CAPTURE
CAPTURE = nil,
} the good
the badBut this has some side effects:
|
Interesting! I will experiment with these approaches. Given I'm adding types to a massive two-decade old project with tens (hundreds?) of contributors and years of development ahead of it, I should probably err on the side of simplicity rather than overfitting the types to the current version of LLS I think. I do think changing this behaviour in LLS would be a big win for the project (seems this issue of enums arises in a few contexts, as you pointed out). Thanks for helping explain and explore this issue @tomlau10. 🙏 |
Currently breaks: LLS: LuaLS/lua-language-server#3104 Docs: CppCXY/emmylua-analyzer-rust#129
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows WSL
What is the issue affecting?
Type Checking
Expected Behaviour
A parameter of union of enum entries should accept those entries.
Actual Behaviour
The enum type is being resolved to
40
and losing information about it being an enum.Reproduction steps
Paste code example above into VSCode.
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered: