[mcs] Constant folding of enum constant and literal was using wrong operands. Fixes...
authorMarek Safar <marek.safar@gmail.com>
Thu, 1 Jun 2017 14:31:57 +0000 (16:31 +0200)
committerMarek Safar <marek.safar@gmail.com>
Thu, 1 Jun 2017 14:31:57 +0000 (16:31 +0200)
mcs/mcs/cfold.cs
mcs/tests/test-99.cs

index da85bf386b21c306f44105af857dec6196e43509..f19c1a400e33d6e0f676f93f4cdf15f3af415129 100644 (file)
@@ -469,27 +469,37 @@ namespace Mono.CSharp {
                                //
                                lc = left as EnumConstant;
                                rc = right as EnumConstant;
-                               if (lc != null || rc != null){
+                               if (lc != null || rc != null) {
+                                       TypeSpec res_type;
                                        if (lc == null) {
-                                               lc = rc;
-                                               lt = lc.Type;
-                                               right = left;
-                                       }
+                                               res_type = right.Type;
 
-                                       // U has to be implicitly convetible to E.base
-                                       right = right.ConvertImplicitly (lc.Child.Type);
-                                       if (right == null)
-                                               return null;
+                                               // Y has to be implicitly convertible to E.base
+                                               left = left.ConvertImplicitly (rc.Child.Type);
+                                               if (left == null)
+                                                       return null;
 
-                                       result = BinaryFold (ec, oper, lc.Child, right, loc);
+                                               right = rc.Child;
+                                       } else {
+                                               res_type = left.Type;
+
+                                               // U has to be implicitly convertible to E.base
+                                               right = right.ConvertImplicitly (lc.Child.Type);
+                                               if (right == null)
+                                                       return null;
+
+                                               left = lc.Child;
+                                       }
+
+                                       result = BinaryFold (ec, oper, left, right, loc);
                                        if (result == null)
                                                return null;
 
-                                       result = result.Reduce (ec, lt);
+                                       result = result.Reduce (ec, res_type);
                                        if (result == null)
                                                return null;
 
-                                       return new EnumConstant (result, lt);
+                                       return new EnumConstant (result, res_type);
                                }
 
                                if (left is NullLiteral && right is NullLiteral) {
index 2f2b7ab2c9ecf6efbb09b6de7a858a561f65258f..036850e34bbd396b89818a143a148ddff87b2ffd 100644 (file)
@@ -41,6 +41,10 @@ class X {
                        return 3;
                if (Test.A == Test.B)
                        return 4;
+
+               const A e2 = 3 - A.b;
+               if (e2 != A.a)
+                       return 5;
                
                return 0;
        }