Skip to content

Commit

Permalink
Ensure GetCorElementType returns value type for enums on Mono runti…
Browse files Browse the repository at this point in the history
…me (#72685)

`Type.GetTypeCode` on Mono was calling `GetCorElementType`, which, when invoked on an enum nested in a generic class, would return `TypeCode.Object` instead of `TypeCode.Int32`. Matching to CoreClr, we change `GetCorElementType` to return `VALUETYPE` for enums to fix the issue. The original issue would also provoke Enum parsing to trigger an assert in a debug runtime and go through a slower, rare path for parsing as well.

Fixes #72543
  • Loading branch information
mathieubourgeois committed Jul 25, 2022
1 parent 98d3841 commit a1468ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libraries/System.Runtime/tests/System/Type/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,36 @@ internal abstract class C4 : I4
}
}
#endregion

[Fact]
public void GetEnumTypeCode()
{
Assert.True(Type.GetTypeCode(typeof(TestEnum)) == TypeCode.Int32);
}

[Fact]
public void GetEnumNestedInGenericClassTypeCode()
{
Assert.True(Type.GetTypeCode(typeof(TestGenericClass<TestClass>.NestedEnum)) == TypeCode.Int32);
}

public enum TestEnum
{
A,
B,
C
}

public class TestClass { }
public class TestGenericClass<T>
{
public enum NestedEnum
{
A,
B,
C
}
}
}

public class NonGenericClass { }
Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -2765,6 +2765,10 @@ ves_icall_RuntimeTypeHandle_GetCorElementType (MonoQCallTypeHandle type_handle)
{
MonoType *type = type_handle.type;

// Enums in generic classes should still return VALUETYPE
if (type->type == MONO_TYPE_GENERICINST && m_class_is_enumtype (type->data.generic_class->container_class) && !m_type_is_byref (type))
return MONO_TYPE_VALUETYPE;

if (m_type_is_byref (type))
return MONO_TYPE_BYREF;
else
Expand Down

0 comments on commit a1468ed

Please sign in to comment.