LuaLS / LuaLS/lua-language-server

When the subclass inherits the constructor of the parent class, generics do not work (the type of `self` cannot be used for annotation)

Đang mở
#2,706 1 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?

Other

Which OS are you using?

Windows

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

---@class Vehicle
Vehicle = {}

---comment
---@generic T
---@param self `T`
---@param o? T
---@return T
function Vehicle:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

---@class Plane: Vehicle
Plane = {}

setmetatable(Plane, { __index__ = Vehicle })

local obj = Plane.new()

图片

图片

The code prompt given when calling a constructor annotated with generic parameters that is inherited by a subclass is incorrect.

The prompt message should be

 function Plane.new(self: Plane, o?: Plane)

And the type of the obj variable in the code hint should be Plane instead of unknown

Actual Behaviour
---@param o? self
---@return self
function Vehicle:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

self should be used to refer to the type of the method caller. And the self method should replace the generic method to annotate the type in this use case

Such type annotations should work, and code completion should show self as the caller's type

Reproduction steps
  1. Copy example to editor
  2. Call code prompt
  3. The prompt result is not as expected
Additional Notes

No response

Log File

No response

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

Bắt đầu bằng cách tái hiện ví dụ Vehicle và Plane được cung cấp trong trình chỉnh sửa, rồi theo dõi quá trình suy luận generic của hàm tạo và kiểu self cho các phương thức kế thừa. Xác minh rằng lời nhắc mã hiển thị function Plane.new(self: Plane, o?: Plane) và kiểu được suy luận của objPlane thay vì unknown.

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
35/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.