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]

Re: [patch] Fix PR c++/22233,ICE on invalid number of template parameters


On 22 Aug, Mark Mitchell wrote:
> Volker Reichelt wrote:
>> Consider the following invalid testcase:
>> 
>>   template<int> struct A
>>   {
>>     A();
>>   };
>> 
>>   template<int N, char> A<N>::A() {}
>> 
>>   A<0> a;
>> 
>> After a suitable error message [snip]
>> we get an ICE when instantiating A:

[snip]

>> The patch fixes that by returning an error_mark_node from
>> push_template_decl_real when the non-matching template arguments
>> are encountered. This prevents that the invalid declaration is
>> considered at instantiation time.
>> 
>> Bootstrapped and regtested on i686-pc-linux-gnu.
>> 
>> Ok for mainline, 4.0 branch and 3.4 branch (since this is a regression)?
> 
> OK.

Committed to mainline and 4.0 branch.
I didn't commit it to the 3.4 branch since the fix requires an
additional patchlet which I forgot to attach in my original mail.
Sorry!

Attached is the complete patch for the 3.4 branch. The testcase and
the part in pt.c are the same as for the 4.0 branch and mainline. But
we also need a small fix in decl.c to deal with the error_mark_node
returned by pt.c. (This code snippet is already present in mainline
and the 4.0 branch. It fixed PR 19980, which didn't show on the
3.4 branch.)

Bootstrapped and regtested on i686-pc-linux-gnu.
Ok for to commit to the 3.4 branch?

Regards,
Volker


2005-08-22  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c++/22233
	* pt.c (push_template_decl_real): Return error_mark_node if the
	number of template parameters does not match previous definition.
	* decl.c (start_function): Handle error_mark_node returned by
	push_template_decl.

===================================================================
--- gcc/gcc/cp/decl.c	14 Jun 2005 16:55:45 -0000	1.1174.2.37
+++ gcc/gcc/cp/decl.c	22 Aug 2005 10:52:21 -0000
@@ -10317,7 +10317,11 @@ start_function (tree declspecs, tree dec
      class scope, current_class_type will be NULL_TREE until set above
      by push_nested_class.)  */
   if (processing_template_decl)
-    decl1 = push_template_decl (decl1);
+    {
+      tree newdecl1 = push_template_decl (decl1);
+      if (newdecl1 != error_mark_node)
+        decl1 = newdecl1;
+    }
 
   /* We are now in the scope of the function being defined.  */
   current_function_decl = decl1;
===================================================================
--- gcc/gcc/cp/pt.c	28 Jul 2005 10:22:15 -0000	1.816.2.57
+++ gcc/gcc/cp/pt.c	22 Aug 2005 10:49:16 -0000
@@ -2978,6 +2978,7 @@ push_template_decl_real (tree decl, int 
 		  error ("got %d template parameters for `%#T'",
 			    TREE_VEC_LENGTH (a), current);
 		error ("  but %d required", TREE_VEC_LENGTH (t));
+                return error_mark_node;
 	      }
 
 	    /* Perhaps we should also check that the parms are used in the
===================================================================

2005-08-22  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c++/22233
	* g++.dg/template/param1.C: New test.

===================================================================
--- gcc/gcc/testsuite/g++.dg/template/param1.C	2003-09-23 19:59:22 +0200
+++ gcc/gcc/testsuite/g++.dg/template/param1.C	2005-08-17 19:54:38 +0200
@@ -0,0 +1,12 @@
+// PR c++/22233
+// Origin: Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
+// { dg-do compile }
+
+template<int> struct A
+{
+  A();
+};
+
+template<int N, char> A<N>::A() {}  // { dg-error "got 2|but 1 required" }
+
+A<0> a;
===================================================================



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