LuaLS / LuaLS/lua-language-server
`---@cast` annotation doesn't respect scope rules
未關閉
還沒有人認領這個 Issue。
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 442
- PR 合併指標
- 30 天內沒有已合併 PR
描述
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
instancevariable in the second if block. - See the error in the popup.
Additional Notes
No response
Log File
No response
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先,在 Visual Studio Code 擴充功能中使用提供的 Lua 範例重現問題,並檢查型別檢查如何處理兩個 if 區塊的作用域。完成條件是,對 foo 的 cast 僅在第一個區塊內套用,而第二個區塊中的執行個體保留適當的推斷型別,而不是 foo。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- devtools
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 42/100