From patchwork Fri Mar 17 18:44:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 66530 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 952683851402 for ; Fri, 17 Mar 2023 18:44:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 952683851402 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679078690; bh=dyWPr98NmKnvl+pdMeqgogpld15C9zeCfCLzvYcgEzA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IKIjvBvoq2qNT+Kl1RH3DBoHG7DxU+e0iFhfKWAsO6yF7KlbSo3EdazryLLh82emb kvecD0506uJLik8MSGwJ4KolzRoPhu4rT3vn12YA0wNrZyOpn9onJVM2UHGSkEDX/C CIOIHXbO3mw5uTwtnjF3kW5yNYpR6wRzZK4dc8rQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6A522385559F for ; Fri, 17 Mar 2023 18:44:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A522385559F Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-158-IpYuUfJbNrOvQbknpCBZyA-1; Fri, 17 Mar 2023 14:44:18 -0400 X-MC-Unique: IpYuUfJbNrOvQbknpCBZyA-1 Received: by mail-qt1-f198.google.com with SMTP id c11-20020ac85a8b000000b003bfdd43ac76so2959081qtc.5 for ; Fri, 17 Mar 2023 11:44:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679078658; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dyWPr98NmKnvl+pdMeqgogpld15C9zeCfCLzvYcgEzA=; b=fAB94d7qc/wO6yUyYgARGNTsx5fZbxC8mYcfv04GWrQFvRHt6xWx2zKvVJ/uQYKCi1 BU++/O/FUaChiIm2RU5cIt4sEP0zfo2DULn+WMebfi/vws3moiTRUB3ulmv7RNbrXk6l Ld1MXH24ZRRDF7KD2RUXAz0RatPQ1G5M3bJ3NQfIxlpcyZk8y4k+nXI5yE1PgocWJ9y/ 2xu2smVXD3/X8rkRaWri3YdaRQpthXRdsTPRCzHVx0FCUWv4WUbvbZqYOf8i8QWC9XHT l6wjq6UbmKlv316i31R7366P7q+VMEkhQEC2e0PZ/uV7Ipv6P5vWOPmtIwFFvexCOlJD Wjtg== X-Gm-Message-State: AO0yUKUHe1aeryCQEqmYqWIr0DuqqLsvF7g1fPtt2MDYHHc2oI8ak6AZ GB58KbsmMSgwruIhL6oOOuSdClJiXhpJWgrSbxg68uaBka2I5cYIKhpXrzwe7W3t9239zHWH29F zMkvBaV89tiVeJzxb9JwGwVK+Y9fWvSNFUuXP0b+vrzTv7Xl1k3WLpxioIlN5XxJWVTOYvGstWw == X-Received: by 2002:a05:622a:1987:b0:3b9:a4c8:d57a with SMTP id u7-20020a05622a198700b003b9a4c8d57amr13858240qtc.32.1679078658180; Fri, 17 Mar 2023 11:44:18 -0700 (PDT) X-Google-Smtp-Source: AK7set+iEExYOmWktPwJpSmOjarLMDzUwBXifa6o7VGkjEUIy3Qc4ZTRr12eUZkfxQN8Ry5S/B5S3Q== X-Received: by 2002:a05:622a:1987:b0:3b9:a4c8:d57a with SMTP id u7-20020a05622a198700b003b9a4c8d57amr13858203qtc.32.1679078657709; Fri, 17 Mar 2023 11:44:17 -0700 (PDT) Received: from jason.cygnus.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s128-20020a37a986000000b00741a984943fsm2180799qke.40.2023.03.17.11.44.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Mar 2023 11:44:17 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: namespace-scoped friend in local class [PR69410] Date: Fri, 17 Mar 2023 14:44:14 -0400 Message-Id: <20230317184414.1335691-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- 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.cc (do_friend): Handle namespace as scope argument. * decl.cc (grokdeclarator): Pass down in_namespace. gcc/testsuite/ChangeLog: * g++.dg/lookup/friend24.C: New test. --- gcc/cp/decl.cc | 3 ++- gcc/cp/friend.cc | 21 +++++++++++++++++---- gcc/testsuite/g++.dg/lookup/friend24.C | 9 +++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/friend24.C base-commit: 103d423f6ce72ccb03d55b7b1dfa2dabd5854371 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 1d1ae022606..f5785339c5a 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14393,7 +14393,8 @@ grokdeclarator (const cp_declarator *declarator, cplus_decl_attributes (&decl, *attrlist, 0); *attrlist = NULL_TREE; - decl = do_friend (ctype, unqualified_id, decl, + tree scope = ctype ? ctype : in_namespace; + decl = do_friend (scope, unqualified_id, decl, flags, funcdef_flag); return decl; } diff --git a/gcc/cp/friend.cc b/gcc/cp/friend.cc index 2d9bd4bb4aa..b36de2b20bb 100644 --- a/gcc/cp/friend.cc +++ b/gcc/cp/friend.cc @@ -487,19 +487,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, 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; + } /* Friend functions are unique, until proved otherwise. */ DECL_UNIQUE_FRIEND_P (decl) = 1; @@ -609,7 +622,7 @@ do_friend (tree ctype, tree declarator, tree decl, parameters. Instead, we call pushdecl when the class is instantiated. */ decl = push_template_decl (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, /*hiding=*/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; + }; +}