yanghuan / CSharp.lua

The C# to Lua compiler
Other
1.23k stars 202 forks source link

Incorrect order of operations with increment and array access #473

Open joelverhagen opened 10 months ago

joelverhagen commented 10 months ago

Repro:

internal class Program
{
    private static void Main(string[] args)
    {
        var ids = new[] { 0, 1, 2, 3 };
        int j = 1;
        ids[j + 1] = ids[j--];
        Console.WriteLine(string.Join(" ", ids)); // should be 0 1 1 3
    }
}

Expected output 0 1 1 3 Actual output: 0 1 2 3

Generated Lua:


-- Generated by CSharp.lua Compiler
do
local System = System
local ArrayInt32 = System.Array(System.Int32)
System.namespace("", function (namespace)
  namespace.class("Program", function (namespace)
    local Main
    Main = function (args)
      local ids = ArrayInt32 { 0, 1, 2, 3 }
      local j = 1
      local default = j
      j = default - 1 -- this should go after the next line
      ids:set(j + 1, ids:get(default))
      System.Console.WriteLine(System.String.JoinEnumerable(" ", ids))
      -- should be 0 1 1 3
    end
    return {
      Main = Main
    }
  end)
end)

end
System.init({
  types = {
    "Program"
  }
})

Program.Main()

I found this via a bubble sort component of Quicksort in delaunator-sharp: https://github.com/nol1fe/delaunator-sharp/blob/4bb1d3d21218c621c26b87449d6d23f59dd47c0c/DelaunatorSharp/Delaunator.cs#L454