[mcs] Easy restrictions on enum base type epxression
authorMarek Safar <marek.safar@gmail.com>
Wed, 20 Aug 2014 11:16:55 +0000 (13:16 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 20 Aug 2014 11:16:55 +0000 (13:16 +0200)
mcs/errors/cs1008-2.cs [deleted file]
mcs/mcs/cs-parser.jay
mcs/mcs/enum.cs
mcs/tests/test-905.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_5.xml

diff --git a/mcs/errors/cs1008-2.cs b/mcs/errors/cs1008-2.cs
deleted file mode 100644 (file)
index 326e486..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// CS1008: Type byte, sbyte, short, ushort, int, uint, long or ulong expected
-// Line: 3
-enum Test : System.Int16
-{
-       One
-}
-
-class X
-{
-       static void Main ()
-       { }
-}
-
-
index 26b6a80cbbbc5e1328a9f471d25d635975fbc197..b3cd88ad6c4b30610eb56753a33b6f7a9032645d 100644 (file)
@@ -2754,10 +2754,6 @@ opt_enum_base
        : /* empty */
        | COLON type
         {
-               var te = $2 as TypeExpression;
-               if (te == null || !EnumSpec.IsValidUnderlyingType (te.Type)) {
-                       Enum.Error_1008 (GetLocation ($2), report);
-               }
                $$ = $2;
         }
        | COLON error
index 943c55ba6737aa2d12e9a889130d3b090e32d055..3e1c3fc653091cd47f86df6b839d92c96a296127 100644 (file)
@@ -64,11 +64,12 @@ namespace Mono.CSharp {
                        if (expr is EnumConstant)
                                expr = ((EnumConstant) expr).Child;
 
-                       var underlying = ((Enum) Parent).UnderlyingType;
+                       var en = (Enum)Parent;
+                       var underlying = en.UnderlyingType;
                        if (expr != null) {
                                expr = expr.ImplicitConversionRequired (rc, underlying);
                                if (expr != null && !IsValidEnumType (expr.Type)) {
-                                       Enum.Error_1008 (Location, Report);
+                                       en.Error_UnderlyingType (Location);
                                        expr = null;
                                }
                        }
@@ -217,7 +218,7 @@ namespace Mono.CSharp {
                        AddMember (em);
                }
 
-               public static void Error_1008 (Location loc, Report Report)
+               public void Error_UnderlyingType (Location loc)
                {
                        Report.Error (1008, loc,
                                "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
@@ -225,7 +226,21 @@ namespace Mono.CSharp {
 
                protected override void DoDefineContainer ()
                {
-                       ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
+                       TypeSpec ut;
+                       if (underlying_type_expr != null) {
+                               ut = underlying_type_expr.ResolveAsType (this);
+                               if (!EnumSpec.IsValidUnderlyingType (ut)) {
+                                       Error_UnderlyingType (underlying_type_expr.Location);
+                                       ut = null;
+                               }
+                       } else {
+                               ut = null;
+                       }
+
+                       if (ut == null)
+                               ut = Compiler.BuiltinTypes.Int;
+
+                       ((EnumSpec) spec).UnderlyingType = ut;
 
                        TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
                                FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
diff --git a/mcs/tests/test-905.cs b/mcs/tests/test-905.cs
new file mode 100644 (file)
index 0000000..ceff84a
--- /dev/null
@@ -0,0 +1,13 @@
+using System;
+using i = System.Int16;
+
+enum E : i
+{
+}
+
+class X
+{
+       public static void Main ()
+       {
+       }
+}
\ No newline at end of file
index 6242ebb7c44c75872fdc62178be14cfc961ebd03..5ae996d6732b1e4ce5873022a9da872203d249da 100644 (file)
       </method>\r
     </type>\r
   </test>\r
+  <test name="test-905.cs">\r
+    <type name="X">\r
+      <method name="Void Main()" attrs="150">\r
+        <size>2</size>\r
+      </method>\r
+      <method name="Void .ctor()" attrs="6278">\r
+        <size>7</size>\r
+      </method>\r
+    </type>\r
+  </test>\r
   <test name="test-91.cs">\r
     <type name="Abstract">\r
       <method name="Void .ctor()" attrs="6276">\r