diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs index 5cd7fd59185d2..bb7aaa108f62b 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs @@ -673,11 +673,6 @@ private static void EmitNumericConversion(this ILGenerator il, Type typeFrom, Ty } else { - if (tf == TypeCode.Byte) - { - return; - } - convCode = OpCodes.Conv_I1; } @@ -689,11 +684,6 @@ private static void EmitNumericConversion(this ILGenerator il, Type typeFrom, Ty } else { - if (tf == TypeCode.SByte) - { - return; - } - convCode = OpCodes.Conv_U1; } @@ -704,14 +694,6 @@ private static void EmitNumericConversion(this ILGenerator il, Type typeFrom, Ty case TypeCode.SByte: case TypeCode.Byte: return; - case TypeCode.Char: - case TypeCode.UInt16: - if (!isChecked) - { - return; - } - - break; } convCode = isChecked @@ -726,14 +708,6 @@ private static void EmitNumericConversion(this ILGenerator il, Type typeFrom, Ty case TypeCode.Char: case TypeCode.UInt16: return; - case TypeCode.SByte: - case TypeCode.Int16: - if (!isChecked) - { - return; - } - - break; } convCode = isChecked diff --git a/src/libraries/System.Linq.Expressions/tests/Convert/ConvertTests.cs b/src/libraries/System.Linq.Expressions/tests/Convert/ConvertTests.cs index eb7d20b6ac771..dd9c274f0a3ab 100644 --- a/src/libraries/System.Linq.Expressions/tests/Convert/ConvertTests.cs +++ b/src/libraries/System.Linq.Expressions/tests/Convert/ConvertTests.cs @@ -182,6 +182,15 @@ public static void ConvertByteToSByteTest(bool useInterpreter) } } + [Theory, ClassData(typeof(CompilationTypes))] + public static void ConvertByteToSByteRetIntTest(bool useInterpreter) + { + foreach (byte value in new byte[] { 0, 1, byte.MaxValue }) + { + VerifyByteToSByteRetInt(value, useInterpreter); + } + } + [Theory, ClassData(typeof(CompilationTypes))] public static void ConvertByteToNullableSByteTest(bool useInterpreter) { @@ -4412,6 +4421,15 @@ public static void ConvertSByteToByteTest(bool useInterpreter) } } + [Theory, ClassData(typeof(CompilationTypes))] + public static void ConvertSByteToByteRetIntTest(bool useInterpreter) + { + foreach (sbyte value in new sbyte[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue }) + { + VerifySByteToByteRetInt(value, useInterpreter); + } + } + [Theory, ClassData(typeof(CompilationTypes))] public static void ConvertSByteToNullableByteTest(bool useInterpreter) { @@ -5150,6 +5168,15 @@ public static void ConvertShortToUShortTest(bool useInterpreter) } } + [Theory, ClassData(typeof(CompilationTypes))] + public static void ConvertShortToUShortRetIntTest(bool useInterpreter) + { + foreach (short value in new short[] { 0, 1, -1, short.MinValue, short.MaxValue }) + { + VerifyShortToUShortRetInt(value, useInterpreter); + } + } + [Theory, ClassData(typeof(CompilationTypes))] public static void ConvertShortToNullableUShortTest(bool useInterpreter) { @@ -6608,6 +6635,15 @@ public static void ConvertUShortToShortTest(bool useInterpreter) } } + [Theory, ClassData(typeof(CompilationTypes))] + public static void ConvertUShortToShortRetIntTest(bool useInterpreter) + { + foreach (ushort value in new ushort[] { 0, 1, ushort.MaxValue }) + { + VerifyUShortToShortRetInt(value, useInterpreter); + } + } + [Theory, ClassData(typeof(CompilationTypes))] public static void ConvertUShortToNullableShortTest(bool useInterpreter) { @@ -7136,6 +7172,17 @@ private static void VerifyByteToSByte(byte value, bool useInterpreter) Assert.Equal(unchecked((sbyte)value), f()); } + private static void VerifyByteToSByteRetInt(byte value, bool useInterpreter) + { + Expression> e = + Expression.Lambda>( + Expression.Convert(Expression.Convert(Expression.Constant(value, typeof(byte)), typeof(sbyte)), typeof(int)), + Enumerable.Empty()); + Func f = e.Compile(useInterpreter); + + Assert.Equal((int)unchecked((sbyte)value), f()); + } + private static void VerifyByteToNullableSByte(byte value, bool useInterpreter) { Expression> e = @@ -13178,6 +13225,17 @@ private static void VerifySByteToByte(sbyte value, bool useInterpreter) Assert.Equal(unchecked((byte)value), f()); } + private static void VerifySByteToByteRetInt(sbyte value, bool useInterpreter) + { + Expression> e = + Expression.Lambda>( + Expression.Convert(Expression.Convert(Expression.Constant(value, typeof(sbyte)), typeof(byte)), typeof(int)), + Enumerable.Empty()); + Func f = e.Compile(useInterpreter); + + Assert.Equal((int)unchecked((byte)value), f()); + } + private static void VerifySByteToNullableByte(sbyte value, bool useInterpreter) { Expression> e = @@ -14122,6 +14180,17 @@ private static void VerifyShortToUShort(short value, bool useInterpreter) Assert.Equal(unchecked((ushort)value), f()); } + private static void VerifyShortToUShortRetInt(short value, bool useInterpreter) + { + Expression> e = + Expression.Lambda>( + Expression.Convert(Expression.Convert(Expression.Constant(value, typeof(short)), typeof(ushort)), typeof(int)), + Enumerable.Empty()); + Func f = e.Compile(useInterpreter); + + Assert.Equal((int)unchecked((ushort)value), f()); + } + private static void VerifyShortToNullableUShort(short value, bool useInterpreter) { Expression> e = @@ -16030,6 +16099,17 @@ private static void VerifyUShortToShort(ushort value, bool useInterpreter) Assert.Equal(unchecked((short)value), f()); } + private static void VerifyUShortToShortRetInt(ushort value, bool useInterpreter) + { + Expression> e = + Expression.Lambda>( + Expression.Convert(Expression.Convert(Expression.Constant(value, typeof(ushort)), typeof(short)), typeof(int)), + Enumerable.Empty()); + Func f = e.Compile(useInterpreter); + + Assert.Equal((int)unchecked((short)value), f()); + } + private static void VerifyUShortToNullableShort(ushort value, bool useInterpreter) { Expression> e =