LuaLS / LuaLS/lua-language-server

Accessing deeply nested variable at 'if' statemet cause type collision

未關閉
#2,725 6 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 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

Type checking does not breaks after accessing variable at 'if' statement.

### Actual Behaviour

2nd nested variable type became '\ | unknown' so accessing variable or function which not exists in this class does not cause warning.

### Reproduction steps

```lua
---@class DemoD
---@field c integer
local DemoD = {}
DemoD.c = 0

---@class DemoC
local DemoC = {}
DemoC.D = DemoD

---@class DemoB
local DemoB = {}
DemoB.C = DemoC

---@class DemoA
local DemoA = {}
DemoA.B = DemoB

---@class DemoRoot
DemoRoot = {}
DemoRoot.A = DemoA

local d = DemoRoot.A.B.C.D

-- if d.c > 1 then end -- if we replace 'DemoRoot.A.B.C.D' with 'd' then F is undefined in all 4 cases
if DemoRoot.A.B.C.D.c > 1 then end

DemoRoot.F() -- warning: F is undefined
DemoRoot.A.F() -- warning: F is undefined
DemoRoot.A.B.F() -- no warning
DemoRoot.A.B.C.F() -- no warning
```
![if-statement-bug](https://github.com/LuaLS/lua-language-server/assets/27363039/982f29e7-3ae5-4153-a438-68fd0e6b34ad)

### Additional Notes

_No response_

### Log File

_No response_

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,使用 issue 中的 Lua 程式碼片段重現回報的行為,並比較 Visual Studio Code 擴充功能所使用的型別檢查器中的直接形式與區域變數形式。當 DemoRoot.A.B.F() 和 DemoRoot.A.B.C.F() 等無效成員產生未定義成員警告,同時不破壞有效的巢狀存取時,修正才算完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
lua
領域
devtools
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。