LuaLS / LuaLS/lua-language-server

Type checking does not work at all with `` (backtick) literals

未关闭
#2,732 0 条评论 4 个 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?

Windows

What is the issue affecting?

Type Checking

Expected Behaviour
A = 1
B = 2
---@type `A`
local foo = A -- this should be fine
---@type `A`
local bar = B -- but this should give a type error
---@type `A`
local baz = 1 -- ideally, so should this (because you didn't use the literal name "A")

---@type `A`|string
local var = A -- this should be fine
Actual Behaviour
A = 1
B = 2
---@type `A`
local foo = -- A is correctly the only autocomplete suggestion
---@type `A`
local bar = 3 -- but this does not give any type error

---@param a `A`
function f(a) end
local baz = f("Hello") -- nor does this

---@type `A`|string
local var = A --[[Cannot assign `integer` to `string|`A``.
- `integer` cannot match `string|`A``
- Type `integer` cannot match `string`
- Type `number` cannot match `string`
Lua Diagnostics.(assign-type-mismatch)
]]
Reproduction steps
  1. Extension version v3.9.3
Additional Notes

This is important for things like

A = 1
B = 2
---@alias Enum `A`|`B`

("enums" without tables) to be type checked, like in the "Literal Custom Type" example of https://luals.github.io/wiki/annotations/#alias

I experimented for a bit trying to fix the problem, but I can't quite find where the problem lies. All I know is that the backtick literals aren't being interpreted as types for the purposes of type checking.

In any case, here are some tests for test/diagnostics/assign-type-mismatch.lua to make sure it's fixed:

TEST [[
local A = "Hello"
local B = 2

---@type `A`
local x = A

---@type `B`
local y = B
]]

TEST [[
local A = "Hello"

---@alias myLiteralAliases `A` | integer

---@type myLiteralAliases
local x = A

---@type myLiteralAliases
local y = 3
]]

TEST [[
local B = 2

---@alias myLiteralAliases `B` | string

---@type myLiteralAliases
local x = B

---@type myLiteralAliases
local y = "Hello"
]]

TEST [[
local A = "Hello"
local B = 2

---@alias myLiteralAliases `A`|`B`

---@type myLiteralAliases
--local x = A

---@type myLiteralAliases
--local y = B
]]

TEST [[
local A = "Hello"
local B = "World"

---@alias myLiteralAliases `A`|`B`

---@type myLiteralAliases
--local <!x!> = 1

---@type myLiteralAliases
--local <!y!> = "A"
]]

TEST [[
local A = 1
local B = 2

---@alias myLiteralAliases `A`|`B`

---@type myLiteralAliases
--local <!x!> = "A"
]]

and if we want to disallow the value (only allowing the actual symbol):

TEST [[
local A = "Hello"

---@type `A`
local <!x!> = "Hello"
]]

TEST [[
local A = "Hello"

---@alias myLiteralAliases `A` | integer

---@type myLiteralAliases
local <!x!> = "Hello"
]]

TEST [[
local A = "Hello"
local B = "World"

---@alias myLiteralAliases `A`|`B`

---@type myLiteralAliases
local <!x!> = "Hello"
]]

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 test/diagnostics/assign-type-mismatch.lua 中的复现用例开始,并运行该诊断测试。跟踪类型检查如何处理反引号字面量,然后让列出的有效和无效赋值产生预期的诊断结果,包括字面量别名。

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

评估

技术栈
lua
领域
devtools
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 发到你的邮箱

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