question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Roslyn produces wrong code for huge switch expression

See original GitHub issue

Version Used: .NET Core SDK 3.1.302 .NET 5 RC1

Steps to Reproduce:

Use the following source code:

using System;
public class C {
    public static int M0(int x)
    {
        var b1 = false;
        var b2 = false;
        var b3 = false;
        var b4 = false;
        var b5 = false;
        var b6 = false;
        var b7 = true; // but, if you remove these two variables b7, b8
        var b8 = true; // then everything is ok
        
        return (x, b1, b2, b3, b4, b5, b6, b7, b8) switch
        {
            (1, false, false, false, false, false, false, true, true) => 1,
            _ => -1,
        };
    }
    
    public static void Main() {
        Console.WriteLine(M0(1));
    }
}

It will produce:

using System;
public class C
{
    public static int M0(int x)
    {
        bool flag = false;
        bool flag2 = false;
        bool flag3 = false;
        bool flag4 = false;
        bool flag5 = false;
        bool flag6 = false;
        bool flag7 = true;
        bool flag8 = true;
        bool flag9 = default(bool);   // these vars are not present in source code
        bool flag10 = default(bool);  // they are used instead of the "real" ones
        if (x == 1 && !flag && !flag2 && !flag3 && !flag4 && !flag5 && !flag6 && flag9 && flag10)
        {
            return 1;
        }
        return -1;
    }

    public static void Main()
    {
        Console.WriteLine(M0(1));
    }
}

Expected Behavior: Provided sample should print “1”.

Actual Behavior: Provided sample will print “-1” (wrong code is generated).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

CS8509 The switch expression does not handle all ...
CS8509 The switch expression does not handle all possible inputs when it does #38571.
Read more >
Can I force a non-exhaustive c# switch expression to cause ...
Yes you can. This case raises the warning CS8509, as you can see here. To turn this into an error, add the following...
Read more >
Adding Matt operator to Roslyn - Syntax, Lexer and Parser
ScanSyntaxToken is basically a huge switch statement reading charactes from the provided text input (source code) and trying to match them into ...
Read more >
pattern matching expressions using the switch keyword
The compiler generates an error when a lower switch expression arm can't be chosen because a higher switch expression arm matches all its ......
Read more >
Pattern Matching for switch Expressions and Statements
A switch statement transfers control to one of several statements or expressions, depending on the value of its selector expression. In earlier releases ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found