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#: Descending loop broken #453

Closed
hex64dbg opened this issue Dec 6, 2019 · 2 comments
Closed

VB -> C#: Descending loop broken #453

hex64dbg opened this issue Dec 6, 2019 · 2 comments
Assignees
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@hex64dbg
Copy link

hex64dbg commented Dec 6, 2019

Input code

Sub PrintLoop(startIndex As Integer, endIndex As Integer, [step] As Integer)
  For i As Integer = startIndex To endIndex Step [step]
    Debug.WriteLine(i)
  Next
End Sub

'Ascending
PrintLoop(0, 2, 1) 'Prints 0, 1, 2
	
'Descending
PrintLoop(2, 0, -1) 'Prints 2, 1, 0

Erroneous output

public void PrintLoop(int startIndex, int endIndex, int step)
{
    for (int i = startIndex; i <= endIndex; i += step)
        Debug.WriteLine(i);
}

PrintLoop(0, 2, 1); // Prints 0, 1, 2
PrintLoop(2, 0, -1); // Prints nothing

I think this should work :

Expected output

public void PrintLoop(int startIndex, int endIndex, int step)
{
    for (int i = startIndex; (step >=0) ? i <= endIndex : i>=endIndex ; i += step)
        Debug.WriteLine(i);
}

EDIT 9 dec : I changed the expected output.

@hex64dbg hex64dbg added the VB -> C# Specific to VB -> C# conversion label Dec 6, 2019
@GrahamTheCoder
Copy link
Member

Thanks for raising this. Seems like a sensible solution. We can use semanticModel.GetConstantValue to check if the conditional can be optimised away at conversion time.

@GrahamTheCoder GrahamTheCoder modified the milestones: 8.1.0, 8.1.0 candidate Mar 20, 2020
@GrahamTheCoder GrahamTheCoder self-assigned this Apr 2, 2020
@Saibamen
Copy link
Contributor

Please retest with latest release

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

3 participants