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 indexes incorrect #330

Closed
mrmonday opened this issue Jul 18, 2019 · 1 comment

Comments

@mrmonday
Copy link
Contributor

commented Jul 18, 2019

Input code

Friend Class Program
    Public Shared Sub Main(ByVal args As String())
        For idx = 0 To 10
        Next

        For idx = 0 To 10
        Next
    End Sub
End Class

IL:

	.entrypoint
	.locals init (
		[0] int32,
		[1] int32
	)

	IL_0000: nop
	IL_0001: ldc.i4.0
	IL_0002: stloc.0
	// loop start (head: IL_0003)
		IL_0003: ldloc.0
		IL_0004: ldc.i4.1
		IL_0005: add.ovf
		IL_0006: stloc.0
		IL_0007: ldloc.0
		IL_0008: ldc.i4.s 10
		IL_000a: ble.s IL_0003
	// end loop
	IL_000c: ldc.i4.0
	IL_000d: stloc.1
	// loop start (head: IL_000e)
		IL_000e: ldloc.1
		IL_000f: ldc.i4.1
		IL_0010: add.ovf
		IL_0011: stloc.1
		IL_0012: ldloc.1
		IL_0013: ldc.i4.s 10
		IL_0015: ble.s IL_000e
	// end loop
	IL_0017: ret

Erroneous output

namespace ConsoleApp4
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            for (var idx = 0; idx <= 10; idx++)
            {
            }

            for (idx = 0; idx <= 10; idx++)
            {
            }
        }
    }
}

IL:

	.locals init (
		[0] int32,
		[1] bool,
		[2] int32,
		[3] bool
	)

	IL_0000: nop
	IL_0001: ldc.i4.0
	IL_0002: stloc.0
	IL_0003: br.s IL_000b
	// loop start (head: IL_000b)
		IL_0005: nop
		IL_0006: nop
		IL_0007: ldloc.0
		IL_0008: ldc.i4.1
		IL_0009: add
		IL_000a: stloc.0

		IL_000b: ldloc.0
		IL_000c: ldc.i4.s 10
		IL_000e: cgt
		IL_0010: ldc.i4.0
		IL_0011: ceq
		IL_0013: stloc.1
		IL_0014: ldloc.1
		IL_0015: brtrue.s IL_0005
	// end loop

	IL_0017: ldc.i4.0
	IL_0018: stloc.2
	IL_0019: br.s IL_0021
	// loop start (head: IL_0021)
		IL_001b: nop
		IL_001c: nop
		IL_001d: ldloc.2
		IL_001e: ldc.i4.1
		IL_001f: add
		IL_0020: stloc.2

		IL_0021: ldloc.2
		IL_0022: ldc.i4.s 10
		IL_0024: cgt
		IL_0026: ldc.i4.0
		IL_0027: ceq
		IL_0029: stloc.3
		IL_002a: ldloc.3
		IL_002b: brtrue.s IL_001b
	// end loop

	IL_002d: ret

Gives error:

error CS0103: The name 'idx' does not exist in the current context

Expected output

namespace ConsoleApp4
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            for (var idx = 0; idx <= 10; idx++)
            {
            }

            for (var idx = 0; idx <= 10; idx++)
            {
            }
        }
    }
}

Details

Product in use: VS extension

Version in use: 6.9.0.0

I think there's a similar issue if you add a Dim idx = 0 at the top of the method, I haven't investigated this case though.

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

commented Jul 20, 2019

I think a long time ago I added some code to try to cater for behaviour observed with Option Explicit Off. I don't think I realized it was that option causing the effects so the code doesn't actually check that property though. Since we're in a much later phase of development that hack should probably be removed before separately deciding whether to properly tackle Option Explicit handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.