public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-11304] c++: namespace-scoped friend in local class [PR69410] Date: Fri, 21 Apr 2023 20:28:04 +0000 (GMT) [thread overview] Message-ID: <20230421202804.2B9B3385773F@sourceware.org> (raw) https://gcc.gnu.org/g:fc03893d2fead133ed336511ac00d75c10c5a88d commit r10-11304-gfc03893d2fead133ed336511ac00d75c10c5a88d Author: Jason Merrill <jason@redhat.com> Date: Fri Mar 17 09:43:48 2023 -0400 c++: namespace-scoped friend in local class [PR69410] do_friend was only considering class-qualified identifiers for the qualified-id case, but we also need to skip local scope when there's an explicit namespace scope. PR c++/69410 gcc/cp/ChangeLog: * friend.c (do_friend): Handle namespace as scope argument. * decl.c (grokdeclarator): Pass down in_namespace. gcc/testsuite/ChangeLog: * g++.dg/lookup/friend24.C: New test. Diff: --- gcc/cp/decl.c | 3 ++- gcc/cp/friend.c | 21 +++++++++++++++++---- gcc/testsuite/g++.dg/lookup/friend24.C | 9 +++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 6af7a97a7b9..4c7174395b6 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -13467,7 +13467,8 @@ grokdeclarator (const cp_declarator *declarator, return error_mark_node; } - decl = do_friend (ctype, unqualified_id, decl, + tree scope = ctype ? ctype : in_namespace; + decl = do_friend (scope, unqualified_id, decl, *attrlist, flags, funcdef_flag); return decl; diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c index fa20a9377a7..e6e2639be4a 100644 --- a/gcc/cp/friend.c +++ b/gcc/cp/friend.c @@ -467,19 +467,32 @@ make_friend_class (tree type, tree friend_type, bool complain) } /* Record DECL (a FUNCTION_DECL) as a friend of the - CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the + CURRENT_CLASS_TYPE. If DECL is a member function, SCOPE is the class of which it is a member, as named in the friend declaration. + If the friend declaration was explicitly namespace-qualified, SCOPE + is that namespace. DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the friend declaration is a definition of the function. FLAGS is as for grokclass fn. */ tree -do_friend (tree ctype, tree declarator, tree decl, +do_friend (tree scope, tree declarator, tree decl, tree attrlist, enum overload_flags flags, bool funcdef_flag) { gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); - gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype)); + + tree ctype = NULL_TREE; + tree in_namespace = NULL_TREE; + if (!scope) + ; + else if (MAYBE_CLASS_TYPE_P (scope)) + ctype = scope; + else + { + gcc_checking_assert (TREE_CODE (scope) == NAMESPACE_DECL); + in_namespace = scope; + } /* Every decl that gets here is a friend of something. */ DECL_FRIEND_P (decl) = 1; @@ -612,7 +625,7 @@ do_friend (tree ctype, tree declarator, tree decl, parameters. Instead, we call pushdecl when the class is instantiated. */ decl = push_template_decl_real (decl, /*is_friend=*/true); - else if (current_function_decl) + else if (current_function_decl && !in_namespace) /* pushdecl will check there's a local decl already. */ decl = pushdecl (decl, /*is_friend=*/true); else diff --git a/gcc/testsuite/g++.dg/lookup/friend24.C b/gcc/testsuite/g++.dg/lookup/friend24.C new file mode 100644 index 00000000000..9a45410d2a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/friend24.C @@ -0,0 +1,9 @@ +// PR c++/69410 + +void a(); +void f() { + class A { + friend void ::a(); + friend class Z; + }; +}
reply other threads:[~2023-04-21 20:28 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230421202804.2B9B3385773F@sourceware.org \ --to=jason@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).