From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679078660; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dyWPr98NmKnvl+pdMeqgogpld15C9zeCfCLzvYcgEzA=; b=iuOZ++pNw+HfgT1XXdFX+K/s7VCblg16L2CjKbuBnG49wDXRZilMqVDSFtjDprpHt97tW8 ibQ0AHeex1pvJIkSuAbuNrNnNcWw5/Wy9dyJO8Na1RnMB1ZllPChedGS++xlLBvI4u95Qo d72Yl14ktpvyxtc24IXt2v3jhLWOIVM= 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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; + }; +} base-commit: 103d423f6ce72ccb03d55b7b1dfa2dabd5854371 -- 2.31.1