LuaLS / LuaLS/lua-language-server
Optional not removed in if/elseif block for field in filter
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the two narrowing examples in the Visual Studio Code extension, then inspect script/vm/tracer.lua around lines 240-243, the location mentioned in the issue. Done means the field access t.n is narrowed to number in the if branch while remaining number? in the else branch, matching the local-variable behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100