LuaLS / LuaLS/lua-language-server

A key and value of the nested table are not defined

Open
#1,736 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug feat/generic
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?

Other

### Expected Behaviour


![image](https://user-images.githubusercontent.com/57856647/204633131-b651e904-c5dd-4d46-96dc-87f7033a3119.png)

![image](https://user-images.githubusercontent.com/57856647/204633153-d8209139-9ce7-4543-b1c4-37e37c1f536e.png)

![image](https://user-images.githubusercontent.com/57856647/204633184-cab71a17-2f75-44ac-95ba-98bab0877005.png)

### Actual Behaviour

![image](https://user-images.githubusercontent.com/57856647/204633081-2bd4dd79-cae1-4fdb-a78a-c5151897a76e.png)

![image](https://user-images.githubusercontent.com/57856647/204632236-1370ae1e-aa2c-4544-afc5-af330e117ae9.png)

![image](https://user-images.githubusercontent.com/57856647/204632254-2359446b-6365-4064-a1a1-e8c13ed90450.png)

### Reproduction steps

Maybe I'm doing something wrong, but it seems to have worked before

```lua
---@class Test
---@field ID integer
local Test = {};

---@param id integer
---@return Test
function Test:new(id)
return setmetatable({ ID = id }, { __index = self });
end

---@type table >
local tab = {
[1] = { 1.0, 2.1, 3.2 },
[2] = { 4.0, 5.1, 6.2 },
[3] = { 7.0, 8.1, 9.2 }
}

---@generic T
---@param test table
---@return table
local function convert2Test(test)
local t = {};
for k, v in pairs(test) do
t[Test:new(k)] = v;
end
return t;
end

local newTab = convert2Test(tab);

for k, v in pairs(newTab) do
print(k, v); -- ok! k and v detected
end

local test = Test:new(3);
local info = newTab[test];

if info then
for k, v in pairs(info) do
print(k, v); -- oops! k and v not detected
end
end
```

### 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

Start with the Lua reproduction in the issue and compare the inferred types in the expected and actual screenshots. Investigate the nested table type used for tab and the value returned through convert2Test. Done means the inner table's key and value types are inferred in the final pairs loop.

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.