LuaLS / LuaLS/lua-language-server

Missing diagnostic when passing nil-able fields to pairs/ipairs

Open
#2,429 1 comment 0 reactions 0 assignees View on GitHub

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?

Windows

What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

Expected Behaviour

In the following code, I expect to get param-type mismatch for each of the for loops (with pairs and ipairs) because the variable being iterated can be nil:

---@type string[]?
local x1 = {}

for _ in ipairs(x1) do  -- param-type-mismatch: Cannot assign `string[]?` to parameter `<T:table>
    break
end

---@type table<string, boolean>?
local y1 = {}

for _ in ipairs(y1) do -- param-type-mismatch: Cannot assign `table<string, boolean>?` to parameter `<T:table>
    break
end

---@class Class
---@field x string[]?
---@field y table<string, boolean>?

---@type Class
local x2 = {}

for _ in ipairs(x2.x) do -- Should warn, but it doesn't
    break
end

for _ in pairs(x2.y) do -- Should warn, but it doesn't
    break
end
Actual Behaviour

The param-type-mismatch diagnostic is only shown for the first two loops (direct variables without nesting/fields), but not when using fields (the third and fourth for loops).

Reproduction steps

Copy the code into a Lua file and observe the diagnostics

Additional Notes

No response

Log File

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue by copying the provided Lua example into a Lua file and comparing diagnostics for direct variables versus nested fields. Trace the type-checking path that handles ipairs and pairs arguments, then verify that nullable fields receive the same param-type-mismatch diagnostic as direct variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.