LuaLS / LuaLS/lua-language-server

overload type narrowing for returns

未关闭
#2,933 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

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
  1. Copy the setup code
  2. 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现 issue 中的重载返回示例,并检查 language server 针对条件收窄的类型检查和补全行为。payload 没有列出仓库文件或测试,因此请在进行更改之前,定位相关的重载和返回类型分析入口。完成的标准是:在检查 resource 为 nil 后,fail 能收窄为 string,且不再出现现有警告。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
devtools
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。