LuaLS / LuaLS/lua-language-server
Feature Request: Propagate and Validate Array Index Annotations from Immutable Variable Indexing
まだ誰も着手していません。
- 主要言語
- 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?
Annotations, Type Checking, Diagnostics/Syntax Checking
Expected Behaviour
The linter should propagate and validate the array index annotations from immutable variables, recognizing that the indices are constant and understanding the types associated with these indices.
local one = 1
---@class A
---@field [1] integer
---@field [2] string
---@type A
local a = {2, 'b'}
---@type integer
local b = a[one] -- no error
Actual Behaviour
The linter assumes that the value of the indices could be anything, even when the variables are immutable and clearly annotated. This leads to incorrect type warnings and reduces the effectiveness of the type checking.
local b = a[one] -- unexpectedly produces the following error; but 'local b = a[1]' does not.
Cannot assign string|integer to integer.
stringcannot matchinteger- Type
stringcannot matchintegerLua Diagnostics. (assign-type-mismatch)
Reproduction steps
Copy source in expected behavior into VS Code with extension v3.13.1 into a file with diagnostics enabled.
Additional Notes
I use prefer arrays instead of tables in my projects to avoid the cost of hashing the key and maximize the compressibility of the Lua script with external tools. Immutable local variables, used as enums, help maintain readability and maintainability. As noted by #2721 , a table seems unnecessary.
-
Using the enum annotation doesn't change the resulting behavior.
-
Using the alias annotation also doesn't change the resulting behavior.
---@alias a_1 1
---@type a_1
local one = 1
- The const annotation is not supported and would be another alternative to using alias.
Log File
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
診断を有効にした Visual Studio Code 拡張機能で、提供された Lua スニペットを再現し、その後、報告された assign-type-mismatch 警告を担当する配列インデックスの型チェックのエントリポイントを追跡します。変更の完了条件は、不変のローカルインデックスがその定数値として検証され、整数の代入について例が string|integer を報告しなくなり、直接インデックス指定は引き続き正しく機能することです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- lua
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100