Skip to content
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

LuaMultiReturn returns all values even when indexed with [0] #1411

Closed
Cheatoid opened this issue Feb 26, 2023 · 0 comments · Fixed by #1428
Closed

LuaMultiReturn returns all values even when indexed with [0] #1411

Cheatoid opened this issue Feb 26, 2023 · 0 comments · Fixed by #1428
Labels
bug scope: language extensions Related to language-extension types scope: transformation Transformation of TS to Lua

Comments

@Cheatoid
Copy link
Contributor

See Playground sample.

I expected that foo would have transpiled to (notice parentheses):

function foo(str)
    return (string.gsub(str, ".", "*"))
end

But, it clearly doesn't and therefore leads to multiple returns, because string.gsub also returns the amount of replacements as a secondary return value, which invalidates the specified return type : string of the foo... It works fine in case of bar and baz since they make use of a variable.

@Perryvw Perryvw added bug scope: transformation Transformation of TS to Lua scope: language extensions Related to language-extension types labels Feb 26, 2023
@Perryvw Perryvw changed the title A small bug related to LuaMultiReturn LuaMultiReturn returns all values even when indexed with [0] Feb 26, 2023
lolleko added a commit that referenced this issue Apr 10, 2023
We try to avoid using `select()` when we use `[0]` to access the first value of multiple returns (e.g. `multiCall()[0]`)
In that case, we used to simply omit the `select()`. Unfortunately, this only works if the value is assigned directly (`local x = multiCall()`). In cases where the value is not assigned, omitting the select and keeping the original expression (`multiCall()`) will lead to all return values being passed on.
(See #1411)

To fix this, we now wrap the multireturncall in parentheses. using a new Lua parentheses expression. Using parenthesis is the correct way to keep only the first value of multiple returns (see https://www.lua.org/manual/5.1/manual.html#2.5)

This fixes #1411
Perryvw pushed a commit that referenced this issue Apr 12, 2023
We try to avoid using `select()` when we use `[0]` to access the first value of multiple returns (e.g. `multiCall()[0]`)
In that case, we used to simply omit the `select()`. Unfortunately, this only works if the value is assigned directly (`local x = multiCall()`). In cases where the value is not assigned, omitting the select and keeping the original expression (`multiCall()`) will lead to all return values being passed on.
(See #1411)

To fix this, we now wrap the multireturncall in parentheses. using a new Lua parentheses expression. Using parenthesis is the correct way to keep only the first value of multiple returns (see https://www.lua.org/manual/5.1/manual.html#2.5)

This fixes #1411
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug scope: language extensions Related to language-extension types scope: transformation Transformation of TS to Lua
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants