LuaLS / LuaLS/lua-language-server

`---@cast` annotation doesn't respect scope rules

オープン
#2,761 コメント 3 件 リアクション 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?

Windows

What is the issue affecting?

Type Checking

Expected Behaviour

The ---@cast annotation should respect Lua scoping rules.

Actual Behaviour

Let's consider:

--- @class foo
--- @field a number

--- @class bar
--- @field b string

--- @type { [integer]: foo|bar }
local array = {}

for i, instance in ipairs(array) do
	if instance.a then
                -- First if block
		--- @cast instance foo
	end

	if instance.b then
                -- Second if block
	end
end

In the first if block, we cast the instance to the foo type. When hovering over the instance variable in the second if block we can see its type is foo. It would be expected for its type to be bar since the ---@cast happened in different scope.
image

Reproduction steps
  1. Copy-paste the given example code into your text editor.
  2. Hover over the instance variable in the second if block.
  3. See the error in the popup.
Additional Notes

No response

Log File

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、Visual Studio Code 拡張機能で提供されている Lua の例を使って問題を再現し、型チェックが 2 つの if ブロックのスコープをどのように扱うかを調べます。完了条件は、foo へのキャストが最初のブロック内でのみ適用され、2 番目のブロックのインスタンスが foo ではなく適切な推論型を保持することです。

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

評価

技術スタック
lua
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
42/100

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

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