LuaLS / LuaLS/lua-language-server

Casting an otherwise unused variable in a block preserves the cast outside of block

Open
#2,393 0 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.