C++ PATCH for c++/61490 - qualified-id in friend function definition

Marek Polacek polacek@redhat.com
Mon Jun 17 21:44:00 GMT 2019


[class.friend]/6 says that when we define a function in a friend declaration,
the function name must be unqualified.  But we never made sure that's so.

For good measure, I'm also improving the location of the related diagnostic.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-06-17  Marek Polacek  <polacek@redhat.com>

	PR c++/61490 - qualified-id in friend function definition.
	* decl.c (grokdeclarator): Diagnose qualified-id in friend function
	definition.  Improve location for diagnostics of friend functions.

	* g++.dg/diagnostic/friend2.C: New test.
	* g++.dg/diagnostic/friend3.C: New test.

diff --git gcc/cp/decl.c gcc/cp/decl.c
index 0a3ef452536..efc49137cdc 100644
--- gcc/cp/decl.c
+++ gcc/cp/decl.c
@@ -11605,13 +11605,29 @@ grokdeclarator (const cp_declarator *declarator,
 		    friendp = 0;
 		  }
 		if (decl_context == NORMAL)
-		  error ("friend declaration not in class definition");
+		  error_at (declarator->id_loc,
+			    "friend declaration not in class definition");
 		if (current_function_decl && funcdef_flag)
 		  {
-		    error ("cannot define friend function %qs in a local "
-			   "class definition", name);
+		    error_at (declarator->id_loc,
+			      "cannot define friend function %qs in a local "
+			      "class definition", name);
 		    friendp = 0;
 		  }
+		/* [class.friend]/6: A function can be defined in a friend
+		   declaration if the function name is unqualified.  */
+		if (funcdef_flag && in_namespace)
+		  {
+		    if (in_namespace == global_namespace)
+		      error_at (declarator->id_loc,
+				"friend function definition %qs cannot have "
+				"a name qualified with %<::%>", name);
+		    else
+		      error_at (declarator->id_loc,
+				"friend function definition %qs cannot have "
+				"a name qualified with %<%D::%>", name,
+				in_namespace);
+		  }
 	      }
 	    else if (ctype && sfk == sfk_conversion)
 	      {
diff --git gcc/testsuite/g++.dg/diagnostic/friend2.C gcc/testsuite/g++.dg/diagnostic/friend2.C
new file mode 100644
index 00000000000..4f4ada8bc16
--- /dev/null
+++ gcc/testsuite/g++.dg/diagnostic/friend2.C
@@ -0,0 +1,10 @@
+// PR c++/61490
+// { dg-do compile }
+
+namespace N { void f (); }
+void f2 ();
+
+struct A {
+  friend void N::f () { } // { dg-error "15:friend function definition 'f' cannot have a name qualified with 'N::'" }
+  friend void ::f2 () { } // { dg-error "15:friend function definition 'f2' cannot have a name qualified with '::'" }
+};
diff --git gcc/testsuite/g++.dg/diagnostic/friend3.C gcc/testsuite/g++.dg/diagnostic/friend3.C
new file mode 100644
index 00000000000..574d7caa5fb
--- /dev/null
+++ gcc/testsuite/g++.dg/diagnostic/friend3.C
@@ -0,0 +1,9 @@
+// { dg-do compile }
+
+void
+fn ()
+{
+  struct S {
+    friend void bar () { } // { dg-error "17:cannot define friend function 'bar' in a local class definition" }
+  };
+}



More information about the Gcc-patches mailing list