On 6/17/19 5:43 PM, Marek Polacek wrote:
[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 <pola...@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);
For these two, maybe use the location of the "friend" token instead?
+ /* [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);
These I'd leave at the name location.
OK either way.
Jason