ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Switch Expression Decompilation Bug with Docs.msft Sample

Open christophwille opened this issue 4 years ago • 5 comments

Sample in question: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (basic example)

Output in ILSpy (latest master):

switchexprfunkydecomp

Seems the default case that is compiler-generated makes ILSpy stumble on pattern detection.

christophwille avatar Aug 25 '20 08:08 christophwille

I'd argue this is a bug in roslyn, WTF is it doing with those if (1 == 0) checks? Do we now need a de-obfuscation step for regular C# decompilation?

dgrunwald avatar Aug 25 '20 08:08 dgrunwald

I built the sample using VS Preview and the .NET 5 tooling. Code & binaries attached (might differ to RTM Roslyn).

SwitchExprSample.zip

christophwille avatar Aug 25 '20 08:08 christophwille

The thing is: switch expressions missing the _ => case have a default: throw new SwitchExpressionException(...); implicitly added. We should remove these.

About the if (1 == 0)... no idea... we have seen similar blocks while implementing the async enumerator patterns, iirc.

siegfriedpammer avatar Aug 25 '20 08:08 siegfriedpammer

I don't think we should remove the throw new SwitchExpressionException; I'd like to keep that explicit. But it's perfectly fine to have a throw expression in a switch expression (and this is already implemented); the only problem here is the if (1 == 0).

dgrunwald avatar Aug 25 '20 09:08 dgrunwald

I just discovered this aswell and found out this only occurs in Assemblies compiled with debug-Configuration. Thought I'd share my observation. I'd guess it's something like a Placeholder for debug purposes?

Herrmel avatar Jun 25 '21 09:06 Herrmel