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 · 2 comments
Open

Invalid decompilation of switch expression with no default case #3382

mmusu3 opened this issue Jan 30, 2025 · 2 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
siegfriedpammer added a commit that referenced this issue Feb 20, 2025
siegfriedpammer added a commit that referenced this issue Feb 20, 2025
@mmusu3
Copy link
Author

mmusu3 commented Feb 21, 2025

@siegfriedpammer I've tested the changes in #3404 and it works well but I've identified some other cases that are not handled.

Input

class IncompleteSwitchExpressionTest
{
    static void Test2(ref StringComparison? c)
    {
        c = c switch {
            null => StringComparison.Ordinal, // Problem also persists without this case
            StringComparison.Ordinal => StringComparison.OrdinalIgnoreCase,
            StringComparison.OrdinalIgnoreCase => StringComparison.InvariantCulture,
        };
    }

    interface I1 { }

    class C1 : I1 { }
    class C2 : I1 { }
    class C3 : I1 { }

    static int Test3(I1 i)
    {
        return i switch {
            C1 => 1,
            C2 => 2,
            C3 => 3,
        };
    }
}

Output

internal class IncompleteSwitchExpressionTest
{
	private static void Test2(ref StringComparison? c)
	{
		StringComparison? stringComparison = c;
		StringComparison value = default(StringComparison);

		switch (stringComparison)
		{
		case null:
			value = StringComparison.Ordinal;
			break;
		case StringComparison.Ordinal:
			value = StringComparison.OrdinalIgnoreCase;
			break;
		case StringComparison.OrdinalIgnoreCase:
			value = StringComparison.InvariantCulture;
			break;
		default:
			global::<PrivateImplementationDetails>.ThrowSwitchExpressionException(stringComparison);
			break;
		}

		c = value;
	}

	private interface I1 { }
	private class C1 : I1 { }
	private class C2 : I1 { }
	private class C3 : I1 { }

	private static int Test3(I1 i)
	{
		if (!(i is C1))
		{
			if (!(i is C2))
			{
				if (i is C3)
					return 3;

				global::<PrivateImplementationDetails>.ThrowSwitchExpressionException(i);

				int result = default(int);

				return result;
			}

			return 2;
		}

		return 1;
	}
}

@siegfriedpammer
Copy link
Member

Thanks for providing further test cases. Please note that your Test3 case uses pattern matching with switch, which is currently not supported at all.

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

2 participants