-
-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@overload
return union unexpected behaviour
#3078
Comments
I believe this is a duplicate of #2933 and #1583 Currently there is no support for narrowing based on return tuple type. In your example:
additional notes
function fua()
-> integer
2. table|nil (you can see from its hover)
if not funcs or #funcs == 1 then
return funcs
elseif not args then
-- remove the base function if it is annotated with only @overload
for i, n in ipairs(funcs) do
if vm.isFunctionWithOnlyOverloads(n) then
table.remove(funcs, i)
break
end
end
return funcs
end |
@tomlau10 First of all, thanks. Then I found another issue with that example, which may also be duplicated. ---@type string
local ab = ''
-- or
---@param abc string
---@return EA, [string]
---@overload fun(): 0
local function lua(abc) --- fun(string):EA|0, [string]
if abc == 0 then
return 0
else
return EA[ab] or EA.a, { "aa" }
end
end
-- or more
local function nua() -- fun():integer|string
if true then
return 0
else
return "not allowed"
end
end
What about it's
It's about false positives and false negatives. In my opinion, false positives is more correct, because it forces you to explicit think about why it is there. But all of that is not allowed. |
seems you are posting multiple issues in a single example 😂
function lua(abc: string | undefined) { // function lua(abc: string | undefined): 0 | ""
if (abc == 0) { // though this line will have warning:
// This comparison appears to be unintentional because the types 'string | undefined' and 'number' have no overlap. ts(2367)
return 0;
} else {
return "";
}
}
similarly, I also converted it into typescript and here is the result: function nua() { // function nua(): 0 | "not allowed"
if (true) {
return 0;
} else {
return "not allowed" // Unreachable code detected. ts(7027)
}
}
language server can only perform static analysis at its best, and it will not execute lua logic. Let me give a very extreme example: local function rua() -- luals: function rua() -> string|integer
local result = true
for i = 1, 100000 do
result = math.random(2) % 2 == 9
end
if result then
return 0
else
return ""
end
end
|
How are you using the lua-language-server?
NeoVim
Which OS are you using?
Windows
What is the issue affecting?
Type Checking, Diagnostics/Syntax Checking
Expected Behaviour
Type of
r1
=EA|0
Not warning: probably
nil
Actual Behaviour
Type of
r1
=integer|EA|0
Warning: probably
nil
Reproduction steps
test.lua
r1
Need check nil
at line 22Additional Notes
Maybe duplicate of #1456
Log File
Log file
The text was updated successfully, but these errors were encountered: