Skip to content

Invalid decompilation of switch expression with no default case #3382

@mmusu3

Description

@mmusu3

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDecompilerThe decompiler engine itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions