LuaLS / LuaLS/lua-language-server

overload type narrowing for returns

オープン
#2,933 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず issue にある overload-return の例を再現し、条件付き narrowing に対する language server の型チェックと補完の挙動を調べます。payload にはリポジトリのファイルやテストが記載されていないため、変更を加える前に、関連する overload と return-type の解析エントリーポイントを特定してください。resource が nil であることを確認した後に failstring に絞り込まれ、既存の警告が出ない状態になれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua
領域
devtools
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。