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]

[C++ PATCH] Avoid ICE on invalid struct A { friend int A () {} } (PR c++/34488)


Hi!

The following ICE on invalid was introduced by
http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123312 (PR29077 fix),
where we now mark a decl with DECL_CONSTRUCTOR_P much earlier than we did
before and for invalid unqualified friend sfk_constructor this means ICE.
Similar invalid struct A { friend int ~A () {} }; doesn't crash since the
PR22293 fix, this patch fixes the constructor case similarly.  AFAIK the
only valid constructor in friend should be a qualified one, which will have
METHOD_TYPE, rather than FUNCTION_TYPE as in this invalid case.

Regtested on x86_64-linux, ok for trunk?

2007-12-17  Jakub Jelinek  <jakub@redhat.com>

	PR c++/34488
	* decl.c (grokdeclarator): Reject friend sfk_constructor
	FUNCTION_TYPE.

	* g++.dg/parse/friend7.C: New test.

--- gcc/cp/decl.c.jj	2007-11-26 22:14:07.000000000 +0100
+++ gcc/cp/decl.c	2007-12-17 14:56:49.000000000 +0100
@@ -8818,6 +8818,13 @@ grokdeclarator (const cp_declarator *dec
 		    return error_mark_node;
 		  }
 	      }
+	    else if (sfk == sfk_constructor && friendp)
+	      {
+		error ("expected qualified name in friend declaration "
+		       "for constructor %qD",
+		       id_declarator->u.id.unqualified_name);
+		return error_mark_node;
+	      }
 
 	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
 	    function_context = (ctype != NULL_TREE) ?
--- gcc/testsuite/g++.dg/parse/friend7.C.jj	2007-12-17 15:05:39.000000000 +0100
+++ gcc/testsuite/g++.dg/parse/friend7.C	2007-12-17 15:04:38.000000000 +0100
@@ -0,0 +1,37 @@
+// PR c++/34488
+// { dg-do compile }
+
+struct A
+{
+  A ();
+  ~A ();
+  A (const A &);
+};
+
+struct B
+{
+  friend A::A ();
+  friend A::~A ();
+  friend A::A (const A &);
+};
+
+struct C
+{
+  friend int C ();		// { dg-error "return type|in friend decl" }
+  friend int ~C ();		// { dg-error "return type|in friend decl" }
+  friend int C (const C &);	// { dg-error "return type|in friend decl" }
+};
+
+struct D
+{
+  friend int D () {}		// { dg-error "return type|in friend decl" }
+  friend int ~D () {}		// { dg-error "return type|in friend decl" }
+  friend int D (const D &) {}	// { dg-error "return type|in friend decl" }
+};
+
+struct E
+{
+  friend A::A () {}		// { dg-error "cannot define member" }
+  friend A::~A () {}		// { dg-error "cannot define member" }
+  friend A::A (const A &) {}	// { dg-error "cannot define member" }
+};

	Jakub


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