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

Optional not removed in if/elseif block for field in filter

Open d-enk opened this issue 1 year ago • 0 comments

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Linux

What is the issue affecting?

Type Checking

Expected Behaviour

---@type { n: number? }
local t = { n = 1 }

if t.n then
	_ = t.n -- (field) t.n: number
else
	_ = t.n -- (field) t.n: number?
end

Actual Behaviour

---@type number?
local n = 0

if n then
	_ = n -- local t: number
else
	_ = n -- local t: nil
end

-- but

---@type { n: number? }
local t = { n = 1 }

if t.n then
	_ = t.n -- (field) t.n: number? !!!
else
	_ = t.n -- (field) t.n: number?
end

Additional Notes

I couldn't immediately understand where exactly this type casting is happening

Looks like here https://github.com/LuaLS/lua-language-server/blob/34ff9d3ca730bc28879ab2b0c1a49f2c5480f9a3/script/vm/tracer.lua#L240-L243 but adding debug log nothing came out

I would be grateful for the information, I could try to fix it myself

d-enk avatar Apr 02 '24 10:04 d-enk