LuaLS / LuaLS/lua-language-server
Casting an otherwise unused variable in a block preserves the cast outside of block
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
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
Any casts inside a block should not be maintained once leaving that block.
Actual Behaviour
Type cast is maintained even after the block in which the variable was cast if and only if the variable was not used in that block.
Reproduction steps
This is incorrect:
---@type number|string
local i
if true then --can be `false` or any other logical
---@cast i number
local _ = 0 --not referencing variable `i` that was cast above
end
local j = i --type of j is `number`, should be `number|string`
This is correct:
---@type number|string
local i
if true then --can be `false` or any other logical
---@cast i number
local _ = i -- referencing variable `i` that was cast above
end
local j = i --type of j is `number|string`
Additional Notes
No response
Log File
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce both Lua snippets through the Visual Studio Code extension and compare the inferred type of j after the block. Trace the type-checking path for @cast handling when the cast variable is unused, then verify that the cast does not survive the block while the in-block reference case remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100