LuaLS / LuaLS/lua-language-server

Widening cast has no effect

Offen
#2,215 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
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?

Linux

What is the issue affecting?

Annotations, Type Checking, Hover

Expected Behaviour

Cast unconditionally overwrites the variable's type with the new type.

Actual Behaviour

Cast is ignored and in some cases causes additional type check errors.

Reproduction steps
---@param str string
local function foo(str)
    ---@cast str +?
    if str == '' then str = nil end
end

Note how it produces an error on assignment:

This variable is defined as type `string`. Cannot convert its type to `nil`.
- `nil` cannot match `string`
- Type `nil` cannot match `string` Lua Diagnostics.(cast-local-type)

Note also that the tooltip for str in the comparison says correctly that it is a string?. But this has no observable effect on the actual type.

Example two (attempted workaround):

---@param str string
local function foo(str)
    ---@cast str string?
    if str == '' then str = nil end
end

Note how the cast causes a type check error now, which makes no sense.

Example three (from the wiki on cast, modified to actually verify that the cast worked):

---@type integer
local x

---@cast x +boolean
x = false

Note how it produces an error, similarly to the first example. You would expect at least the example from the official documentation itself to work, but it doesn't. This leads me to conclude this is a bug.

Additional Notes

No response

Log File

No response

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 damit, die drei Lua-Reproduktionen im Issue auszuführen, und vergleiche die Hover-Informationen mit den Zuweisungsdiagnosen. Verfolge die Verarbeitung von Casts und den anschließenden Type-Checking-Pfad für lokale Variablen. Füge dann Tests hinzu oder aktualisiere sie, sodass Widening-Casts die Zuweisungsprüfungen beeinflussen, ohne widersprüchliche Fehler einzuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
lua
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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