icsharpcode/CodeConverter

VB -> C#: incorrect handling of ref arguments in loop conditions

TymurGubayev opened this issue · 0 comments

An invocation with a ref argument in a loop condition isn't converted to a local function call, like it happens if the same invocation is in the if condition.

VB.Net input code

Imports System

Public Class ByRefLoopCondition
    Sub S()
        Dim i = 0
        If F(i) Then
            Console.WriteLine(i) : Console.WriteLine()
        End If
        While F(i)
            Console.WriteLine(i) : Console.WriteLine()
        End While
        Do Until F(i)
            Console.WriteLine(i) : Console.WriteLine()
        Loop
        Do While F(i)
            Console.WriteLine(i) : Console.WriteLine()
        Loop
        Console.WriteLine(i)
    End Sub

    Function F(ByRef i As Object) As Boolean
        i = i + 1
        Return i < 4
    End Function
End Class

Erroneous output

public void S()
{
    int i = 0;
    bool localF() { object argi = i; var ret = F(ref argi); i = Conversions.ToInteger(argi); return ret; }

    if (localF())
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    object argi = i;
    while (F(ref argi))
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    i = Conversions.ToInteger(argi);
    object argi1 = i;
    while (!F(ref argi1))
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    i = Conversions.ToInteger(argi1);
    object argi2 = i;
    while (F(ref argi2))
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    i = Conversions.ToInteger(argi2);
    Console.WriteLine(i);
}

public bool F(ref object i)
{
    i = Operators.AddObject(i, 1);
    return Conversions.ToBoolean(Operators.ConditionalCompareObjectLess(i, 4, false));
}

Expected output

public void S()
{
    int i = 0;
    bool localF() { object argi = i; var ret = F(ref argi); i = Conversions.ToInteger(argi); return ret; }

    if (localF())
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    while (localF())
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    while (!localF())
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    while (localF())
    {
        Console.WriteLine(i);
        Console.WriteLine();
    }
    Console.WriteLine(i);
}

public bool F(ref object i)
{
    i = Operators.AddObject(i, 1);
    return Conversions.ToBoolean(Operators.ConditionalCompareObjectLess(i, 4, false));
}

Details

  • Product in use: VS extension
  • Version in use: 625c207
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.