Roslyn produces wrong code for huge switch expression
See original GitHub issueVersion 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:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Simpler repro:
https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxAgrgOyQExAagB8ABAJgEYBYAKGIGYACMhgYQYG8aHunHjyAbAwCWmGAwCyABgAUASi492PRTx7EA7AxnkkDXfr0HjRo3IZQA7sJgBjABaq1HJ8547Thrye/mAvAB8hq5uDAD6DIEMALS6IdwAvgDcTglOTmSkLtShTOQAnDLS8nIpOTxp1AlAA===
Fixed by #48028. Thank you for reporting.