Convert literals to constants when reducing literal value
authorMarek Safar <marek.safar@gmail.com>
Fri, 30 Mar 2012 14:00:01 +0000 (15:00 +0100)
committerMarek Safar <marek.safar@gmail.com>
Fri, 30 Mar 2012 14:14:11 +0000 (15:14 +0100)
mcs/mcs/constant.cs
mcs/mcs/expression.cs
mcs/tests/test-129.cs
mcs/tests/ver-il-net_4_5.xml

index af0243f46c22751e64c85631596aa1165da2dc12..3cf689ddd81fc60b9fbafbda936eb4733fb4fc43 100644 (file)
@@ -258,8 +258,7 @@ namespace Mono.CSharp {
                {
                        try {
                                return TryReduceConstant (ec, target_type);
-                       }
-                       catch (OverflowException) {
+                       } catch (OverflowException) {
                                if (ec.ConstantCheckState && Type.BuiltinType != BuiltinTypeSpec.Type.Decimal) {
                                        ec.Report.Error (221, loc,
                                                "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
@@ -274,8 +273,15 @@ namespace Mono.CSharp {
 
                Constant TryReduceConstant (ResolveContext ec, TypeSpec target_type)
                {
-                       if (Type == target_type)
+                       if (Type == target_type) {
+                               //
+                               // Reducing literal value produces a new constant. Syntactically 10 is not same as (int)10 
+                               //
+                               if (IsLiteral)
+                                       return CreateConstantFromValue (target_type, GetValue (), loc);
+
                                return this;
+                       }
 
                        Constant c;
                        if (target_type.IsEnum) {
index 133a7f7cb230b75f4c28842030bb6120691cde1a..db9babed55c3a22ca2cecd8a07879acf5869e195 100644 (file)
@@ -1701,12 +1701,13 @@ namespace Mono.CSharp
                        eclass = ExprClass.Value;
                        
                        Constant c = expr as Constant;
-                       Expression res = c != null ? c.TryReduce (ec, type) : null;
-
-                       if (res == null) {
-                               res = Convert.ExplicitConversion (ec, expr, type, loc);
+                       if (c != null) {
+                               c = c.TryReduce (ec, type);
+                               if (c != null)
+                                       return c;
                        }
 
+                       var res = Convert.ExplicitConversion (ec, expr, type, loc);
                        if (res == expr)
                                return EmptyCast.Create (res, type);
 
index ebe321bb468d417687cb4fc4c72f1ac32382401a..92fd6914b4518969ea02a0da30d313bafaebd5a5 100644 (file)
@@ -22,7 +22,9 @@ class X {
                        return 2;
 
                uint ui = (1);
-
+               byte b1 = (int)(0x30);
+               byte b2 = (int)0x30;
+               
                return 0;
        }
 }
index c8e8840171944092bf3e3c081321de693542a8d6..1aec49eb4182032e9c8a49aa09e0bda111dc5ec6 100644 (file)
   <test name="test-129.cs">
     <type name="X">
       <method name="Int32 Main()" attrs="145">
-        <size>148</size>
+        <size>156</size>
       </method>
       <method name="Void .ctor()" attrs="6278">
         <size>7</size>