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:
- Create an enum that takes a generic type parameter
- Create at least one case that has an associated value of that generic type
- Create an instance of that case in the enum
- 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 notInt64
-
!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