Incorrect Type-checking Error for String Functions on Indexed Value from Table

While using the Script Editor, I get this type error, of which the judgment in the soon-to-be-provided context is incorrect:

image

Using this code, you can see how the behavior of the typechecker is faulty, per the comments next to each line:

local tbl = {pcall(function() return "abc" end)} -- tbl becomes {true, "abc"} from the pcall output, making tbl[2] a string; if the function errors, the 2nd arg is still a string
print(string.find(tbl[2], "c")) -- squiggly line under tbl[2] for supposed incorrect type

I presume this happens because the first argument of pcall is a boolean, and the type-checker never weighs whether pcall returns more than 1 argument, although I could be wrong. However, the second argument (what is being indexed from tbl) will always return a string, no matter if the function errors or not.

Here is another code snippet along with its type-check error, a slight variation of the one above:

image

In this example, the type error is only partially correct. Since pcall will not return 2 arguments (as there is another value defined after pcall), that means the value indexed from tbl is an incorrect type for the string function. However, it still includes that it is being given (potentially) a boolean, which is never going to be the case.

Expected behavior

I expect that no type-check error occur when the second value inside of the table is used as an argument in a string function, since the second argument of pcall will always be a string.

2 Likes

Additionally, it actually appears to be reversed:
image

1 Like

I presume this happens because the first argument of pcall is a boolean, and the type-checker never weighs whether pcall returns more than 1 argument, although I could be wrong.

There’s a bug here which is that Script Analysis doesn’t recognize that a function call being the last element of an array like table means that all returned are included, e.g.:

local tbl = { pcall(function() return "abc" end) } 
for k, v in tbl do
    print(k, v) -- prints `1, true`, then `2, abc`
end

… however, Luau does not support inferring the exact shape of an array like table, e.g.:

-- This is inferred as `{ number | string | boolean }`
local tbl = { 1, "foo", true }

… Luau as-is should at least give a consistent answer here.

This is not correct, error takes any arbitrary value, not just strings. They can be received by pcall just the same.

Case 1: The function inside pcall does not error. The function then returns “abc” as second argument.
Case 2: The function inside pcall errors. Pcall returns a second argument, a string, that represents what the error was.
Perhaps you misinterpreted what I said?

I was saying in general. Errors do not have to be strings. The second return value of pcall can be anything if an exception is raised.

local s, e = pcall(function()
    error({})
end)

print(typeof(e)) --> "table"

This is the same reason why the e is inferred as unknown and not string? as well.

(Yes this is extremely "erm akshually :index_pointing_up: :nerd_face: ")

Oh yeah, I concur. But even if in such a case, the type shouldn’t be inferred as a boolean. I guess manually calling error will return something that is not a string, but in the scenario the function itself errors without the function itself invoking it, the error should be a string.

The main point anyway is that (my message sent early so this is an edit) typechecking should not incorrectly assume the last argument in a function that returns numerous args inside of a table the first arg returned by the function. That was a bit of word salad but I’m sure you can understand. Anyway, thanks for the error input, forgot that exists lmfao

1 Like