LuaLS / LuaLS/lua-language-server

Widening cast has no effect

Đang mở
#2,215 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.

bug
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?

Linux

What is the issue affecting?

Annotations, Type Checking, Hover

Expected Behaviour

Cast unconditionally overwrites the variable's type with the new type.

Actual Behaviour

Cast is ignored and in some cases causes additional type check errors.

Reproduction steps
---@param str string
local function foo(str)
    ---@cast str +?
    if str == '' then str = nil end
end

Note how it produces an error on assignment:

This variable is defined as type `string`. Cannot convert its type to `nil`.
- `nil` cannot match `string`
- Type `nil` cannot match `string` Lua Diagnostics.(cast-local-type)

Note also that the tooltip for str in the comparison says correctly that it is a string?. But this has no observable effect on the actual type.

Example two (attempted workaround):

---@param str string
local function foo(str)
    ---@cast str string?
    if str == '' then str = nil end
end

Note how the cast causes a type check error now, which makes no sense.

Example three (from the wiki on cast, modified to actually verify that the cast worked):

---@type integer
local x

---@cast x +boolean
x = false

Note how it produces an error, similarly to the first example. You would expect at least the example from the official documentation itself to work, but it doesn't. This leads me to conclude this is a bug.

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 chạy ba bản tái hiện bằng Lua trong issue và so sánh thông tin hover với các chẩn đoán phép gán. Truy vết việc xử lý cast và đường dẫn kiểm tra kiểu tiếp theo đối với các biến cục bộ, sau đó thêm hoặc cập nhật coverage để các cast mở rộng ảnh hưởng đến việc kiểm tra phép gán mà không tạo ra các lỗi mâu thuẫn.

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.