Generic enum case results in an invalid .NET assembly

IDE: Visual Studio 2019
Version: Elements Version 11.0.0.2653 (develop) built on bajor, 20210723-121308. Commit 05475c2.
Target (If relevant): .NET Core
Description: When using an enum with generic type parameters in a switch or if statement, the resulting assembly does not execute and throws a System.BadImageFormatException

Here’s about the shortest example i could get

enum E<T> {
	case v(T)
}

if case .v(let t) = E<Int>.v(4) {
	print(t)
}

Expected Behavior: The code compiles, runs and prints the number 4

Actual Behavior: The code compiles, but doesn’t run because the resulting assembly is invalid

Steps:

  1. Create an enum that takes a generic type parameter
  2. Create at least one case that has an associated value of that generic type
  3. Create an instance of that case in the enum
  4. Try using it in a switch or if statement

Observations:

  • The resulting IL code wants the associated value of the enum to have the type !0, and not Int64
  • !0 is not a valid type name as far as i know
  • If you switch on only an enum case that has no associated values, the code will run. The resulting IL code does not use the type !0

The entrypoint decompiles to the following C# using ILSpy:

    public unsafe static int Main(string[] __args)
    {
        Swift.__Global.$$setArgV(__args);
        E<long> e = E<long>.v(4L);
        !0 t = (!0)((E<>*)(&e))->__ValueForv();
        int num;
        if (e.__Value == 0)
        {
            t = (!0)((E<>*)(&e))->__ValueForv();
            num = 1;
        }
        else
        {
            num = 0;
        }
        if (num != 0)
        {
            object[] objects = new object[1] { t };
            Swift.__Global.print____separator__terminator(objects);
        }
        int Result = default(int);
        return Result;
    }

If you use the enum in a switch statement, ILSpy’s decompiled code contains errors

        //IL_004f: Expected O, but got I8
        //IL_005e: Expected I8, but got O

Logged as bugs://E25252.

bugs://E25252 was closed as fixed.

Version 11.0.0.2655 fixes the issue in if statements, but not in switch statements.

The following swift code still produces the same error:

enum E<T> {
    case v(T)
}

switch E<Int>.v(4) {
    case .v(let t):
        print(t)
}

ILSpy decompiles the entrypoint to the following C#

    public static int Main(string[] __args)
    {
        //IL_004f: Expected O, but got I8
        //IL_005e: Expected I8, but got O
        Swift.__Global.$$setArgV(__args);
        E<long> e = E<long>.v(4L);
        int _Value = E<long>.v(4L).__Value;
        !0 val = default(!0);
        long t = default(long);
        int num;
        if (e.__Value == 0)
        {
            val = (!0)e.__ValueForv();
            if (val is long)
            {
                t = (long)val;
                num = 1;
            }
            else
            {
                num = 0;
            }
        }
        else
        {
            num = 0;
        }
        if (num != 0)
        {
            object[] objects = new object[1] { t };
            Swift.__Global.print____separator__terminator(objects);
        }
        int Result = default(int);
        return Result;
    }

bugs://E25252 was reopened.

bugs://E25252 was closed as fixed.