From cdccf695f38b5973ce7b3c40d4cd2d915e617915 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 4 May 2024 13:32:15 -0700 Subject: [PATCH] Restore support for bool-backed enums too --- .../src/System/InvokeUtils.cs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) 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 7a30a50834a768..85b37326f45b3e 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs @@ -130,8 +130,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje EETypeElementType dstElementType = dstEEType->ElementType; EETypeElementType srcElementType = srcEEType->ElementType; - void* rawDstValue = null; - + bool boolValue; char charValue; sbyte sbyteValue; byte byteValue; @@ -144,8 +143,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje float floatValue; double doubleValue; - Unsafe.SkipInit(out dstObject); - + void* rawDstValue; ref byte rawSrcValue = ref RuntimeHelpers.GetRawData(srcObject); switch (dstElementType) @@ -154,11 +152,12 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Boolean: - dstObject = srcObject; + boolValue = Unsafe.As(ref rawSrcValue); break; default: goto Failure; } + rawDstValue = &boolValue; break; case EETypeElementType.Char: @@ -429,13 +428,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje goto Failure; } - // The only case where 'rawDstValue' is not set is for bool values, which set 'dstObject' - // directly. In all other cases, 'rawDstValue' is guaranteed to not be null. - if (rawDstValue != null) - { - dstObject = RuntimeImports.RhBox(dstEEType, ref *(byte*)rawDstValue); - } - + dstObject = RuntimeImports.RhBox(dstEEType, ref *(byte*)rawDstValue); Debug.Assert(dstObject.GetMethodTable() == dstEEType); return null;