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: PR c++/29733


This patch fixes PR c++/29733, an ICE-on-invalid.  We did not notice
when a variable in a template function is instantiated with a function
type.

Tested on x86_64-unknown-linux-gnu, applied on the mainline.  I will
apply to 4.1 and 4.2 after testing completes.

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

2006-12-04  Mark Mitchell  <mark@codesourcery.com>

	PR c++/29733
	* pt.c (tsubst_decl): Disallow variables of function type.

2006-12-04  Mark Mitchell  <mark@codesourcery.com>

	PR c++/29733
	* g++.dg/template/crash61.C: New test. 

Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c	(revision 119478)
+++ gcc/cp/pt.c	(working copy)
@@ -6946,6 +6946,27 @@ tsubst_decl (tree t, tree args, tsubst_f
 	    type = tsubst (TREE_TYPE (t), args, complain, in_decl);
 	    if (type == error_mark_node)
 	      return error_mark_node;
+	    if (TREE_CODE (type) == FUNCTION_TYPE)
+	      {
+		/* It may seem that this case cannot occur, since:
+
+		     typedef void f();
+		     void g() { f x; }
+
+		   declares a function, not a variable.  However:
+      
+		     typedef void f();
+		     template <typename T> void g() { T t; }
+		     template void g<f>();
+
+		   is an attempt to declare a variable with function
+		   type.  */
+		error ("variable %qD has function type",
+		       /* R is not yet sufficiently initialized, so we
+			  just use its name.  */
+		       DECL_NAME (r));
+		return error_mark_node;
+	      }
 	    type = complete_type (type);
 	    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
 	      = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
Index: gcc/testsuite/g++.dg/template/crash61.C
===================================================================
--- gcc/testsuite/g++.dg/template/crash61.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/crash61.C	(revision 0)
@@ -0,0 +1,11 @@
+// PR c++/29733
+
+template<typename T> void foo()
+{
+  T t = 0; // { dg-error "function type" }
+}
+
+void bar()
+{
+  foo<int()>();
+}


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