Skip to content

Incorrect order of operations with increment and array access #473

Description

@joelverhagen

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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