This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR c++/31517: ICE on invalid initialization of static member of a template class


Hi all.

The following invalid snippet currently gives an ICE:

=== cut here ===
template <typename> struct A
{
  static const int i=0;
};

template <typename T> const int A<T>::i = 0=0;
=== cut here ===

We call 'value_dependent_expression_p' with the "second" definition of 
A<T>::i (0=0), which is a MODOP_EXPR with a NOP_EXPR (with no operand) as 
second operand (a MODOP_EXPR's second operand only carries a tree code). This 
results in the call of 'value_dependent_expression_p' with each of the 
MODOP_EXPR's operands.

When working on the second one, 'value_dependent_expression_p' calls itself 
with the NOP_EXPR's operand, that is NULL_TREE, and we end up with a 
segmentation fault.

(One can note that the same testcase but without the in-class definition does 
not segfault because in that case, the decl for A<T>::i is not a 
DECL_INTEGRAL_CONSTANT_VAR_P, so we skip the call to 
'value_dependent_expression_p' in decl.c:5130)

The attached patch fixes this PR by teaching 'value_dependent_expression_p' to 
handle MODOP_EXPR properly, that is only call itself with the 0-th and 2-nd 
operands.

I've successfully regtested it on i686-pc-linux-gnu. Is it OK for mainline? 
For the 4.2 and 4.1 branches?

Thanks in advance,
Simon

:ADDPATCH c++:
2007-04-17  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/31517
	* pt.c (value_dependent_expression_p): Handle MODOP_EXPRs.
Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c	(revision 123881)
+++ gcc/cp/pt.c	(working copy)
@@ -14949,6 +14949,10 @@ value_dependent_expression_p (tree expre
 		|| (type2 ? dependent_type_p (type2) : false));
       }
 
+    case MODOP_EXPR:
+      return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
+	      || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
+
     default:
       /* A constant expression is value-dependent if any subexpression is
 	 value-dependent.  */
2007-04-17  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/31517
	* g++.dg/template/init7.C: New test.
/* PR c++/31517. This used to ICE.  */
/* { dg-do "compile" } */

template<typename> struct A
{
  static const int i=0;
};

template<typename T> const int A<T>::i = 0=0; /* { dg-error "duplicate initialization" } */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]