Skip to content

Issue #440 : Fix multiple assignment overwriting values it still needs - #560

Open
phinze wants to merge 1 commit into
yuin:masterfrom
phinze:issue-440-multi-assign
Open

phinze wants to merge 1 commit into
yuin:masterfrom
phinze:issue-440-multi-assign

Conversation

@phinze

@phinze phinze commented Nov 15, 2026

Copy link
Copy Markdown

Fixes #440 .

I ran into this in Rex, a terminal app whose Lua config runs on gopher-lua v1.1.2: a helper in my config doing url, closes = url:sub(1, -2), closes - 1 crashed with a nil pointer dereference. Tracking that down led here.

Changes proposed in this pull request:

  • With more than one target, a local target's value is compiled into a temporary register and moved into place at the end, instead of straight into the local. Compiling straight into the local is what made a, b = b, a give 1, 1, and made u, c = u:sub(1, -2), c - 1 crash with a nil pointer dereference: the method lookup overwrote u before the call read it.
  • With more than one target, a table field's value and the table itself stay in temporary registers instead of pointing at a local's register. Otherwise s.x, d = d, 7 stores the new d. Constants can still stand in for values (PropagateK), since nothing can overwrite them.
  • The final store loop only releases a temporary register for a table value that actually used one. It used to release one it never took, so g, t.x = 5, k set g to "x".
  • Re-enables the multiple-assignment block in _lua5.1-tests/attrib.lua, which passes now, and adds -- issue #440 cases to _glua-tests/issues.lua.

Every change applies only when there's more than one target, so single-target assignments compile as before.

How I checked it:

  • make build leaves the generated files unchanged, and go test ./... passes. The new tests fail without the compiler change.
  • I ran a differential fuzz: 9,000 random multiple assignments mixing locals, globals, upvalues, table fields, function calls, method calls, multi-value calls and and/or, each run under this branch and under PUC Lua 5.1.5, comparing every variable afterwards. All 9,000 match. Master differs from Lua 5.1.5 on 1,641 of them. Happy to share the fuzz script.

This touches the same line in compileAssignStmtLeft as #538. They should fit together: #538 changes the single-target path, and this only changes the multi-target one.

I worked this out with help from Claude Code. I've reviewed the change, and the checks above were run as described.

buyemune-sys pushed a commit to tarantool-contrib/go-moonscript that referenced this pull request Nov 27, 2026
…nd long jumps

Three bugs of the compiler, found by the fuzz test of the next commit
and reachable from source in both dialects:

- An assignment to a single local with more values than targets
  evaluated the first value into the local, before the others: in
  "i = 5, f(i)" f got the new i (lua5.1 and luajit pass the old one,
  1). The value now goes to a temporary, as for a multiple assignment
  (yuin/gopher-lua#560).
- A "..." value for a local's register, as in "x = (...)" or the
  case above with "i = ..., ...", was a VARARG into that register.
  The VM's VARARG sets the top of the stack after the values it loads
  and clears the registers above it, so the locals and temporaries
  above the target became Go nils: "local x, y = 1, 2; x = (...)"
  then reading y was a nil-pointer panic in the VM, and the
  compileExpr result was negative (-1 registers), so the next value
  was compiled over a live register. It is loaded into a free register
  and moved.
- A jump over more instructions than an instruction addresses was
  checked for forward jumps only: the backward jump of a repeat loop
  and a numeric for's FORPREP and FORLOOP over a body of more than
  131071 instructions jumped out of the code (index out of range in
  the VM). They are lua5.1's "control structure too long", now at
  the jump's line (it was "too long to jump." at the function's
  first line, line 0 for the main chunk). Under DialectLuaJIT the
  parser's model of LuaJIT's code generator finds them first in
  source. A function with more than 262143 closures (CLOSURE's Bx) is
  "constant table overflow", lua5.1's.

Tests: testdata/compat/compile_assign_extra.lua (lua5.1 ref),
TestCompileLongJumps.
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

Successfully merging this pull request may close these issues.

Wrong result of multi-assignment

1 participant