You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A=1B=2---@type`A`localfoo=A-- this should be fine---@type`A`localbar=B-- but this should give a type error---@type`A`localbaz=1-- ideally, so should this (because you didn't use the literal name "A")---@type`A`|stringlocalvar=A-- this should be fine
Actual Behaviour
A=1B=2---@type`A`localfoo=-- A is correctly the only autocomplete suggestion---@type`A`localbar=3-- but this does not give any type error---@parama`A`functionf(a) endlocalbaz=f("Hello") -- nor does this---@type`A`|stringlocalvar=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)]]
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 myLiteralAliaseslocal x = A---@type myLiteralAliaseslocal y = 3]]TEST[[local B = 2---@alias myLiteralAliases `B` | string---@type myLiteralAliaseslocal x = B---@type myLiteralAliaseslocal 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 = 1local 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 myLiteralAliaseslocal <!x!> = "Hello"]]TEST[[local A = "Hello"local B = "World"---@alias myLiteralAliases `A`|`B`---@type myLiteralAliaseslocal <!x!> = "Hello"]]
The text was updated successfully, but these errors were encountered:
mycroftjr
changed the title
Type checking does not work for `` (backtick) literals
Type checking does not work with `` (backtick) literals
Jun 29, 2024
mycroftjr
changed the title
Type checking does not work with `` (backtick) literals
Type checking does not work at all with `` (backtick) literals
Jun 30, 2024
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
Actual Behaviour
Reproduction steps
Additional Notes
This is important for things like
("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:
and if we want to disallow the value (only allowing the actual symbol):
The text was updated successfully, but these errors were encountered: