gopher-lua icon indicating copy to clipboard operation
gopher-lua copied to clipboard

Wrong result of multi-assignment

Open AlexanderYastrebov opened this issue 2 years ago • 2 comments

You must post issues only here. Questions, ideas must be posted in discussions.

  • [ ] GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • [x] GopherLua is a Lua5.1 implementation. In Lua, to keep it simple, it is more important to remove functionalities rather than to add functionalities unlike other languages . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library.

Please answer the following before submitting your issue:

  1. What version of GopherLua are you using? : 2b3f02d9173010b12bb3ff2c0e758921accb4096
  2. What version of Go are you using? : go version go1.20.4 linux/amd64
  3. What operating system and processor architecture are you using? : -
  4. What did you do? :
-- x.lua
local a, b = 0, 0
a, b = a + 1, a + b
print(a, b)
$ lua -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
$ lua x.lua 
1       0
$ git describe --tags
v1.1.0-11-g2b3f02d
$ make glua
$ ./glua x.lua 
1       1
  1. What did you expect to see? :
1       0
  1. What did you see instead? :
1       1

This looks related to https://github.com/yuin/gopher-lua/issues/355 and https://github.com/yuin/gopher-lua/issues/315.

Discovered while debugging test suite for https://github.com/Egor-Skriptunoff/pure_lua_SHA

$ glua sha2_test.lua 
./sha2.lua:87: at least 53-bit floating point numbers are required
stack traceback:
        [G]: in function 'assert'
        ./sha2.lua:87: in function <./sha2.lua:0>
        [G]: in function 'require'
        sha2_test.lua:5: in main chunk
        [G]: ?

which fails due to incorrect multi-assignment calculation here https://github.com/Egor-Skriptunoff/pure_lua_SHA/blob/6adac177c16c3496899f69d220dfb20bc31c03df/sha2.lua#L75

AlexanderYastrebov avatar Jun 02 '23 16:06 AlexanderYastrebov

Funny, https://github.com/yuin/gopher-lua/issues/315#issuecomment-825057818 reports the same problem and comment is liked by @Egor-Skriptunoff who is the author https://github.com/Egor-Skriptunoff/pure_lua_SHA 🙈

AlexanderYastrebov avatar Jun 02 '23 17:06 AlexanderYastrebov

Bump. Hugely annoying issue.

A bog-standard non-recursive fib function that uses multi-assignment can be easily used to reproduce this bug:

function fib(n)
    if n <= 1 then
        return n
    end
    local a, b = 0, 1
    for i = 2, n do
        print("calc: [i: " .. i .. ", b: " .. b .. "]")
        a, b = b, a + b
    end
    return b
end

fib(35) -- produces 17179869184 instead of 9227465, a symptom of doubling the value every cycle

octalide avatar Jun 17 '24 09:06 octalide