C++ PATCH: PR 28595

Mark Mitchell mark@codesourcery.com
Sat Aug 26 16:38:00 GMT 2006


This patch fixes a P2 ICE-on-invalid regression involving the use of a
static data member as an array bound in a template.  If the static
data member has no initializer, it is not a valid array bound -- but
we cannot diagnose that until instantiation time.

Tested on x86_64-unknown-linux-gnu, applied on the mainline.  I will
apply this to 4.1 when testing completes.

--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

2006-08-26  Mark Mitchell  <mark@codesourcery.com>

	PR c++/28595
	* pt.c (tsubst): Issue errors about attempts to create VLAs at
	template-instantiation time.

2006-08-26  Mark Mitchell  <mark@codesourcery.com>

	PR c++/28595
	* g++.dg/template/array15.C: New test.
	* g++.dg/template/crash2.C: Tweak error markers.

Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c	(revision 116430)
+++ gcc/cp/pt.c	(working copy)
@@ -7191,6 +7193,15 @@ tsubst (tree t, tree args, tsubst_flags_
 	max = tsubst_template_arg (omax, args, complain, in_decl);
 	max = fold_decl_constant_value (max);
 
+	if (TREE_CODE (max) != INTEGER_CST 
+	    && TREE_CODE (max) != TEMPLATE_PARM_INDEX
+	    && !at_function_scope_p ())
+	  {
+	    if (complain & tf_error)
+	      error ("array bound is not an integer constant");
+	    return error_mark_node;
+	  }
+
 	/* [temp.deduct]
 
 	   Type deduction may fail for any of the following
@@ -7203,7 +7214,6 @@ tsubst (tree t, tree args, tsubst_flags_
 	     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))
 	  {
Index: gcc/testsuite/g++.dg/template/crash2.C
===================================================================
--- gcc/testsuite/g++.dg/template/crash2.C	(revision 116430)
+++ gcc/testsuite/g++.dg/template/crash2.C	(working copy)
@@ -5,7 +5,7 @@ class A
 {
 public:
   static const EnumType size = max; // { dg-error "" }
-  int table[size];
+  int table[size]; // { dg-error "constant" }
 };
 template <class EnumType>
 const EnumType A<EnumType>::size;
Index: gcc/testsuite/g++.dg/template/array15.C
===================================================================
--- gcc/testsuite/g++.dg/template/array15.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/array15.C	(revision 0)
@@ -0,0 +1,13 @@
+// PR c++/28595
+
+template<int> struct A
+{
+  static const int i;
+};
+
+template<int N> struct B
+{
+  char c[A<N>::i], d; // { dg-error "constant" }
+};
+
+B<0> b;



More information about the Gcc-patches mailing list