From ad91e5646952e68a97d55a4061517b2b23cdf435 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sun, 25 Dec 2022 17:47:33 +0100 Subject: [PATCH 01/13] delete enum support for bool as an underlying type --- docs/design/specs/Ecma-335-Augments.md | 6 +- .../src/System/Enum.CoreCLR.cs | 2 +- .../src/System/Enum.NativeAot.cs | 4 - .../System.Private.CoreLib/src/System/Enum.cs | 125 ------------------ .../src/System/RuntimeType.cs | 1 - .../System.Runtime/tests/System/EnumTests.cs | 112 ---------------- 6 files changed, 6 insertions(+), 244 deletions(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 8847ab7285d8ae..9b8a69996cb4cf 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -745,7 +745,11 @@ T Exponential(T exponent) where Another example of polymorphic maths calculating the exponential using the Taylor series, usable for calculating the exponential of a matrix. -### II.15.2 Static, Instance and Virtual Methods (page 177) +### II.14.3 Enums (page 177) + +We deleted enum support for bool as underlying type (see [issue 79224](https://github.com/dotnet/runtime/issues/79224)). + +### II.15.2 Static, Instance and Virtual Methods (page 168) (Clarify first paragraph) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs index c6aac1b7bfb967..5850fef8e1170c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs @@ -42,7 +42,7 @@ private unsafe CorElementType InternalGetCorElementType() { null, null, - (RuntimeType)typeof(bool), + null, (RuntimeType)typeof(char), (RuntimeType)typeof(sbyte), (RuntimeType)typeof(byte), diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs index 6bdb4725be7262..2efa61dbb120bd 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs @@ -91,10 +91,6 @@ internal static bool TryGetUnboxedValueOfEnumOrInteger(object value, out ulong r switch (elementType) { - case EETypeElementType.Boolean: - result = Unsafe.As(ref pValue) ? 1UL : 0UL; - return true; - case EETypeElementType.Char: result = (ulong)(long)Unsafe.As(ref pValue); return true; diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index 992448c3304439..23b297b225f7df 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -60,7 +60,6 @@ public abstract partial class Enum : ValueType, IComparable, ISpanFormattable, I if (underlyingType == typeof(float)) return GetNameInlined(GetEnumInfo(rt), *(float*)&value); if (underlyingType == typeof(double)) return GetNameInlined(GetEnumInfo(rt), *(double*)&value); if (underlyingType == typeof(char)) return GetNameInlined(GetEnumInfo(rt), *(char*)&value); - if (underlyingType == typeof(bool)) return GetNameInlined(GetEnumInfo(rt), *(bool*)&value ? (byte)1 : (byte)0); #endif throw CreateUnknownEnumTypeException(); } @@ -132,10 +131,6 @@ public abstract partial class Enum : ValueType, IComparable, ISpanFormattable, I case TypeCode.Char: if (uint64Value > char.MaxValue) return null; return GetName(GetEnumInfo(enumType), (char)uint64Value); - - case TypeCode.Boolean: - if (uint64Value > 1) return null; - return GetName(GetEnumInfo(enumType), (byte)uint64Value); #endif }; @@ -235,7 +230,6 @@ public static string[] GetNames() where TEnum : struct, Enum else if (underlyingType == typeof(float)) names = GetEnumInfo(rt).Names; else if (underlyingType == typeof(double)) names = GetEnumInfo(rt).Names; else if (underlyingType == typeof(char)) names = GetEnumInfo(rt).Names; - else if (underlyingType == typeof(bool)) names = GetEnumInfo(rt).Names; #endif else throw CreateUnknownEnumTypeException(); @@ -276,7 +270,6 @@ internal static string[] GetNamesNoCopy(RuntimeType enumType) CorElementType.ELEMENT_TYPE_I => GetEnumInfo(enumType).Names, CorElementType.ELEMENT_TYPE_U => GetEnumInfo(enumType).Names, CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo(enumType).Names, - CorElementType.ELEMENT_TYPE_BOOLEAN => GetEnumInfo(enumType).Names, #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -365,7 +358,6 @@ internal static Array GetValuesAsUnderlyingType(RuntimeType enumType) CorElementType.ELEMENT_TYPE_I => GetEnumInfo(enumType, getNames: false).CloneValues(), CorElementType.ELEMENT_TYPE_U => GetEnumInfo(enumType, getNames: false).CloneValues(), CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo(enumType, getNames: false).CloneValues(), - CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo(enumType, getNames: false).Values), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -393,7 +385,6 @@ internal static Array GetValuesAsUnderlyingTypeNoCopy(RuntimeType enumType) CorElementType.ELEMENT_TYPE_I => GetEnumInfo(enumType, getNames: false).Values, CorElementType.ELEMENT_TYPE_U => GetEnumInfo(enumType, getNames: false).Values, CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo(enumType, getNames: false).Values, - CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo(enumType, getNames: false).Values), // this is the only case that clones, out of necessity #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -445,9 +436,6 @@ public bool HasFlag(Enum flag) } #if RARE_ENUMS - case CorElementType.ELEMENT_TYPE_BOOLEAN: - goto case CorElementType.ELEMENT_TYPE_U1; - case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2; @@ -472,23 +460,6 @@ public bool HasFlag(Enum flag) } } - /// Marshals a byte[] to a bool[]. - /// The input byte array. - /// A new bool[] containing true whereever a byte was non-zero and false wherever a byte was zero. - /// - /// Since bool isn't a numeric type, we use an EnumInfo{byte} for bool-based enums. This method is used to translate - /// that EnumInfo's Values back to bools. - /// - private static bool[] CopyByteArrayToNewBoolArray(byte[] bytes) - { - bool[] result = new bool[bytes.Length]; - for (int i = 0; i < bytes.Length; i++) - { - result[i] = bytes[i] != 0; - } - return result; - } - /// Returns a telling whether a given integral value, or its name as a string, exists in a specified enumeration. /// The type of the enumeration. /// The value or name of a constant in . @@ -513,7 +484,6 @@ public static unsafe bool IsDefined(TEnum value) where TEnum : struct, En if (underlyingType == typeof(float)) return IsDefinedPrimitive(rt, *(float*)&value); if (underlyingType == typeof(double)) return IsDefinedPrimitive(rt, *(double*)&value); if (underlyingType == typeof(char)) return IsDefinedPrimitive(rt, *(char*)&value); - if (underlyingType == typeof(bool)) return IsDefinedPrimitive(rt, *(bool*)&value ? (byte)1 : (byte)0); #endif throw CreateUnknownEnumTypeException(); @@ -844,13 +814,6 @@ static bool TryParseRareTypes(RuntimeType rt, ReadOnlySpan value, bool ign } break; - case CorElementType.ELEMENT_TYPE_BOOLEAN: - { - parsed = TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out byte localResult); - result = localResult; - } - break; - default: throw CreateUnknownEnumTypeException(); } @@ -943,7 +906,6 @@ private static bool TryParse(ReadOnlySpan value, bool ignoreCase, b if (underlyingType == typeof(nint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As(ref result)); if (underlyingType == typeof(nuint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As(ref result)); if (underlyingType == typeof(char)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As(ref result)); - if (underlyingType == typeof(bool)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As(ref result)); #endif throw CreateUnknownEnumTypeException(); @@ -1186,7 +1148,6 @@ internal static ulong ToUInt64(object value) case TypeCode.UInt32: return (uint)value; case TypeCode.UInt64: return (ulong)value; case TypeCode.Char: return (char)value; - case TypeCode.Boolean: return (bool)value ? 1UL : 0UL; }; if (value is not null) @@ -1224,7 +1185,6 @@ internal object GetValue() CorElementType.ELEMENT_TYPE_I => Unsafe.As(ref data), CorElementType.ELEMENT_TYPE_U => Unsafe.As(ref data), CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As(ref data), - CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As(ref data), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1264,9 +1224,6 @@ public override bool Equals([NotNullWhen(true)] object? obj) return Unsafe.As(ref pThisValue) == Unsafe.As(ref pOtherValue); #if RARE_ENUMS - case CorElementType.ELEMENT_TYPE_BOOLEAN: - goto case CorElementType.ELEMENT_TYPE_U1; - case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2; @@ -1314,7 +1271,6 @@ public override int GetHashCode() CorElementType.ELEMENT_TYPE_I => Unsafe.As(ref data).GetHashCode(), CorElementType.ELEMENT_TYPE_U => Unsafe.As(ref data).GetHashCode(), CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As(ref data).GetHashCode(), - CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As(ref data).GetHashCode(), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1368,9 +1324,6 @@ public int CompareTo(object? target) case CorElementType.ELEMENT_TYPE_R8: return Unsafe.As(ref pThisValue).CompareTo(Unsafe.As(ref pTargetValue)); - case CorElementType.ELEMENT_TYPE_BOOLEAN: - goto case CorElementType.ELEMENT_TYPE_U1; - case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2; @@ -1423,7 +1376,6 @@ static string HandleRareTypes(RuntimeType enumType, ref byte rawData) => CorElementType.ELEMENT_TYPE_I => ToString(enumType, ref rawData), CorElementType.ELEMENT_TYPE_U => ToString(enumType, ref rawData), CorElementType.ELEMENT_TYPE_CHAR => ToString(enumType, ref rawData), - CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, null, ref rawData), _ => throw CreateUnknownEnumTypeException(), }; #else @@ -1475,7 +1427,6 @@ static string HandleRareTypes(RuntimeType enumType, char formatChar, ref byte ra CorElementType.ELEMENT_TYPE_I => ToString(enumType, formatChar, ref rawData), CorElementType.ELEMENT_TYPE_U => ToString(enumType, formatChar, ref rawData), CorElementType.ELEMENT_TYPE_CHAR => ToString(enumType, formatChar, ref rawData), - CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, formatChar.ToString(), ref rawData), _ => throw CreateUnknownEnumTypeException(), }; #else @@ -1569,73 +1520,6 @@ private static string ToStringInlined(RuntimeType enumType, ch return result; } - /// ToString implementation to use for enums with bool as an underlying type. - /// - /// bool is the only underlying type that doesn't fit our scheme using INumber{T}, since it doesn't implement the numeric interfaces. - /// We can mostly make everything work for it by substituting byte, but for some rendering purposes we need to use a dedicated implementation. - /// Performance here is not an issue; these can't be expressed in C#, and serve little purpose, so bool-based enums are exceedingly rare. - /// This exists purely for compat. - /// - private static string ToStringBool(RuntimeType enumType, string? format, ref byte rawData) - { - if (string.IsNullOrEmpty(format)) - { - format = "g"; - } - - byte byteValue = rawData; - bool boolValue = byteValue != 0; - - string? result; - switch (format[0] | 0x20) - { - case 'g': - EnumInfo enumInfo = GetEnumInfo(enumType); - result = enumInfo.HasFlagsAttribute ? FormatFlagNames(enumInfo, byteValue) : GetName(enumInfo, byteValue); - if (result is null) - { - goto case 'd'; - } - break; - - case 'd': - return boolValue.ToString(); - - case 'x': - return boolValue ? "01" : "00"; - - case 'f': - result = FormatFlagNames(GetEnumInfo(enumType), byteValue); - if (result is null) - { - goto case 'd'; - } - break; - - default: - throw CreateInvalidFormatSpecifierException(); - } - - return result; - } - - /// TryFormat implementation to use for enums with bool as an underlying type. - /// See remarks on . - private static bool TryFormatBool(RuntimeType enumType, bool boolValue, Span destination, out int charsWritten, ReadOnlySpan format) - { - byte byteValue = boolValue ? (byte)1 : (byte)0; - - string result = ToStringBool(enumType, format.ToString(), ref byteValue); - if (result.TryCopyTo(destination)) - { - charsWritten = result.Length; - return true; - } - - charsWritten = 0; - return false; - } - /// Formats the data for the underlying value as hex into a new, fixed-length string. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe string FormatNumberAsHex(ref byte data) where TUnderlyingValue : struct @@ -1772,7 +1656,6 @@ public static string Format(Type enumType, object value, [StringSyntax(StringSyn CorElementType.ELEMENT_TYPE_I => ToString(rtType, formatChar, ref rawData), CorElementType.ELEMENT_TYPE_U => ToString(rtType, formatChar, ref rawData), CorElementType.ELEMENT_TYPE_CHAR => ToString(rtType, formatChar, ref rawData), - CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(rtType, format, ref rawData), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1812,7 +1695,6 @@ bool ISpanFormattable.TryFormat(Span destination, out int charsWritten, Re CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten), CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten), CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten), - CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1835,7 +1717,6 @@ bool ISpanFormattable.TryFormat(Span destination, out int charsWritten, Re CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveNonDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten, format), CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveNonDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten, format), CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveNonDefault(enumType, Unsafe.As(ref rawData), destination, out charsWritten, format), - CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1875,7 +1756,6 @@ public static unsafe bool TryFormat(TEnum value, Span destination, if (underlyingType == typeof(float)) return TryFormatPrimitiveDefault(rt, *(float*)&value, destination, out charsWritten); if (underlyingType == typeof(double)) return TryFormatPrimitiveDefault(rt, *(double*)&value, destination, out charsWritten); if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten); - if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format: default); #endif } else @@ -1894,7 +1774,6 @@ public static unsafe bool TryFormat(TEnum value, Span destination, if (underlyingType == typeof(float)) return TryFormatPrimitiveNonDefault(rt, *(float*)&value, destination, out charsWritten, format); if (underlyingType == typeof(double)) return TryFormatPrimitiveNonDefault(rt, *(double*)&value, destination, out charsWritten, format); if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format); - if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format); #endif } @@ -1936,7 +1815,6 @@ internal static unsafe bool TryFormatUnconstrained(TEnum value, Span(TEnum value, Span CorElementType.ELEMENT_TYPE_I8 => TypeCode.Int64, CorElementType.ELEMENT_TYPE_U8 => TypeCode.UInt64, CorElementType.ELEMENT_TYPE_CHAR => TypeCode.Char, - CorElementType.ELEMENT_TYPE_BOOLEAN => TypeCode.Boolean, // There's no TypeCode for nint or nuint, and our VB support (or at least // tests) needs to be updated in order to include float/double here. _ => throw CreateUnknownEnumTypeException(), @@ -2314,7 +2190,6 @@ public static object ToObject(Type enumType, object value) case TypeCode.Single: return ToObject(enumType, BitConverter.SingleToInt32Bits((float)value)); case TypeCode.Double: return ToObject(enumType, BitConverter.DoubleToInt64Bits((double)value)); case TypeCode.Char: return ToObject(enumType, (char)value); - case TypeCode.Boolean: return ToObject(enumType, (bool)value ? 1L : 0L); }; Type valueType = value.GetType(); diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index 8fd7947e8797ad..939de05b9a00bf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -245,7 +245,6 @@ public override bool IsEnumDefined(object value) TypeCode.Single => Enum.IsDefinedPrimitive(this, (float)value), TypeCode.Double => Enum.IsDefinedPrimitive(this, (double)value), TypeCode.Char => Enum.IsDefinedPrimitive(this, (char)value), - TypeCode.Boolean => Enum.IsDefinedPrimitive(this, (bool)value ? (byte)1 : (byte)0), _ => underlyingType == typeof(nint) ? Enum.IsDefinedPrimitive(this, (nint)value) : underlyingType == typeof(nuint) ? Enum.IsDefinedPrimitive(this, (nuint)value) : diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index d87abc5bdbc7c6..059d6ee9575be8 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -84,10 +84,6 @@ public static IEnumerable Parse_TestData() yield return new object[] { "vaLue2", true, Enum.ToObject(s_charEnumType, (char)2) }; yield return new object[] { "1", false, Enum.ToObject(s_charEnumType, '1') }; - // Bool - yield return new object[] { "Value1", false, Enum.ToObject(s_boolEnumType, true) }; - yield return new object[] { "vaLue2", true, Enum.ToObject(s_boolEnumType, false) }; - // Single yield return new object[] { "Value1", false, Enum.GetValues(s_floatEnumType).GetValue(1) }; yield return new object[] { "vaLue2", true, Enum.GetValues(s_floatEnumType).GetValue(2) }; @@ -210,10 +206,6 @@ public static IEnumerable Parse_Invalid_TestData() yield return new object[] { s_charEnumType, ((char)1).ToString(), false, typeof(ArgumentException) }; yield return new object[] { s_charEnumType, ((char)5).ToString(), false, typeof(ArgumentException) }; - // Bool - yield return new object[] { s_boolEnumType, bool.TrueString, false, typeof(ArgumentException) }; - yield return new object[] { s_boolEnumType, bool.FalseString, false, typeof(ArgumentException) }; - // IntPtr yield return new object[] { s_intPtrEnumType, "1", false, typeof(InvalidCastException) }; yield return new object[] { s_intPtrEnumType, "5", false, typeof(InvalidCastException) }; @@ -463,21 +455,6 @@ public void GetName_InvokeCharEnum_ReturnsExpected(object value, string expected TestGetName(s_charEnumType, value, expected); } - public static IEnumerable GetName_BoolEnum_TestData() - { - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), "Value1" }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value2"), "Value2" }; - yield return new object[] { true, "Value1" }; - yield return new object[] { false, "Value2" }; - } - - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - [MemberData(nameof(GetName_BoolEnum_TestData))] - public void GetName_InvokeBoolEnum_ReturnsExpected(object value, string expected) - { - TestGetName(s_boolEnumType, value, expected); - } - private void TestGetName(Type enumType, object value, string expected) { Assert.Equal(expected, Enum.GetName(enumType, value)); @@ -540,7 +517,6 @@ public void GetName_InvalidValue_ThrowsArgumentException(object value) [InlineData(typeof(SByteEnum), 0xffffffffffffff80LU, "Min")] [InlineData(typeof(SByteEnum), 0xffffff80u, null)] [InlineData(typeof(SByteEnum), unchecked((int)(0xffffff80u)), "Min")] - [InlineData(typeof(SByteEnum), true, "One")] [InlineData(typeof(SByteEnum), (char)1, "One")] [InlineData(typeof(SByteEnum), SimpleEnum.Red, "One")] // API doesn't care if you pass in a completely different enum public static void GetName_NonIntegralTypes_ReturnsExpected(Type enumType, object value, string expected) @@ -719,23 +695,6 @@ public void IsDefined_InvokeCharEnum_ReturnsExpected(object value, bool expected Assert.Equal(expected, Enum.IsDefined(s_charEnumType, value)); } - public static IEnumerable IsDefined_BoolEnum_TestData() - { - yield return new object[] { "Value1", true }; - yield return new object[] { "None", false }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), true }; - yield return new object[] { "Value1", true }; - yield return new object[] { true, true }; - yield return new object[] { false, true }; - } - - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - [MemberData(nameof(IsDefined_BoolEnum_TestData))] - public void IsDefined_InvokeBoolEnum_ReturnsExpected(object value, bool expected) - { - Assert.Equal(expected, Enum.IsDefined(s_boolEnumType, value)); - } - [Fact] public void IsDefined_NullEnumType_ThrowsArgumentNullException() { @@ -856,12 +815,6 @@ public static IEnumerable HasFlag_TestData() yield return new object[] { Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x0010"), false }; yield return new object[] { Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x3f16"), false }; - // Bool - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value1"), true }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value2"), true }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value2"), true }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value1"), false }; - // Single yield return new object[] { Enum.ToObject(s_floatEnumType, 0x3f06), Enum.ToObject(s_floatEnumType, 0x0000), true }; yield return new object[] { Enum.ToObject(s_floatEnumType, 0x3f06), Enum.ToObject(s_floatEnumType, 0x0f06), true }; @@ -944,7 +897,6 @@ public static IEnumerable ToObject_TestData() yield return new object[] { typeof(Int32Enum), 66, (Int32Enum)66 }; yield return new object[] { typeof(Int32Enum), 'a', (Int32Enum)97 }; yield return new object[] { typeof(Int32Enum), 'b', (Int32Enum)98 }; - yield return new object[] { typeof(Int32Enum), true, (Int32Enum)1 }; // UInt32 yield return new object[] { typeof(UInt32Enum), uint.MaxValue, UInt32Enum.Max }; @@ -968,10 +920,6 @@ public static IEnumerable ToObject_TestData() yield return new object[] { s_charEnumType, (char)1, Enum.Parse(s_charEnumType, "Value1") }; yield return new object[] { s_charEnumType, (char)2, Enum.Parse(s_charEnumType, "Value2") }; - // Bool - yield return new object[] { s_boolEnumType, true, Enum.Parse(s_boolEnumType, "Value1") }; - yield return new object[] { s_boolEnumType, false, Enum.Parse(s_boolEnumType, "Value2") }; - // Float yield return new object[] { s_floatEnumType, 1.0f, Enum.Parse(s_floatEnumType, "Value1") }; yield return new object[] { s_floatEnumType, 2.0f, Enum.Parse(s_floatEnumType, "Value2") }; @@ -1012,7 +960,6 @@ public static void ToObject_InvalidEnumType_ThrowsException(Type enumType, Type Assert.Throws(exceptionType, () => Enum.ToObject(enumType, (ushort)5)); Assert.Throws(exceptionType, () => Enum.ToObject(enumType, (ulong)5)); Assert.Throws(exceptionType, () => Enum.ToObject(enumType, 'a')); - Assert.Throws(exceptionType, () => Enum.ToObject(enumType, true)); } public static IEnumerable ToObject_InvalidValue_TestData() @@ -1115,14 +1062,6 @@ public static IEnumerable Equals_TestData() yield return new object[] { Enum.Parse(s_charEnumType, "Value1"), new object(), false }; yield return new object[] { Enum.Parse(s_charEnumType, "Value1"), null, false }; - // Bool - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value1"), true }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value2"), false }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), UInt16Enum.One, false }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), true, false }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), new object(), false }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), null, false }; - // Single yield return new object[] { Enum.ToObject(s_floatEnumType, 1), Enum.ToObject(s_floatEnumType, 1), true }; yield return new object[] { Enum.ToObject(s_floatEnumType, 1), Enum.ToObject(s_floatEnumType, 2), false }; @@ -1224,12 +1163,6 @@ public static IEnumerable CompareTo_TestData() yield return new object[] { Enum.Parse(s_charEnumType, "Value1"), Enum.Parse(s_charEnumType, "Value2"), -1 }; yield return new object[] { Enum.Parse(s_charEnumType, "Value2"), null, 1 }; - // Bool - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value1"), 0 }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), Enum.Parse(s_boolEnumType, "Value2"), 1 }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value1"), -1 }; - yield return new object[] { Enum.Parse(s_boolEnumType, "Value1"), null, 1 }; - // Single yield return new object[] { Enum.ToObject(s_floatEnumType, 1), Enum.ToObject(s_floatEnumType, 1), 0 }; yield return new object[] { Enum.ToObject(s_floatEnumType, 1), Enum.ToObject(s_floatEnumType, 2), -1 }; @@ -1284,7 +1217,6 @@ public static IEnumerable GetUnderlyingType_TestData() if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) { yield return new object[] { s_charEnumType, typeof(char) }; - yield return new object[] { s_boolEnumType, typeof(bool) }; yield return new object[] { s_floatEnumType, typeof(float) }; yield return new object[] { s_doubleEnumType, typeof(double) }; yield return new object[] { s_intPtrEnumType, typeof(IntPtr) }; @@ -1395,14 +1327,6 @@ public void GetNames_InvokeCharEnum_ReturnsExpected() Assert.NotSame(Enum.GetNames(s_charEnumType), Enum.GetNames(s_charEnumType)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - public void GetNames_InvokeBoolEnum_ReturnsExpected() - { - var expected = new string[] { "Value2", "Value1" }; - Assert.Equal(expected, Enum.GetNames(s_boolEnumType)); - Assert.NotSame(Enum.GetNames(s_boolEnumType), Enum.GetNames(s_boolEnumType)); - } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] public void GetNames_InvokeSingleEnum_ReturnsExpected() { @@ -1540,14 +1464,6 @@ public void GetValues_InvokeCharEnum_ReturnsExpected() Assert.NotSame(Enum.GetValues(s_charEnumType), Enum.GetValues(s_charEnumType)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - public void GetValues_InvokeBoolEnum_ReturnsExpected() - { - var expected = new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value1") }; - Assert.Equal(expected, Enum.GetValues(s_boolEnumType)); - Assert.NotSame(Enum.GetValues(s_boolEnumType), Enum.GetValues(s_boolEnumType)); - } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] public void GetValues_InvokeSingleEnum_ReturnsExpected() { @@ -1759,11 +1675,6 @@ public static IEnumerable ToString_Format_TestData() yield return new object[] { Enum.ToObject(s_charEnumType, (char)2), "D", ((char)2).ToString() }; yield return new object[] { Enum.ToObject(s_charEnumType, char.MaxValue), "D", char.MaxValue.ToString() }; - // "D:" Bool - yield return new object[] { Enum.ToObject(s_boolEnumType, true), "D", bool.TrueString }; - yield return new object[] { Enum.ToObject(s_boolEnumType, false), "D", bool.FalseString }; - yield return new object[] { Enum.ToObject(s_boolEnumType, 123), "D", bool.TrueString }; - // "D": Single yield return new object[] { Enum.ToObject(s_floatEnumType, 0), "D", "0" }; yield return new object[] { Enum.ToObject(s_floatEnumType, 1), "D", float.Epsilon.ToString() }; @@ -1841,12 +1752,6 @@ public static IEnumerable ToString_Format_TestData() yield return new object[] { Enum.ToObject(s_charEnumType, (char)1), "X", "0001" }; yield return new object[] { Enum.ToObject(s_charEnumType, (char)2), "X", "0002" }; yield return new object[] { Enum.ToObject(s_charEnumType, char.MaxValue), "X", "FFFF" }; - - - // "X": Bool - yield return new object[] { Enum.ToObject(s_boolEnumType, false), "X", "00" }; - yield return new object[] { Enum.ToObject(s_boolEnumType, true), "X", "01" }; - yield return new object[] { Enum.ToObject(s_boolEnumType, 123), "X", "01" }; } // "X": SimpleEnum @@ -1908,10 +1813,6 @@ public static IEnumerable ToString_Format_TestData() yield return new object[] { Enum.ToObject(s_charEnumType, (char)5), "F", ((char)5).ToString() }; yield return new object[] { Enum.ToObject(s_charEnumType, char.MaxValue), "F", char.MaxValue.ToString() }; - // "F": Bool - yield return new object[] { Enum.ToObject(s_boolEnumType, true), "F", "Value1" }; - yield return new object[] { Enum.ToObject(s_boolEnumType, false), "F", "Value2" }; - // "F": IntPtr yield return new object[] { Enum.ToObject(s_intPtrEnumType, 5), "F", "5" }; @@ -2436,19 +2337,6 @@ private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType) return module.DefineEnum("TestName_" + underlyingType.Name, TypeAttributes.Public, underlyingType); } - private static Type s_boolEnumType = GetBoolEnumType(); - private static Type GetBoolEnumType() - { - EnumBuilder enumBuilder = GetNonRuntimeEnumTypeBuilder(typeof(bool)); - if (enumBuilder == null) - return null; - - enumBuilder.DefineLiteral("Value1", true); - enumBuilder.DefineLiteral("Value2", false); - - return enumBuilder.CreateType(); - } - private static Type s_charEnumType = GetCharEnumType(); private static Type GetCharEnumType() { From f01ed0a47e102a76dce6e1c303866f63d9b05ee4 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sun, 25 Dec 2022 19:59:30 +0100 Subject: [PATCH 02/13] fix failing unit tests 1 --- .../tests/ConversionsTests.cs | 74 ------------------- .../tests/Cast/CastTests.cs | 1 - .../tests/CustomAttributeBuilderTests.cs | 2 - .../ModuleBuilder/ModuleBuilderDefineEnum.cs | 1 - .../TypeIntrinsics.GetEnumUnderlyingType.cs | 2 - src/tests/JIT/Intrinsics/TypeIntrinsics.cs | 1 - .../JIT/Intrinsics/TypeIntrinsicsEnums.il | 6 -- 7 files changed, 87 deletions(-) diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionsTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionsTests.cs index 3d0366a96bb2fd..1712e8f549bda3 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionsTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionsTests.cs @@ -159,10 +159,6 @@ public static IEnumerable ToByte_Object_TestData() // bool. yield return new object[] { true, byte.MaxValue }; yield return new object[] { false, byte.MinValue }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, byte.MinValue }; - } // null. yield return new object[] { null, byte.MinValue }; @@ -340,10 +336,6 @@ public static IEnumerable ToSByte_Object_TestData() // bool. yield return new object[] { true, (sbyte)(-1) }; yield return new object[] { false, (sbyte)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (sbyte)0 }; - } // null. yield return new object[] { null, (sbyte)0 }; @@ -514,10 +506,6 @@ public static IEnumerable ToUShort_Object_TestData() // bool. yield return new object[] { true, ushort.MaxValue }; yield return new object[] { false, ushort.MinValue }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, ushort.MinValue }; - } // null. yield return new object[] { null, ushort.MinValue }; @@ -698,10 +686,6 @@ public static IEnumerable ToShort_Object_TestData() // bool. yield return new object[] { true, (short)(-1) }; yield return new object[] { false, (short)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (short)0 }; - } // null. yield return new object[] { null, (short)0 }; @@ -876,10 +860,6 @@ public static IEnumerable ToUInteger_Object_TestData() // bool. yield return new object[] { true, uint.MaxValue }; yield return new object[] { false, uint.MinValue }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, uint.MinValue }; - } // null. yield return new object[] { null, uint.MinValue }; @@ -1066,10 +1046,6 @@ public static IEnumerable ToInteger_Object_TestData() // bool. yield return new object[] { true, -1 }; yield return new object[] { false, 0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, 0 }; - } // null. yield return new object[] { null, 0 }; @@ -1252,10 +1228,6 @@ public static IEnumerable ToULong_Object_TestData() // bool. yield return new object[] { true, ulong.MaxValue }; yield return new object[] { false, ulong.MinValue }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, ulong.MinValue }; - } // null. yield return new object[] { null, ulong.MinValue }; @@ -1480,10 +1452,6 @@ public static IEnumerable ToLong_Object_TestData() // bool. yield return new object[] { true, (long)(-1) }; yield return new object[] { false, (long)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (long)0 }; - } // null. yield return new object[] { null, (long)0 }; @@ -1706,10 +1674,6 @@ public static IEnumerable ToSingle_Object_TestData() // bool. yield return new object[] { true, (float)(-1) }; yield return new object[] { false, (float)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (float)0 }; - } // null. yield return new object[] { null, (float)0 }; @@ -1899,10 +1863,6 @@ public static IEnumerable ToDouble_Object_TestData() // bool. yield return new object[] { true, (double)(-1) }; yield return new object[] { false, (double)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (double)0 }; - } // null. yield return new object[] { null, (double)0 }; @@ -2095,10 +2055,6 @@ public static IEnumerable ToDecimal_Object_TestData() // bool. yield return new object[] { true, (decimal)(-1) }; yield return new object[] { false, (decimal)0 }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, (decimal)0 }; - } // null. yield return new object[] { null, (decimal)0 }; @@ -2321,10 +2277,6 @@ public static IEnumerable ToBoolean_Object_TestData() // bool. yield return new object[] { true, true }; yield return new object[] { false, false }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, false }; - } // null. yield return new object[] { null, false }; @@ -2636,10 +2588,6 @@ public static IEnumerable ToString_IConvertible_TestData() // bool. yield return new object[] { true, "True" }; yield return new object[] { false, "False" }; - if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) - { - yield return new object[] { BoolEnum, "False" }; - } // string. yield return new object[] { "", "" }; @@ -2749,28 +2697,6 @@ public static object DoubleEnum } } - private static object s_boolEnum; - - public static object BoolEnum - { - get - { - if (s_boolEnum == null) - { - AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Name"), AssemblyBuilderAccess.RunAndCollect); - ModuleBuilder module = assembly.DefineDynamicModule("Name"); - - EnumBuilder eb = module.DefineEnum("BoolEnumType", TypeAttributes.Public, typeof(bool)); - eb.DefineLiteral("False", false); - eb.DefineLiteral("True", true); - - s_boolEnum = Activator.CreateInstance(eb.CreateTypeInfo()); - } - - return s_boolEnum; - } - } - private static object s_charEnum; public static object CharEnum diff --git a/src/libraries/System.Linq.Expressions/tests/Cast/CastTests.cs b/src/libraries/System.Linq.Expressions/tests/Cast/CastTests.cs index 8a3b0569baa2cc..96e627af9d6b23 100644 --- a/src/libraries/System.Linq.Expressions/tests/Cast/CastTests.cs +++ b/src/libraries/System.Linq.Expressions/tests/Cast/CastTests.cs @@ -2376,7 +2376,6 @@ public static IEnumerable EnumerableTypes() if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) { yield return NonCSharpTypes.CharEnumType; - yield return NonCSharpTypes.BoolEnumType; } } diff --git a/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs b/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs index b349b1796d5876..47779e6f7fa364 100644 --- a/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs @@ -580,7 +580,6 @@ public static IEnumerable NotSupportedObject_Constructor_TestData() if (PlatformDetection.IsRareEnumsSupported) { yield return new object[] { Enum.GetValues(CreateEnum(typeof(char), 'a')).GetValue(0) }; - yield return new object[] { Enum.GetValues(CreateEnum(typeof(bool), true)).GetValue(0) }; } } @@ -648,7 +647,6 @@ public static IEnumerable InvalidAttributeTypes_TestData() if (PlatformDetection.IsRareEnumsSupported) { yield return new object[] { CreateEnum(typeof(char), 'a'), 'a' }; - yield return new object[] { CreateEnum(typeof(bool), false), true }; yield return new object[] { CreateEnum(typeof(float), 1.0f), 1.0f }; yield return new object[] { CreateEnum(typeof(double), 1.0), 1.0 }; yield return new object[] { CreateEnum(typeof(IntPtr)), (IntPtr)1 }; diff --git a/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs b/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs index 6173ffd8570815..32d6723c71e416 100644 --- a/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs +++ b/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs @@ -19,7 +19,6 @@ public static IEnumerable DefineEnum_TestData() yield return new object[] { "Name", TypeAttributes.Public, typeof(uint) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(long) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(char) }; - yield return new object[] { "Name", TypeAttributes.Public, typeof(bool) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(ulong) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(float) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(double) }; diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs index 93142921f1d73f..d919e4056e66ae 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs @@ -22,7 +22,6 @@ public static void TestGetEnumUnderlyingType() AssertEquals(typeof(ulong), typeof(ULongEnum).GetEnumUnderlyingType()); AssertEquals(typeof(char), typeof(CharEnum).GetEnumUnderlyingType()); - AssertEquals(typeof(bool), typeof(BoolEnum).GetEnumUnderlyingType()); AssertEquals(typeof(float), typeof(FloatEnum).GetEnumUnderlyingType()); AssertEquals(typeof(double), typeof(DoubleEnum).GetEnumUnderlyingType()); AssertEquals(typeof(nint), typeof(IntPtrEnum).GetEnumUnderlyingType()); @@ -44,7 +43,6 @@ public static void TestGetEnumUnderlyingType() AssertEquals(typeof(ulong), NoInline(typeof(ULongEnum).GetEnumUnderlyingType())); AssertEquals(typeof(char), NoInline(typeof(CharEnum).GetEnumUnderlyingType())); - AssertEquals(typeof(bool), NoInline(typeof(BoolEnum).GetEnumUnderlyingType())); AssertEquals(typeof(float), NoInline(typeof(FloatEnum).GetEnumUnderlyingType())); AssertEquals(typeof(double), NoInline(typeof(DoubleEnum).GetEnumUnderlyingType())); AssertEquals(typeof(nint), NoInline(typeof(IntPtrEnum).GetEnumUnderlyingType())); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs index c697abc879155e..3f892b5c9df09d 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs @@ -120,7 +120,6 @@ public static int Main() IsFalse(typeof(SimpleStruct).IsEnum); IsTrue (typeof(SimpleEnum).IsEnum); IsTrue (typeof(CharEnum).IsEnum); - IsTrue (typeof(BoolEnum).IsEnum); IsTrue (typeof(FloatEnum).IsEnum); IsTrue (typeof(DoubleEnum).IsEnum); IsTrue (typeof(IntPtrEnum).IsEnum); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il b/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il index 36f9f07dd8fdcf..e3b5d797bf4d98 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il +++ b/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il @@ -11,12 +11,6 @@ .field public specialname rtspecialname char value__ } // end of class CharEnum -.class public auto ansi sealed BoolEnum - extends [System.Runtime]System.Enum -{ - .field public specialname rtspecialname bool value__ -} // end of class BoolEnum - .class public auto ansi sealed FloatEnum extends [System.Runtime]System.Enum { From 3bcdf7a8b29c46d5c6381719f7f2b481c730d51c Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Tue, 27 Dec 2022 23:33:18 +0100 Subject: [PATCH 03/13] forgotten boolean enum code --- docs/design/specs/Ecma-335-Augments.md | 6 +++--- .../System.Private.CoreLib/src/System/InvokeUtils.cs | 3 +-- src/coreclr/vm/reflectioninvocation.cpp | 3 +-- .../src/System/Reflection/InvokeUtils.cs | 3 +-- .../System.Private.CoreLib/src/System/Type.Enum.cs | 3 +-- src/mono/mono/metadata/class.c | 1 - src/mono/mono/metadata/icall.c | 4 +--- 7 files changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 9b8a69996cb4cf..67503eaf1b5dfa 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -745,11 +745,11 @@ T Exponential(T exponent) where Another example of polymorphic maths calculating the exponential using the Taylor series, usable for calculating the exponential of a matrix. -### II.14.3 Enums (page 177) +### II.14.3 Enums (page 168) -We deleted enum support for bool as underlying type (see [issue 79224](https://github.com/dotnet/runtime/issues/79224)). +Enum support for bool as underlying type was deleted (see [issue 79224](https://github.com/dotnet/runtime/issues/79224)). -### II.15.2 Static, Instance and Virtual Methods (page 168) +### II.15.2 Static, Instance and Virtual Methods (page 177) (Clarify first paragraph) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs index 9756ce8c6b95e1..e1d9d80dac1287 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs @@ -134,8 +134,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (dstCorElementType) { case CorElementType.ELEMENT_TYPE_BOOLEAN: - bool boolValue = Convert.ToBoolean(srcObject); - dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, boolValue ? 1 : 0) : boolValue; + dstObject = Convert.ToBoolean(srcObject); break; case CorElementType.ELEMENT_TYPE_CHAR: diff --git a/src/coreclr/vm/reflectioninvocation.cpp b/src/coreclr/vm/reflectioninvocation.cpp index 4abe63d6d9cf38..83043302dc1b8b 100644 --- a/src/coreclr/vm/reflectioninvocation.cpp +++ b/src/coreclr/vm/reflectioninvocation.cpp @@ -1979,8 +1979,7 @@ extern "C" void QCALLTYPE Enum_GetValuesAndNames(QCall::TypeHandle pEnumType, QC GCPROTECT_BEGIN(gc); { - // The managed side expects ELEMENT_TYPE_U1 as the underlying type for boolean - gc.values = (BASEARRAYREF) AllocatePrimitiveArray((type == ELEMENT_TYPE_BOOLEAN) ? ELEMENT_TYPE_U1 : type, cFields); + gc.values = (BASEARRAYREF) AllocatePrimitiveArray(type, cFields); BYTE* pToValues = gc.values->GetDataPtr(); size_t elementSize = gc.values->GetComponentSize(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/InvokeUtils.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/InvokeUtils.cs index 16ee1c358171ce..1471d1d977ef1e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/InvokeUtils.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/InvokeUtils.cs @@ -29,8 +29,7 @@ public static object ConvertOrWiden(Type srcType, object srcObject, Type dstType switch (dstElementType) { case CorElementType.ELEMENT_TYPE_BOOLEAN: - bool boolValue = Convert.ToBoolean(srcObject); - dstObject = dstType.IsEnum ? Enum.ToObject(dstType, boolValue ? 1 : 0) : boolValue; + dstObject = Convert.ToBoolean(srcObject); break; case CorElementType.ELEMENT_TYPE_CHAR: diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs index 80b610aa81182f..d9885618c1288f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs @@ -174,8 +174,7 @@ internal static bool IsIntegerType(Type t) t == typeof(uint) || t == typeof(long) || t == typeof(ulong) || - t == typeof(char) || - t == typeof(bool); + t == typeof(char); } } } diff --git a/src/mono/mono/metadata/class.c b/src/mono/mono/metadata/class.c index 3a04a9dee77c41..22251455a0805c 100644 --- a/src/mono/mono/metadata/class.c +++ b/src/mono/mono/metadata/class.c @@ -6494,7 +6494,6 @@ gboolean mono_type_is_valid_enum_basetype (MonoType * type) { switch (type->type) { case MONO_TYPE_I1: case MONO_TYPE_U1: - case MONO_TYPE_BOOLEAN: case MONO_TYPE_I2: case MONO_TYPE_U2: case MONO_TYPE_CHAR: diff --git a/src/mono/mono/metadata/icall.c b/src/mono/mono/metadata/icall.c index 34a4f476e7d616..16f3c7ecfabf99 100644 --- a/src/mono/mono/metadata/icall.c +++ b/src/mono/mono/metadata/icall.c @@ -3557,7 +3557,6 @@ static guint64 read_enum_value (const char *mem, int type) { switch (type) { - case MONO_TYPE_BOOLEAN: case MONO_TYPE_U1: return *(guint8*)mem; case MONO_TYPE_I1: @@ -3594,8 +3593,7 @@ write_enum_value (void *mem, int type, guint64 value) { switch (type) { case MONO_TYPE_U1: - case MONO_TYPE_I1: - case MONO_TYPE_BOOLEAN: { + case MONO_TYPE_I1: { guint8 *p = (guint8*)mem; *p = GUINT64_TO_UINT8 (value); break; From a07cb54bcae7e97c06257f0b66aaa2c72bc76737 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Thu, 29 Dec 2022 22:14:42 +0100 Subject: [PATCH 04/13] fix failing tests --- src/libraries/System.Private.CoreLib/src/System/Enum.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index 23b297b225f7df..4101070b966dc7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -1185,6 +1185,7 @@ internal object GetValue() CorElementType.ELEMENT_TYPE_I => Unsafe.As(ref data), CorElementType.ELEMENT_TYPE_U => Unsafe.As(ref data), CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As(ref data), + CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As(ref data), #endif _ => throw CreateUnknownEnumTypeException(), }; From 9a6bf2ae3e1856f81cb5d9e71cfabe517f83f06f Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Thu, 29 Dec 2022 23:18:26 +0100 Subject: [PATCH 05/13] fix failing tests in system.runtime --- src/libraries/System.Runtime/tests/System/EnumTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index 059d6ee9575be8..ce6013384cbef6 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -709,7 +709,6 @@ public void IsDefined_NullValue_ThrowsArgumentNullException() [Theory] [InlineData(Int32Enum.One)] - [InlineData(true)] [InlineData('a')] public void IsDefined_InvalidValue_ThrowsArgumentException(object value) { From 2503ca5099686811d94ca4896b36ab6e8a3bff7c Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Thu, 5 Jan 2023 22:13:02 +0100 Subject: [PATCH 06/13] add test for boolean enum building --- .../src/System/Reflection/Emit/TypeBuilder.cs | 7 ++++++- .../System.Runtime/tests/System/EnumTests.cs | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index 088d22b5a1983b..ab2e370d5278d6 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -345,9 +345,14 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type switch (corType) { + case CorElementType.ELEMENT_TYPE_BOOLEAN: + if (destType.IsEnum) + { + throw new ArgumentException(SR.Format(SR.Argument_ConstantNotSupported, type)); + } + goto case CorElementType.ELEMENT_TYPE_I1; case CorElementType.ELEMENT_TYPE_I1: case CorElementType.ELEMENT_TYPE_U1: - case CorElementType.ELEMENT_TYPE_BOOLEAN: case CorElementType.ELEMENT_TYPE_I2: case CorElementType.ELEMENT_TYPE_U2: case CorElementType.ELEMENT_TYPE_CHAR: diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index ce6013384cbef6..13aa64e809e180 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; -using System.Linq; using System.Reflection; using System.Reflection.Emit; using Xunit; @@ -1596,6 +1595,13 @@ public void EnumConstraint_ThrowsArgumentException() Assert.Throws(() => Enum.GetValues(genericArgumentWithEnumConstraint)); } + [Fact] + public void DeclaringEnumWithBoolUnderlyingTypeFails() + { + var exception = Assert.Throws(GetBoolEnumType); + Assert.Equal("System.Boolean is not a supported constant type.", exception.Message); + } + public static IEnumerable ToString_Format_TestData() { // Format "D": the decimal equivalent of the value is returned. @@ -2336,6 +2342,19 @@ private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType) return module.DefineEnum("TestName_" + underlyingType.Name, TypeAttributes.Public, underlyingType); } + private static Type GetBoolEnumType() + { + EnumBuilder enumBuilder = GetNonRuntimeEnumTypeBuilder(typeof(bool)); + + if (enumBuilder == null) + return null; + + enumBuilder.DefineLiteral("Value1", true); + enumBuilder.DefineLiteral("Value2", false); + + return enumBuilder.CreateType(); + } + private static Type s_charEnumType = GetCharEnumType(); private static Type GetCharEnumType() { From 72486cc16622ac3de7f0ec7c3e74b0c1e6299187 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Thu, 5 Jan 2023 22:17:12 +0100 Subject: [PATCH 07/13] delete useless new line --- src/libraries/System.Runtime/tests/System/EnumTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index 13aa64e809e180..bff7feaf4501dd 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -2345,7 +2345,6 @@ private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType) private static Type GetBoolEnumType() { EnumBuilder enumBuilder = GetNonRuntimeEnumTypeBuilder(typeof(bool)); - if (enumBuilder == null) return null; From a8bd8383cb3a54a4864de06d077ac967fa7a6aab Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Fri, 6 Jan 2023 01:11:45 +0100 Subject: [PATCH 08/13] fix unit tests --- .../tests/EnumBuilder/EnumBuilder.Methods.Tests.cs | 3 --- .../System.Runtime/tests/System/EnumTests.cs | 11 +++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs b/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs index 32133e76b570eb..386fd15e999743 100644 --- a/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs +++ b/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs @@ -47,9 +47,6 @@ private static IEnumerable DefineLiteralTestDataCore() yield return new object[] { typeof(char), (char)0 }; yield return new object[] { typeof(char), (char)1 }; - yield return new object[] { typeof(bool), true }; - yield return new object[] { typeof(bool), false }; - yield return new object[] { typeof(float), 0f }; yield return new object[] { typeof(float), 1.1f }; diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index bff7feaf4501dd..4f7a44eeb919db 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -1595,13 +1595,20 @@ public void EnumConstraint_ThrowsArgumentException() Assert.Throws(() => Enum.GetValues(genericArgumentWithEnumConstraint)); } - [Fact] - public void DeclaringEnumWithBoolUnderlyingTypeFails() + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + public void DeclaringEnumWithBoolUnderlyingTypeFailsInCoreclr() { var exception = Assert.Throws(GetBoolEnumType); Assert.Equal("System.Boolean is not a supported constant type.", exception.Message); } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMonoRuntime))] + public void DeclaringEnumWithBoolUnderlyingTypeFailsInMono() + { + var exception = Assert.Throws(GetBoolEnumType); + Assert.Equal("Not a valid enumeration", exception.Message); + } + public static IEnumerable ToString_Format_TestData() { // Format "D": the decimal equivalent of the value is returned. From 4f56b30c7215268513ff7269b6b2a59c6b937e22 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sat, 7 Jan 2023 22:58:24 +0100 Subject: [PATCH 09/13] keep loading bool backed enum tests --- docs/design/specs/Ecma-335-Augments.md | 4 ---- .../System.Private.CoreLib/src/System/Enum.CoreCLR.cs | 2 +- src/libraries/System.Runtime/tests/System/EnumTests.cs | 4 ++-- .../JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs | 1 + src/tests/JIT/Intrinsics/TypeIntrinsics.cs | 1 + src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il | 6 ++++++ 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 67503eaf1b5dfa..8847ab7285d8ae 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -745,10 +745,6 @@ T Exponential(T exponent) where Another example of polymorphic maths calculating the exponential using the Taylor series, usable for calculating the exponential of a matrix. -### II.14.3 Enums (page 168) - -Enum support for bool as underlying type was deleted (see [issue 79224](https://github.com/dotnet/runtime/issues/79224)). - ### II.15.2 Static, Instance and Virtual Methods (page 177) (Clarify first paragraph) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs index 5850fef8e1170c..c6aac1b7bfb967 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs @@ -42,7 +42,7 @@ private unsafe CorElementType InternalGetCorElementType() { null, null, - null, + (RuntimeType)typeof(bool), (RuntimeType)typeof(char), (RuntimeType)typeof(sbyte), (RuntimeType)typeof(byte), diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index 4f7a44eeb919db..4b7eb61e7a19ea 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -1595,14 +1595,14 @@ public void EnumConstraint_ThrowsArgumentException() Assert.Throws(() => Enum.GetValues(genericArgumentWithEnumConstraint)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] public void DeclaringEnumWithBoolUnderlyingTypeFailsInCoreclr() { var exception = Assert.Throws(GetBoolEnumType); Assert.Equal("System.Boolean is not a supported constant type.", exception.Message); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMonoRuntime))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMonoRuntime), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] public void DeclaringEnumWithBoolUnderlyingTypeFailsInMono() { var exception = Assert.Throws(GetBoolEnumType); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs index d919e4056e66ae..9220a0c328c570 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs @@ -43,6 +43,7 @@ public static void TestGetEnumUnderlyingType() AssertEquals(typeof(ulong), NoInline(typeof(ULongEnum).GetEnumUnderlyingType())); AssertEquals(typeof(char), NoInline(typeof(CharEnum).GetEnumUnderlyingType())); + AssertEquals(typeof(bool), NoInline(typeof(BoolEnum).GetEnumUnderlyingType())); AssertEquals(typeof(float), NoInline(typeof(FloatEnum).GetEnumUnderlyingType())); AssertEquals(typeof(double), NoInline(typeof(DoubleEnum).GetEnumUnderlyingType())); AssertEquals(typeof(nint), NoInline(typeof(IntPtrEnum).GetEnumUnderlyingType())); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs index 3f892b5c9df09d..7d0080cae4d1fb 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs @@ -120,6 +120,7 @@ public static int Main() IsFalse(typeof(SimpleStruct).IsEnum); IsTrue (typeof(SimpleEnum).IsEnum); IsTrue (typeof(CharEnum).IsEnum); + IsTrue(typeof(BoolEnum).IsEnum); IsTrue (typeof(FloatEnum).IsEnum); IsTrue (typeof(DoubleEnum).IsEnum); IsTrue (typeof(IntPtrEnum).IsEnum); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il b/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il index e3b5d797bf4d98..36f9f07dd8fdcf 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il +++ b/src/tests/JIT/Intrinsics/TypeIntrinsicsEnums.il @@ -11,6 +11,12 @@ .field public specialname rtspecialname char value__ } // end of class CharEnum +.class public auto ansi sealed BoolEnum + extends [System.Runtime]System.Enum +{ + .field public specialname rtspecialname bool value__ +} // end of class BoolEnum + .class public auto ansi sealed FloatEnum extends [System.Runtime]System.Enum { From c2809e33cc996343cc6e3dc76c59084944540610 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sat, 7 Jan 2023 23:01:17 +0100 Subject: [PATCH 10/13] forgotten code --- .../JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs | 1 + src/tests/JIT/Intrinsics/TypeIntrinsics.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs index 9220a0c328c570..93142921f1d73f 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.GetEnumUnderlyingType.cs @@ -22,6 +22,7 @@ public static void TestGetEnumUnderlyingType() AssertEquals(typeof(ulong), typeof(ULongEnum).GetEnumUnderlyingType()); AssertEquals(typeof(char), typeof(CharEnum).GetEnumUnderlyingType()); + AssertEquals(typeof(bool), typeof(BoolEnum).GetEnumUnderlyingType()); AssertEquals(typeof(float), typeof(FloatEnum).GetEnumUnderlyingType()); AssertEquals(typeof(double), typeof(DoubleEnum).GetEnumUnderlyingType()); AssertEquals(typeof(nint), typeof(IntPtrEnum).GetEnumUnderlyingType()); diff --git a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs index 7d0080cae4d1fb..c697abc879155e 100644 --- a/src/tests/JIT/Intrinsics/TypeIntrinsics.cs +++ b/src/tests/JIT/Intrinsics/TypeIntrinsics.cs @@ -120,7 +120,7 @@ public static int Main() IsFalse(typeof(SimpleStruct).IsEnum); IsTrue (typeof(SimpleEnum).IsEnum); IsTrue (typeof(CharEnum).IsEnum); - IsTrue(typeof(BoolEnum).IsEnum); + IsTrue (typeof(BoolEnum).IsEnum); IsTrue (typeof(FloatEnum).IsEnum); IsTrue (typeof(DoubleEnum).IsEnum); IsTrue (typeof(IntPtrEnum).IsEnum); From 5c597b1cef0bc64a7fc15cdce561d30f1d23810e Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Mon, 9 Jan 2023 22:09:35 +0100 Subject: [PATCH 11/13] rollback changes in the type loader --- .../src/System/Reflection/Emit/TypeBuilder.cs | 7 +------ .../tests/CustomAttributeBuilderTests.cs | 1 + .../ModuleBuilder/ModuleBuilderDefineEnum.cs | 1 + .../System.Runtime/tests/System/EnumTests.cs | 16 ++-------------- src/mono/mono/metadata/class.c | 1 + 5 files changed, 6 insertions(+), 20 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index ab2e370d5278d6..088d22b5a1983b 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -345,14 +345,9 @@ internal static unsafe void SetConstantValue(ModuleBuilder module, int tk, Type switch (corType) { - case CorElementType.ELEMENT_TYPE_BOOLEAN: - if (destType.IsEnum) - { - throw new ArgumentException(SR.Format(SR.Argument_ConstantNotSupported, type)); - } - goto case CorElementType.ELEMENT_TYPE_I1; case CorElementType.ELEMENT_TYPE_I1: case CorElementType.ELEMENT_TYPE_U1: + case CorElementType.ELEMENT_TYPE_BOOLEAN: case CorElementType.ELEMENT_TYPE_I2: case CorElementType.ELEMENT_TYPE_U2: case CorElementType.ELEMENT_TYPE_CHAR: diff --git a/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs b/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs index 47779e6f7fa364..d045b35d78c6c0 100644 --- a/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs +++ b/src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs @@ -647,6 +647,7 @@ public static IEnumerable InvalidAttributeTypes_TestData() if (PlatformDetection.IsRareEnumsSupported) { yield return new object[] { CreateEnum(typeof(char), 'a'), 'a' }; + yield return new object[] { CreateEnum(typeof(bool), false), true }; yield return new object[] { CreateEnum(typeof(float), 1.0f), 1.0f }; yield return new object[] { CreateEnum(typeof(double), 1.0), 1.0 }; yield return new object[] { CreateEnum(typeof(IntPtr)), (IntPtr)1 }; diff --git a/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs b/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs index 32d6723c71e416..6173ffd8570815 100644 --- a/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs +++ b/src/libraries/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineEnum.cs @@ -19,6 +19,7 @@ public static IEnumerable DefineEnum_TestData() yield return new object[] { "Name", TypeAttributes.Public, typeof(uint) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(long) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(char) }; + yield return new object[] { "Name", TypeAttributes.Public, typeof(bool) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(ulong) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(float) }; yield return new object[] { "Name", TypeAttributes.Public, typeof(double) }; diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index 4b7eb61e7a19ea..91247f5421e5e3 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -1215,6 +1215,7 @@ public static IEnumerable GetUnderlyingType_TestData() if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported) { yield return new object[] { s_charEnumType, typeof(char) }; + yield return new object[] { s_boolEnumType, typeof(bool) }; yield return new object[] { s_floatEnumType, typeof(float) }; yield return new object[] { s_doubleEnumType, typeof(double) }; yield return new object[] { s_intPtrEnumType, typeof(IntPtr) }; @@ -1595,20 +1596,6 @@ public void EnumConstraint_ThrowsArgumentException() Assert.Throws(() => Enum.GetValues(genericArgumentWithEnumConstraint)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - public void DeclaringEnumWithBoolUnderlyingTypeFailsInCoreclr() - { - var exception = Assert.Throws(GetBoolEnumType); - Assert.Equal("System.Boolean is not a supported constant type.", exception.Message); - } - - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMonoRuntime), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))] - public void DeclaringEnumWithBoolUnderlyingTypeFailsInMono() - { - var exception = Assert.Throws(GetBoolEnumType); - Assert.Equal("Not a valid enumeration", exception.Message); - } - public static IEnumerable ToString_Format_TestData() { // Format "D": the decimal equivalent of the value is returned. @@ -2349,6 +2336,7 @@ private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType) return module.DefineEnum("TestName_" + underlyingType.Name, TypeAttributes.Public, underlyingType); } + private static Type s_boolEnumType = GetBoolEnumType(); private static Type GetBoolEnumType() { EnumBuilder enumBuilder = GetNonRuntimeEnumTypeBuilder(typeof(bool)); diff --git a/src/mono/mono/metadata/class.c b/src/mono/mono/metadata/class.c index 22251455a0805c..3a04a9dee77c41 100644 --- a/src/mono/mono/metadata/class.c +++ b/src/mono/mono/metadata/class.c @@ -6494,6 +6494,7 @@ gboolean mono_type_is_valid_enum_basetype (MonoType * type) { switch (type->type) { case MONO_TYPE_I1: case MONO_TYPE_U1: + case MONO_TYPE_BOOLEAN: case MONO_TYPE_I2: case MONO_TYPE_U2: case MONO_TYPE_CHAR: From 61aa895665d055923e61b8875d7cce6a624be875 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Mon, 9 Jan 2023 22:17:39 +0100 Subject: [PATCH 12/13] rollback additionaml test --- .../tests/EnumBuilder/EnumBuilder.Methods.Tests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs b/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs index 386fd15e999743..32133e76b570eb 100644 --- a/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs +++ b/src/libraries/System.Reflection.Emit/tests/EnumBuilder/EnumBuilder.Methods.Tests.cs @@ -47,6 +47,9 @@ private static IEnumerable DefineLiteralTestDataCore() yield return new object[] { typeof(char), (char)0 }; yield return new object[] { typeof(char), (char)1 }; + yield return new object[] { typeof(bool), true }; + yield return new object[] { typeof(bool), false }; + yield return new object[] { typeof(float), 0f }; yield return new object[] { typeof(float), 1.1f }; From 965f27833a836084af4a302b5780f6c66fccfe78 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Tue, 10 Jan 2023 18:37:07 +0100 Subject: [PATCH 13/13] rollback code optimized by jit --- .../System.Private.CoreLib/src/System/Enum.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index 4101070b966dc7..b1997aac1be436 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -436,6 +436,9 @@ public bool HasFlag(Enum flag) } #if RARE_ENUMS + case CorElementType.ELEMENT_TYPE_BOOLEAN: + goto case CorElementType.ELEMENT_TYPE_U1; + case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2; @@ -1225,6 +1228,9 @@ public override bool Equals([NotNullWhen(true)] object? obj) return Unsafe.As(ref pThisValue) == Unsafe.As(ref pOtherValue); #if RARE_ENUMS + case CorElementType.ELEMENT_TYPE_BOOLEAN: + goto case CorElementType.ELEMENT_TYPE_U1; + case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2; @@ -1272,6 +1278,7 @@ public override int GetHashCode() CorElementType.ELEMENT_TYPE_I => Unsafe.As(ref data).GetHashCode(), CorElementType.ELEMENT_TYPE_U => Unsafe.As(ref data).GetHashCode(), CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As(ref data).GetHashCode(), + CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As(ref data).GetHashCode(), #endif _ => throw CreateUnknownEnumTypeException(), }; @@ -1325,6 +1332,9 @@ public int CompareTo(object? target) case CorElementType.ELEMENT_TYPE_R8: return Unsafe.As(ref pThisValue).CompareTo(Unsafe.As(ref pTargetValue)); + case CorElementType.ELEMENT_TYPE_BOOLEAN: + goto case CorElementType.ELEMENT_TYPE_U1; + case CorElementType.ELEMENT_TYPE_CHAR: goto case CorElementType.ELEMENT_TYPE_U2;