LuaLS / LuaLS/lua-language-server

Widening cast has no effect

Aberta
#2,215 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

bug
Linguagem predominante
Lua
Estrelas
4.4k
Forks
442
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece executando as três reproduções em Lua da issue e compare as informações de hover com os diagnósticos de atribuição. Rastreie o tratamento de casts e o caminho subsequente de verificação de tipos para variáveis locais e, em seguida, adicione ou atualize a cobertura para que casts de widening afetem as verificações de atribuição sem introduzir erros contraditórios.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
lua
Domínio
devtools
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.