yanghuan / CSharp.lua

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

Braceless using makes method return nil #486

Closed joelverhagen closed 10 months ago

joelverhagen commented 10 months ago

Repro:

internal class Program
{
    private static void Main()
    {
        Console.WriteLine(GetValue() ?? "null!");
    }
    private static string GetValue()
    {
        using var x = new DisposableList();
        return x.GetValue();
    }

    private class DisposableList : IDisposable
    {
        public string GetValue()
        {
            return "foo";
        }

        public void Dispose()
        {
        }
    }
}

Excepted output: foo Actual output: null!

Generated Lua:


-- Generated by CSharp.lua Compiler
do
local System = System
System.namespace("", function (namespace)
  namespace.class("Program", function (namespace)
    local Main, GetValue, class
    namespace.class("DisposableList", function (namespace)
      local GetValue, Dispose
      GetValue = function (this)
        return "foo"
      end
      Dispose = function (this)
      end
      return {
        base = function (out)
          return {
            System.IDisposable
          }
        end,
        GetValue = GetValue,
        Dispose = Dispose
      }
    end)
    Main = function ()
      System.Console.WriteLine(GetValue() or "null!")
    end
    GetValue = function ()
      -- the problem is here, no return outside
      System.using(class.DisposableList(), function (x)
        return true, x:GetValue()
      end)
    end
    class = {
      Main = Main
    }
    return class
  end)
end)

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

Program.Main()

A workaround is to include the braces like this:

        using (var x = new DisposableList())
        {
            return x.GetValue();
        }