Hi,

for this kind of code, using the GNU zero-size array extension:

  int a[][0] = {0};

we end up looping forever in the reshape_init_array_1 loop because it calls reshape_init_r -> reshape_init_array -> reshape_init_array_1 which returns early a CONSTRUCTOR with no error. Having considered various solutions, I'm proposing to break the endless loop in reshape_init_array, where we have information about the type of the zero-size array and we can easily provide an accurate error message.

Tested x86_64-linux.

Thanks,
Paolo.

//////////////////////


/cp
2012-10-17  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/54501
        * decl.c (reshape_init_array): Check for zero-size arrays.

/testsuite
2012-10-17  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/54501
        * g++.dg/init/array30.C: New.
        * g++.dg/init/array0.C: Adjust.
        * g++.dg/parse/pr43765.C: Likewise.
Index: cp/decl.c
===================================================================
--- cp/decl.c   (revision 192527)
+++ cp/decl.c   (working copy)
@@ -5068,6 +5068,13 @@ reshape_init_array (tree type, reshape_iter *d, ts
   if (TYPE_DOMAIN (type))
     max_index = array_type_nelts (type);
 
+  if (max_index && integer_all_onesp (max_index))
+    {
+      if (complain & tf_error)
+       error ("initializers provided for zero-size array of type %qT", type);
+      return error_mark_node;
+    }
+
   return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
 }
 
Index: testsuite/g++.dg/init/array0.C
===================================================================
--- testsuite/g++.dg/init/array0.C      (revision 192527)
+++ testsuite/g++.dg/init/array0.C      (working copy)
@@ -8,5 +8,5 @@ void foo()
     unsigned char dir;
     int data[0];
   } yanito;
-  static const yanito horse = { 1,  { 2,  3 }  }; // { dg-error "too many" }
+  static const yanito horse = { 1,  { 2,  3 }  }; // { dg-error "zero-size" }
 }
Index: testsuite/g++.dg/init/array30.C
===================================================================
--- testsuite/g++.dg/init/array30.C     (revision 0)
+++ testsuite/g++.dg/init/array30.C     (working copy)
@@ -0,0 +1,7 @@
+// PR c++/54501
+// { dg-options "" }
+
+int main()
+{
+  int a[][0] = {0};  // { dg-error "zero-size" }
+}
Index: testsuite/g++.dg/parse/pr43765.C
===================================================================
--- testsuite/g++.dg/parse/pr43765.C    (revision 192527)
+++ testsuite/g++.dg/parse/pr43765.C    (working copy)
@@ -11,4 +11,4 @@ SomeType vals[] =
     {
         { values : temp, },
         0
-    };          // { dg-error "invalid" }
+    };          // { dg-error "zero-size" }

Reply via email to