The Incorrect Argument Number is Shown When string:method() Errrors

Issue Type: Other
Impact: Low
Frequency: Constantly
Date First Experienced:
Date Last Experienced:

Reproduction Steps:

  1. In a script or the command line, write the following code or any variation of it:
local str = "a random string"
print(str:split({}))
  1. Run the code.
  2. View the output; for the above example, it errors stating:

invalid argument #2 to 'split' (string expected, got table)

Expected Behavior:
I expect the str:split example to error pointing to the first argument instead of the second since that is place of an incorrectly stated argument.

Actual Behavior:
There are two ways of using string functions, using string.method(str, ...) or str:method(...). In the latter case, whenever any argument-based error occurs e.g. type mismatch, missing a required argument, etc., the output window displays the incorrect argument number. It seems as if it is internally using string.method(str), so the displayed argument in the error is 1 more than it should be:

local str = "hello how is you?"
print(str:gsub({}, 'are'))

The error states:

invalid argument #2 to 'gsub' (string expected, got table)

This should say argument #1.

Note: This happens with all string methods, although I’ve only tested with a few, it’d make sense since the alternative syntax includes every library function.

Note2: This works as intended in mainline Lua 5.4, just tested it out.

Workaround:

1 Like

string.split() doesn’t take a table as it’s parameters. All it needs is the string that will be split, and where to split it

local stringToSplit = "Hello World!" --This is the string that will be split
local splitBy = " " --This is where string.split() will split the string from
local split = string.split(stringToSplit, splitBy) --Saves the splitted string to a variable(returns a table) 

for i,v in ipairs(split) do --Looping through the table
   print(v) --Printing each value
end

--[[ Output:
Hello
World!
--]]

That is not the point of the post, I’m aware it doesn’t take a table, but it had to be done to trigger an error (which that had an issue).

I think the title itself is clear enough, but the post is a definite confirmation.

1 Like

oh wait i thought this was in the scripting support catagory my bad xd

This is mostly intentional. If you look at the call stacks Luau produces, you will notice that we don’t say “method” or “upvalue” on individual entries, we just say “function”. This is related in that to have a clear separation between debug information and execution flow, we now no longer track whether a given invocation is through a method or not - str:split({}) is equivalent to string.split(str, {}) and as such the split call always assumes argument locations of unadorned call.

Lua 5.x uses bytecode tracing when generating errors to try to understand whether the original source contained a method call or a pure function call, but it’s too fragile and expensive for us so we don’t use that.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.