LuaLS / LuaLS/lua-language-server
overload type narrowing for returns
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?
Linux
What is the issue affecting?
Type Checking, Completion
Expected Behaviour
I am not sure this is a duplicate issue considering how common it probably is, but just to make sure.
Possibly a duplicate of https://github.com/LuaLS/lua-language-server/issues/1583.
This is similar to https://github.com/LuaLS/lua-language-server/issues/1146, but with returns instead of params.
It is a common pattern in Lua that on errors you might instead return a nil value to imply failure, along side an error message.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3)) -- fail type here is expected to be "string"
end
Actual Behaviour
You get a warning on fail that it might possibly be nil. Because its type is string|nil.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3) )-- fail type here is actually "string|nil"
end
Reproduction steps
- Copy the setup code
- follow along with the comments
Additional Notes
I believe narrowing down the types in this case is still possible? It seems to me like https://github.com/LuaLS/lua-language-server/issues/1583 was discussing the possibility of adding tuples, but shouldn't this be doable even without tuples support?
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 ví dụ về giá trị trả về của overload trong issue và kiểm tra hành vi kiểm tra kiểu cũng như tính năng hoàn thành của language server đối với việc thu hẹp có điều kiện. Payload không nêu tên tệp hoặc bài kiểm thử nào trong repository, vì vậy hãy xác định các điểm vào liên quan đến việc phân tích overload và kiểu trả về trước khi thực hiện thay đổi. Hoàn tất có nghĩa là fail được thu hẹp thành string sau khi kiểm tra rằng resource là nil, mà không có cảnh báo hiện tại.
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
- Tính năng
- Độ 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