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

Invalid decompilation of switch expression with no default case #3382

Open
mmusu3 opened this issue Jan 30, 2025 · 0 comments
Open

Invalid decompilation of switch expression with no default case #3382

mmusu3 opened this issue Jan 30, 2025 · 0 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@mmusu3
Copy link

mmusu3 commented Jan 30, 2025

Input code

using System;
class IncompleteSwitchExpressionTest
{
    static int Test(StringComparison c)
    {
        return c switch {
            StringComparison.Ordinal => 0,
            StringComparison.OrdinalIgnoreCase => 1,
        };
    }
}

Erroneous output

internal class IncompleteSwitchExpressionTest
{
	private static int Test(StringComparison c)
	{
		switch (c)
		{
		case StringComparison.Ordinal:
			return 0;
		case StringComparison.OrdinalIgnoreCase:
			return 1;
		default:
		{
			global::<PrivateImplementationDetails>.ThrowSwitchExpressionException(c);

			int result = default(int);

			return result;
		}
		}
	}
}

When a switch expression does not specify a default case the compiler will add one that throws a SwitchExpressionException or a InvalidOperationException on older runtimes.
The decompiled code cannot recompile as <PrivateImplementationDetails> is not a valid identifier.

Details

  • Product in use: ILSpy 9.0 VS extension
@mmusu3 mmusu3 added Bug Decompiler The decompiler engine itself labels Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

1 participant