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

switch case without default no longer compiles in visual studio 2019 [c#] #35011

Closed
vsfeedback opened this issue Apr 16, 2019 · 3 comments
Closed
Assignees
Labels
Area-Compilers Bug Developer Community The issue was originally reported on https://developercommunity.visualstudio.com

Comments

@vsfeedback
Copy link

VSF_TYPE_MARKDOWNThe following code compiles fine in Visual Studio 2017, but not in Visual Studio 2019. Is there a default compiler option that has changed which can resolve this?

using System;

namespace NoDefaultCase
{
    public static class Program
    {
        public const string Database = "MongoDB";

        public static string GetDb()
        {
            switch (Database)
            {
                case "MongoDB":
                    return Database;
            }
        }

        public static void Main(string[] args)
        {
            Console.WriteLine($"The value returned is \"{GetDb()}\".");
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
    }
}

There is also a Stack Overflow ticket for this item:
https://stackoverflow.com/questions/55541269/why-does-a-switch-case-statement-on-a-string-constant-require-a-default-in-visua

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/520522/switch-case-without-default-no-longer-compiles-in.html
VSTS ticketId: 841662

These are the original issue comments:

Martin Eyles on 4/6/2019, 05:37 AM (10 days ago):

In Visual Studio 2017, the output of the build is:

1>------ Rebuild All started: Project: NoDefaultCase, Configuration: Debug Any CPU ------
1> NoDefaultCase -> C:\Users\MartinEyles\source\repos\NoDefaultCase\NoDefaultCase\bin\Debug\NoDefaultCase.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

In Visual Studio 2019, the output of the build is:

1>------ Rebuild All started: Project: NoDefaultCase, Configuration: Debug Any CPU ------
1>C:\Users\MartinEyles\source\repos\NoDefaultCase\NoDefaultCase\Program.cs(9,30,9,35): error CS0161: 'Program.GetDb()': not all code paths return a value
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Visual Studio Feedback System on 4/8/2019, 02:55 AM (8 days ago):

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Martin Eyles on 4/8/2019, 03:07 AM (8 days ago):

A github repository containing the example solution can be found at https://github.com/martineyles/NoDefaultCase This includes an archive of the example solution in the state before it was added to github (NoDefaultCase - Before Github.zip).

Martin Eyles on 4/8/2019, 05:59 AM (8 days ago):

I experience this issue in

Microsoft Visual Studio Enterprise 2019
Version 16.0.0
VisualStudio.16.Release/16.0.0+28729.10
Microsoft .NET Framework Version 4.7.03056'

It has been suggested by others in the Stack Overflow community that a preview version of Visual Studio 2019 behaves the same way as Visual Studio 2017

Martin Eyles on 4/12/2019, 05:24 AM (4 days ago):

The issue is still present in:

Microsoft Visual Studio Enterprise 2019
Version 16.0.1
VisualStudio.16.Release/16.0.1+28803.156
Microsoft .NET Framework Version 4.7.03056

Martin Eyles on 4/12/2019, 05:26 AM (4 days ago):

Stack overflow users has suggested that this does not meet the C# specification, and is likely to be either a bug or a change in the specification that hasn't yet been documented.

Martin Eyles on 4/12/2019, 05:29 AM (4 days ago):

To quote a user with very good reputation on stack overflow:

The ECMA C# 5 standard section 13.8.3 describes the reachability of the end of a switch statement:

The end point of a switch statement is reachable if at least one of the following is true:

  • The switch statement contains a reachable break statement that exits the switch statement.
  • The switch statement is reachable, the switch expression is a non-constant value, and no default label is present.
  • The switch statement is reachable, the switch expression is a constant value that doesn’t match any case label, and no default label is present.

None of these seem to be the case in your example:

  • There are no break statements
  • The switch expression is a constant value
  • The constant value does match a case label

So with C# 5 rules, the end point of this switch statement is not reachable, and it should compile with no problems. The draft specification in GitHub has the same text, so it doesn't look like it's changed there yet...

These are the original issue solutions:
(no solutions)

@agocke
Copy link
Member

agocke commented Apr 16, 2019

The description at the end is correct -- the switch is constant, so the end of the switch should not be reachable. This was an inadvertent break.

@agocke agocke self-assigned this Apr 16, 2019
agocke added a commit to agocke/roslyn that referenced this issue Apr 17, 2019
We made a change in dotnet#32818 to restore compatibility with C# 7.x
in rejecting certain switch statements even when we could prove
completeness with the new switch analysis. It looks like that
change just went a little too far, as we could do the analysis
if the argument to the switch expression was a constant. This
change removes the compat clause for switches with constant
values

Fixes dotnet#35011
@agocke
Copy link
Member

agocke commented Apr 22, 2019

This has been fixed and should appear in VS updates as they come out.

@SammyROCK
Copy link

Is there any actual application for this?

@sharwell sharwell added the Developer Community The issue was originally reported on https://developercommunity.visualstudio.com label Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Developer Community The issue was originally reported on https://developercommunity.visualstudio.com
Projects
None yet
Development

No branches or pull requests

4 participants