Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TymurGubayev opened this issue Aug 28, 2024 · 0 comments
Open

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

TymurGubayev opened this issue Aug 28, 2024 · 0 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@TymurGubayev
Copy link
Contributor

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.
@TymurGubayev TymurGubayev added the VB -> C# Specific to VB -> C# conversion label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

1 participant