Use slow path for CallInstruction returning enum value (dotnet/corefx#40976)
authorCharles Stoner <chucks@microsoft.com>
Wed, 11 Sep 2019 00:31:41 +0000 (17:31 -0700)
committerStephen Toub <stoub@microsoft.com>
Wed, 11 Sep 2019 00:31:41 +0000 (20:31 -0400)
* Use slow path for CallInstruction returning enum value

* Fix assert

Commit migrated from https://github.com/dotnet/corefx/commit/f2daf45473553c19cc7b3dd4bb3f2033080a254c

src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/CallInstruction.Generated.cs
src/libraries/System.Linq.Expressions/tests/Call/CallTests.cs

index ce37e3c..3384b27 100644 (file)
@@ -42,6 +42,7 @@ namespace System.Linq.Expressions.Interpreter
                 return new ActionCallInstruction(target);
             }
 
+            if (t.IsEnum) return SlowCreate(target, pi);
             switch (t.GetTypeCode())
             {
                 case TypeCode.Object:
@@ -84,6 +85,7 @@ namespace System.Linq.Expressions.Interpreter
                 return new FuncCallInstruction<T0>(target);
             }
 
+            if (t.IsEnum) return SlowCreate(target, pi);
             switch (t.GetTypeCode())
             {
                 case TypeCode.Object:
@@ -126,6 +128,7 @@ namespace System.Linq.Expressions.Interpreter
                 return new FuncCallInstruction<T0, T1>(target);
             }
 
+            if (t.IsEnum) return SlowCreate(target, pi);
             switch (t.GetTypeCode())
             {
                 case TypeCode.Object:
index 60bc6ac..b67b1d8 100644 (file)
@@ -665,6 +665,37 @@ namespace System.Linq.Expressions.Tests
             }
         }
 
+        [Fact]
+        public static void EnumReturnType0()
+        {
+            Expression<Func<DayOfWeek[]>> expr = () => new[] { ToDayOfWeek0() };
+
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(false)()[0]);
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(true)()[0]);
+        }
+
+        [Fact]
+        public static void EnumReturnType1()
+        {
+            Expression<Func<DayOfWeek[]>> expr = () => new[] { ToDayOfWeek1(1) };
+
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(false)()[0]);
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(true)()[0]);
+        }
+
+        [Fact]
+        public static void EnumReturnType2()
+        {
+            Expression<Func<DayOfWeek[]>> expr = () => new[] { ToDayOfWeek2(0, 1) };
+
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(false)()[0]);
+            Assert.Equal(DayOfWeek.Monday, expr.Compile(true)()[0]);
+        }
+
+        private static DayOfWeek ToDayOfWeek0() => DayOfWeek.Monday;
+        private static DayOfWeek ToDayOfWeek1(int i) => (DayOfWeek)i;
+        private static DayOfWeek ToDayOfWeek2(int i, int j) => (DayOfWeek)(i + j);
+
         public class GenericClass<T>
         {
             public static void NonGenericMethod() { }