LuaLS / LuaLS/lua-language-server

Problem with methods of generic classes

Đang mở
#3,339 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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, Annotations

### Expected Behaviour

1. Define a generic base class Foo with a method function which returns the generic type
2. Inherit from the base class and specify the generic type
3. Use the method and expect the specified generic type on the output

```lua
---@class Foo
---@field myValue T
local base = {}

---@return T
function base:get()
return self.myValue
end

---@type Foo
local ex1 = base --Use class directly
---@class Example2 : Foo
local ex2 = ex1 --Start with a version with generic type specified
---@class Example3 : Foo
local ex3 = base --Start with a version without the generic type specified

local I = ex1.myValue --Correct: number
local a = ex1:get() --Correct: number
local II = ex2.myValue --Correct: string
local b = ex2:get() --Correct: string
local III = ex3.myValue --Correct: boolean
local c = ex3:get() --Correct: boolean
```

### Actual Behaviour

Attempting to expand the parent class directly displays a warning, (*Example 3*) but using a variable with specified type does not (*Example 2*).
The method defined in the parent class fails to infer the generic type from the child classes

```lua
---@class Foo
---@field myValue T
local base = {}

---@return T
function base:get()
return self.myValue
end

---@type Foo
local ex1 = base
---@class Example2 : Foo
local ex2 = ex1
---@class Example3 : Foo
local ex3 = base --Cannot assign 'Foo' to 'Example3'

local I = ex1.myValue --Correct: number
local a = ex1:get() --Correct: number
local II = ex2.myValue --Correct: string
local b = ex2:get() --Incorrect: T
local III = ex3.myValue --Correct: boolean
local c = ex3:get() --Incorrect: T
```

### Reproduction steps

1. Define a generic base class with a method function which returns the generic type
2. Inherit from the base class and specify the generic type
3. Use the method and expect the specified generic type on the output

### Additional Notes

_No response_

### Log File

[service.log](https://github.com/user-attachments/files/24821399/service.log)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ kế thừa generic của Foo từ issue trong extension Visual Studio Code và kiểm tra service.log để xem các chẩn đoán kiểm tra kiểu. Theo dõi cách các phương thức kế thừa thay thế các kiểu trả về generic, sau đó xác minh rằng get() được phân giải thành string và boolean tương ứng với Example2 và Example3 mà không tạo ra các cảnh báo không chính xác.

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
developer-experience, 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
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.