re PR c++/19989 (Inconsistency with zero-sized arrays)
authorJosh Conner <jconner@apple.com>
Fri, 4 Nov 2005 01:23:22 +0000 (01:23 +0000)
committerJosh Conner <jconner@gcc.gnu.org>
Fri, 4 Nov 2005 01:23:22 +0000 (01:23 +0000)
PR c++/19989
cp/pt.c (tsubst): Accept zero-length array if tf_error is set
in complain flags.  Change error message for negative-
length array.
testsuite/g++.dg/ext/array2.C: New test.
testsuite/g++.dg/template/dependent-name3.C: New test.
testsuite/g++.dg/template/dependent-name4.C: New test.
testsuite/g++.dg/template/sfinae2.C: New test.

From-SVN: r106468

gcc/cp/ChangeLog
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/array2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/dependent-name3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/dependent-name4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/sfinae2.C [new file with mode: 0644]

index a270fba..2d82433 100644 (file)
@@ -1,3 +1,10 @@
+2005-11-03  Josh Conner  <jconner@apple.com>
+
+       PR c++/19989
+       pt.c (tsubst): Accept zero-length array if tf_error is set
+       in complain flags.  Change error message for negative-
+       length array.
+
 2005-11-04  Joseph S. Myers  <joseph@codesourcery.com>
 
        * cp-tree.h (cp_cpp_error), error.c (cp_cpp_error): Take va_list*
index 1efd808..0490fd1 100644 (file)
@@ -7083,25 +7083,24 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
        max = tsubst_template_arg (omax, args, complain, in_decl);
        max = fold_decl_constant_value (max);
 
-       if (integer_zerop (omax))
-         {
-           /* Still allow an explicit array of size zero.  */
-           if (pedantic)
-             pedwarn ("creating array with size zero");
-         }
-       else if (integer_zerop (max)
-                || (TREE_CODE (max) == INTEGER_CST
-                    && INT_CST_LT (max, integer_zero_node)))
-         {
-           /* [temp.deduct]
+       /* [temp.deduct]
 
-              Type deduction may fail for any of the following
-              reasons:
+          Type deduction may fail for any of the following
+          reasons:
 
-                Attempting to create an array with a size that is
-                zero or negative.  */
+            Attempting to create an array with a size that is
+            zero or negative.  */
+       if (integer_zerop (max) && !(complain & tf_error))
+         /* We must fail if performing argument deduction (as
+            indicated by the state of complain), so that
+            another substitution can be found.  */
+         return error_mark_node;
+
+       else if (TREE_CODE (max) == INTEGER_CST
+                && INT_CST_LT (max, integer_zero_node))
+         {
            if (complain & tf_error)
-             error ("creating array with size zero (%qE)", max);
+             error ("creating array with negative size (%qE)", max);
 
            return error_mark_node;
          }
index 722318f..093e294 100644 (file)
@@ -1,3 +1,11 @@
+2005-11-03  Josh Conner  <jconner@apple.com>
+
+       PR c++/19989
+       g++.dg/ext/array2.C: New test.
+       g++.dg/template/dependent-name3.C: New test.
+       g++.dg/template/dependent-name4.C: New test.
+       g++.dg/template/sfinae2.C: New test.
+
 2005-11-03  Andrew Pinski  <pinskia@physics.uc.edu>
 
        PR preprocessor/22042
diff --git a/gcc/testsuite/g++.dg/ext/array2.C b/gcc/testsuite/g++.dg/ext/array2.C
new file mode 100644 (file)
index 0000000..2d645ca
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-do compile }
+
+// Avoid -pedantic-error default
+// { dg-options "" }
+// PR 19989 - dependent array of size 0 fails to compile.
+
+template<int I> struct A
+{
+  static const int zero = 0;
+};
+
+template<int N> struct B
+{
+  int x[A<N>::zero];
+};
+
+B<0> b;
diff --git a/gcc/testsuite/g++.dg/template/dependent-name3.C b/gcc/testsuite/g++.dg/template/dependent-name3.C
new file mode 100644 (file)
index 0000000..bbe6fb6
--- /dev/null
@@ -0,0 +1,17 @@
+// { dg-do compile }
+
+// Dependent arrays of invalid size generate appropriate error messages
+
+template<int I> struct A
+{
+  static const int zero = 0;
+  static const int minus_one = -1;
+};
+
+template<int N> struct B
+{
+  int x[A<N>::zero];       // { dg-error "zero" }
+  int y[A<N>::minus_one];  // { dg-error "negative" }
+};
+
+B<0> b;
diff --git a/gcc/testsuite/g++.dg/template/dependent-name4.C b/gcc/testsuite/g++.dg/template/dependent-name4.C
new file mode 100644 (file)
index 0000000..b2b5814
--- /dev/null
@@ -0,0 +1,15 @@
+// { dg-do compile }
+
+// Dependent arrays of invalid size cause template instantiation failure.
+
+// We'll get an error message (duplicate matching templates) if the first
+//  pattern is incorrectly allowed to match.
+
+template<int M> void foobar (int (*) [M] = 0 );
+template<int M> void foobar ( );
+
+void fn (void)
+{
+  foobar<0>();
+  foobar<-1>();
+}
diff --git a/gcc/testsuite/g++.dg/template/sfinae2.C b/gcc/testsuite/g++.dg/template/sfinae2.C
new file mode 100644 (file)
index 0000000..89880a8
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/19989
+// Don't instantiate a function template if it would generate an
+//   array of size zero.
+
+// { dg-do compile }
+
+template<int T> struct cl {
+  const static int value = T;
+};
+
+template<int I> void fn (char (*) [cl<I>::value] = 0 );
+
+void foo (void)
+{
+  fn<0> ();  // { dg-error "no matching function" }
+}
+