LuaLS / LuaLS/lua-language-server
overload type narrowing for returns
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Lua
- Estrellas
- 4.4k
- Forks
- 442
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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?
Type Checking, Completion
Expected Behaviour
I am not sure this is a duplicate issue considering how common it probably is, but just to make sure.
Possibly a duplicate of https://github.com/LuaLS/lua-language-server/issues/1583.
This is similar to https://github.com/LuaLS/lua-language-server/issues/1146, but with returns instead of params.
It is a common pattern in Lua that on errors you might instead return a nil value to imply failure, along side an error message.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3)) -- fail type here is expected to be "string"
end
Actual Behaviour
You get a warning on fail that it might possibly be nil. Because its type is string|nil.
---@return table
---@overload fun(): nil, string
local function fetchX() end
local resource, fail = fetchX()
if not resource then
print(fail:sub(1, 3) )-- fail type here is actually "string|nil"
end
Reproduction steps
- Copy the setup code
- follow along with the comments
Additional Notes
I believe narrowing down the types in this case is still possible? It seems to me like https://github.com/LuaLS/lua-language-server/issues/1583 was discussing the possibility of adding tuples, but shouldn't this be doable even without tuples support?
Log File
No response
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo de retorno de sobrecarga del issue e inspecciona el comportamiento del language server en la comprobación de tipos y la finalización para el estrechamiento condicional. El payload no menciona archivos ni pruebas del repositorio, así que localiza los puntos de entrada relevantes para el análisis de sobrecargas y tipos de retorno antes de hacer cambios. Se considera terminado cuando fail se estrecha a string después de comprobar que resource es nil, sin la advertencia existente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- lua
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100