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: 71390 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp501199wrt; Fri, 17 Mar 2023 11:45:07 -0700 (PDT) X-Google-Smtp-Source: AK7set/M5IF3pTpN4R6NFYeacyfq00Bz8Lx9mFPrAiMinAAo9ZTiA5TOU8plRSlqwKHHcRwhsUnb X-Received: by 2002:a50:fe98:0:b0:4af:81fb:4c72 with SMTP id d24-20020a50fe98000000b004af81fb4c72mr3845710edt.34.1679078707356; Fri, 17 Mar 2023 11:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679078707; cv=none; d=google.com; s=arc-20160816; b=UhpmSHhGFu3wOfaoMUqy22t1PlAUYpbAPMAmFo2ez1PFb4H7M9M2+YuGVgbHAmQ4Fe eKrH25/ZZY5AARMuVJpHcHd9cRGDTB0B0bkROwHE+StkuvCaC2IOjVefuzohljeLCrrs GoTh8NAkJglVrk+EYKi3E4Z07hnKaEIbqJNVuZX8WART6/1JX0l/eWp+TwyQPvPf3WE0 WGHH82qIoZZb3msE0JMgExDDWK45oxJL8Ef4gD4Tc0CaIpAxWuHIVZyrwgU60dmq7sT7 DrOx+2DonKE2yArsiT9Yynpy+yBPNNiQ0mp0e9zXNk2iACKX1iPwsMs15bFOh6HvP0wQ as/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=dyWPr98NmKnvl+pdMeqgogpld15C9zeCfCLzvYcgEzA=; b=Z1TSWajVIxgoxkVWQ/8GZiF5YmmSfk7QIC7GTHS6BQxCnQCy3sWj0c1XgT+MfUK6WW ANuXrc5kOeRc9TCQMFmlV9Clw6wWrjVvc0c30SyEgHyo4iFQLeoxL7x2S1jRYMrYxkhc JHm62jNl5ShcckXu8SgMisd4OIxnT+Sgc0TVBgr8dffq8BjIf6GslU4HexsFxLNMQDfj nL7DZb61Dl9YCY4G8PcXr7IwUaU5gZ1UIgG7N79j+NdBS31JWw5mquJkTuK/wN02kyVe fTPZZMo7zZGzvH1wIMmnu3bUbVVOk38P2mt3N6kWMHjmJ4rFhZ5If+dNDQbgp/9TbPXS 843w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=j7LE18NL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id e3-20020a50fb83000000b004ad7203a2c6si3338095edq.230.2023.03.17.11.45.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Mar 2023 11:45:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=j7LE18NL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 67A123851ABF for ; Fri, 17 Mar 2023 18:45:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67A123851ABF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679078706; bh=dyWPr98NmKnvl+pdMeqgogpld15C9zeCfCLzvYcgEzA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=j7LE18NLP1j9D6KXyAzOPZdfMy6d/1uomR+3XNQtk623dgUf9doWV2lJ3oyPonVbZ 3xZ+aN3aByYFjSKxjzraa2GN4006j01y0E6m+YoBGFu4UqqYzwgyFDGnCdkho/GYJB rrnm9Rsb68f6KDkInUh9lwnh6u8xwsLISimSlnxs= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760641634547238424?= X-GMAIL-MSGID: =?utf-8?q?1760641634547238424?= 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; + }; +}