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#: Duplicate cases should be discarded #374

Open
mrmonday opened this issue Sep 6, 2019 · 1 comment

Comments

@mrmonday
Copy link
Contributor

commented Sep 6, 2019

VB allows Case in a Select Case to have duplicate labels.

Input code

Imports System

Friend Module Module1
    Sub Main()
        Select Case 1
            Case 1
                Console.WriteLine("a")

            Case 1
                Console.WriteLine("b")

        End Select
        Console.ReadLine()
    End Sub
End Module

This compiles, and outputs a.

Erroneous output

using System;

namespace ConsoleApp3
{
    internal static class Module1
    {
        public static void Main()
        {
            switch (1)
            {
                case 1:
                    {
                        Console.WriteLine("a");
                        break;
                    }

                case 1:
                    {
                        Console.WriteLine("b");
                        break;
                    }
            }
            Console.ReadLine();
        }
    }
}

Expected output

using System;

namespace ConsoleApp3
{
    internal static class Module1
    {
        public static void Main()
        {
            switch (1)
            {
                case 1:
                    {
                        Console.WriteLine("a");
                        break;
                    }
            }
            Console.ReadLine();
        }
    }
}

Details

Error:

error CS0152: The switch statement contains multiple cases with the label value '1'

I wonder if it's worth leaving the code there, but commented. Or even leaving the error - the behaviour changes, since it won't compile, but I can't think of a case where this wouldn't be a bug.

Version: master @ 3ffc597

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

commented Sep 10, 2019

Thanks for figuring out the actual behaviour here. I've just found the relevant bit in the docs too.

I've updated the contributing doc to explain how I try to weigh up decisions on what the output should be in these complicated cases.

So from those, if we are confident we can generate compiling code that behaves the same, we should do so. So let's not leave a compile error (or insert an error pragma).

The next applicable point is "code that resembles the input code". This suggests we should try to keep the information that was in the VB source. I like your suggestion of keeping it in a comment.

Having put it as a comment, we might confuse the user, and in a sense we've generated non-idiomatic code. So the comment should explain where it comes from, or be accompanied by a warning pragma. The ideal is to make what we do clear enough that someone wouldn't feel the need to look back at the VB to be sure they could safely delete the code.

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.