LuaLS / LuaLS/lua-language-server

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

Offen
#2,732 0 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Lua
Sterne
4.4k
Forks
442
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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"
]]

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den Reproduktionsfällen in test/diagnostics/assign-type-mismatch.lua und führe diesen Diagnosetest aus. Verfolge, wie Backtick-Literale bei der Typprüfung behandelt werden, und sorge dann dafür, dass die aufgeführten gültigen und ungültigen Zuweisungen die erwarteten Diagnosen erzeugen, einschließlich Literal-Aliassen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
lua
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.