Skip to content

Cannot use destructuring assignment with multiple return values #304

Description

@geomaster

When a function returns a table, MoonScript allows us to use a destructuring assignment to assign values from the table directly to variables in our scope.

However, when we mix a destructuring assignment with assigning from additional return values from the function, the compiled code behaves as if those additional return values are all nil.

Here's a minimal reproduction scenario to clarify:

fun = () ->
    my_table = { a: 1, b: 2 }
    return my_table, 2

{ a: my_a, b: my_b }, my_c = fun!

This generates the following Lua code:

local fun
fun = function()
  local my_table = {
    a = 1,
    b = 2
  }
  return my_table, 2
end
local my_a, my_b
do
  local _obj_0 = fun()
  my_a, my_b = _obj_0.a, _obj_0.b
end
local my_c = nil

When the proper output should be along the lines of:

local fun
fun = function()
  local my_table = {
    a = 1,
    b = 2
  }
  return my_table, 2
end
local my_a, my_b, my_c
do
  local _obj_0, my_c = fun()
  my_a, my_b = _obj_0.a, _obj_0.b
end

I'm seeing this issue with the latest MoonScript release, 0.5.0-1.

This doesn't sound like a terribly difficult fix, so if no one starts working on it soon, I'll probably be able to fix it and contribute a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions