diff mbox

C++ PATCH for c++/51248 (ICE with unusual enum)

Message ID 4EE920B7.4030804@redhat.com
State New
Headers show

Commit Message

Jason Merrill Dec. 14, 2011, 10:18 p.m. UTC
I used to think that there was no way to get a variant of an enum before 
the end of the first mention of it.  But when that's an enum-specifier 
that refers to a cv-variant of the type in one of the enumerator 
initializers, there is a way...

Tested x86_64-pc-linux-gnu, applying to 4.6 and trunk.
diff mbox

Patch

commit cfd4ae9cf58950ccd4f374bc0489e4a8b7bdffe7
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Dec 14 13:37:16 2011 -0500

    	PR c++/51248
    	* decl.c (copy_type_enum): Also update variants.
    	(finish_enum): Allow variants of complete enums.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 5a4e027..480d211 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -11908,15 +11908,19 @@  xref_basetypes (tree ref, tree base_list)
 static void
 copy_type_enum (tree dst, tree src)
 {
-  TYPE_MIN_VALUE (dst) = TYPE_MIN_VALUE (src);
-  TYPE_MAX_VALUE (dst) = TYPE_MAX_VALUE (src);
-  TYPE_SIZE (dst) = TYPE_SIZE (src);
-  TYPE_SIZE_UNIT (dst) = TYPE_SIZE_UNIT (src);
-  SET_TYPE_MODE (dst, TYPE_MODE (src));
-  TYPE_PRECISION (dst) = TYPE_PRECISION (src);
-  TYPE_ALIGN (dst) = TYPE_ALIGN (src);
-  TYPE_USER_ALIGN (dst) = TYPE_USER_ALIGN (src);
-  TYPE_UNSIGNED (dst) = TYPE_UNSIGNED (src);
+  tree t;
+  for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
+    {
+      TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
+      TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
+      TYPE_SIZE (t) = TYPE_SIZE (src);
+      TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
+      SET_TYPE_MODE (dst, TYPE_MODE (src));
+      TYPE_PRECISION (t) = TYPE_PRECISION (src);
+      TYPE_ALIGN (t) = TYPE_ALIGN (src);
+      TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
+      TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
+    }
 }
 
 /* Begin compiling the definition of an enumeration type.
@@ -12285,9 +12289,12 @@  finish_enum (tree enumtype)
       return;
     }
 
-  /* Here there should not be any variants of this type.  */
+  /* If this is a forward declaration, there should not be any variants,
+     though we can get a variant in the middle of an enum-specifier with
+     wacky code like 'enum E { e = sizeof(const E*) };'  */
   gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
-	      && !TYPE_NEXT_VARIANT (enumtype));
+	      && (TYPE_VALUES (enumtype)
+		  || !TYPE_NEXT_VARIANT (enumtype)));
 }
 
 /* Build and install a CONST_DECL for an enumeration constant of the
diff --git a/gcc/testsuite/g++.dg/other/enum2.C b/gcc/testsuite/g++.dg/other/enum2.C
new file mode 100644
index 0000000..3a28f25
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/enum2.C
@@ -0,0 +1,3 @@ 
+// PR c++/51248
+
+enum E { e = sizeof(const E*) };