LuaLS / LuaLS/lua-language-server
Issue when using tables/class with generics?
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Lua
- Star
- 4.4k
- Fork
- 442
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
### 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
In the following code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
I would hope that the inferred type of k and v would work the same in each instance where k is inferred to be a string and v is inferred to be a string.
### Actual Behaviour
In the following code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
the k and in line: `for k, v in pairs(test) do end` and the k in line` for k, v in pairs(test_table) do end` get the error `can not infer type` and are typed as `unknown`.
I assume there is some issue here with generics as the pairs definition uses them heavily
```
---@generic T: table, K, V
---@param t T
---@return fun(table: table, index?: K):K, V
---@return T
```
but this is only a guess.
### Reproduction steps
1. Copy this code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
2. See errors
### Additional Notes
_No response_
### Log File
_No response_
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện đoạn mã được cung cấp trong extension Visual Studio Code, sau đó kiểm tra định nghĩa `pairs` và các kiểu trả về generic của nó. Bản sửa hoàn tất khi `k` và `v` được suy luận là string cho các trường hợp `TestClass`, bảng chưa khởi tạo và bảng đã khởi tạo mà không có lỗi `can not infer type`.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- lua
- Lĩnh vực
- devtools
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100