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

destructing assignment works wrong with for generator #449

Open
INoorik opened this issue Oct 21, 2023 · 1 comment
Open

destructing assignment works wrong with for generator #449

INoorik opened this issue Oct 21, 2023 · 1 comment

Comments

@INoorik
Copy link

INoorik commented Oct 21, 2023

Code like this

{a, b} = for i in *{2, 3} do i*i

print a, b

Compiles to

do
  local _accum_0 = { }
  local _len_0 = 1
  local _list_0 = {
    2,
    3
  }
  for _index_0 = 1, #_list_0 do
    local i = _list_0[_index_0]
    _accum_0[_len_0] = i * i
    _len_0 = _len_0 + 1
  end
  local a, b
  a, b = _accum_0[1], _accum_0[2]
end
return print(a, b)

There variables a and b are local, so execution result is nil nil instead of 4 9

@fwcd
Copy link

fwcd commented Dec 12, 2023

This bug affects list comprehensions too:

f = ->
  {x} = [x for x in *{42}]
  print x

f!

which emits

local f
f = function()
  do
    local _accum_0 = { }
    local _len_0 = 1
    local _list_0 = {
      42
    }
    for _index_0 = 1, #_list_0 do
      local x = _list_0[_index_0]
      _accum_0[_len_0] = x
      _len_0 = _len_0 + 1
    end
    local x
    x = _accum_0[1]
  end
  return print(x)
end
return f()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants