lua-language-server
lua-language-server copied to clipboard
`---@cast` annotation doesn't respect scope rules
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
The ---@cast
annotation should respect Lua scoping rules.
Actual Behaviour
Let's consider:
--- @class foo
--- @field a number
--- @class bar
--- @field b string
--- @type { [integer]: foo|bar }
local array = {}
for i, instance in ipairs(array) do
if instance.a then
-- First if block
--- @cast instance foo
end
if instance.b then
-- Second if block
end
end
In the first if block, we cast the instance
to the foo
type. When hovering over the instance
variable in the second if block we can see its type is foo
. It would be expected for its type to be bar
since the ---@cast
happened in different scope.
Reproduction steps
- Copy-paste the given example code into your text editor.
- Hover over the
instance
variable in the second if block. - See the error in the popup.
Additional Notes
No response
Log File
No response