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#: for loop with Int16 index needs explicit cast in generated code declaring loopTo variable #607

Closed
jamesmanning opened this issue Aug 14, 2020 · 2 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@jamesmanning
Copy link

Input code

    Sub DummyMethod()
        Dim someArray = New Integer() { 1, 2, 3}
        For index As Int16 = 0 To someArray.Length - 1
            Console.WriteLine(index)
        Next
    End Sub

Erroneous output

        public void DummyMethod()
        {
            var someArray = new int[] { 1, 2, 3 };
            for (short index = 0, loopTo = someArray.Length - 1; index <= Conversions.ToShort(loopTo); index++)
                Console.WriteLine(index);
        }

Expected output

Assuming loopTo is correctly being declared a short, we can/should drop the Conversions.ToShort in the check clause and move it to the assignment clause AFAICT? Removing it from the check clause is optional, the compiler error is due to trying to implicitly assign the 32-bit integer of someArray.Length - 1 to the 16-bit short loopTo

        public void DummyMethod()
        {
            var someArray = new int[] { 1, 2, 3 };
            for (short index = 0, loopTo = Conversions.ToShort(someArray.Length - 1); index <= loopTo; index++)
                Console.WriteLine(index);
        }

Details

  • VS 2019 Community with Code Converter extension 8.1.6.0
@jamesmanning jamesmanning added the VB -> C# Specific to VB -> C# conversion label Aug 14, 2020
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Aug 15, 2020

This was fixed to match your expected output for #602, sorry it didn't get released in time for your conversion. I'll try to get it released this weekend.

I hadn't carefully checked this case, but I've now checked the decompilation eagerly coerces the loopTo variable to the control variable type (I was worried about cases where someArray.Length - 1 > short.MaxValue.

There's one remaining subtle difference from the decompilation, which is that it appears VB uses unchecked addition. I'm going to add this as a note in known limitations #16

@jamesmanning
Copy link
Author

Excellent, thank you @GrahamTheCoder !

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

2 participants