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

`---@cast` annotation doesn't respect scope rules

Open C3pa opened this issue 7 months ago • 3 comments

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. image

Reproduction steps

  1. Copy-paste the given example code into your text editor.
  2. Hover over the instance variable in the second if block.
  3. See the error in the popup.

Additional Notes

No response

Log File

No response

C3pa avatar Jul 15 '24 12:07 C3pa