From patchwork Wed Jun 8 20:38:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1640883 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=WZgwL4JZ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LJJwQ27KGz9sFr for ; Thu, 9 Jun 2022 06:38:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D787E38BD25B for ; Wed, 8 Jun 2022 20:38:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D787E38BD25B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654720721; bh=H/AVk86XbgdS76p2nid//ULA0BEmthgBtAUivo+zUMc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=WZgwL4JZxYI8grq4tTQw+2WRDn6KN83hELKrNKBa4sEZmSWZMhxFuotou1AqIestH Ndb+hjxSRG2saFej4SYbxYRhy7a3pEBpQr88OA0nu+LvMzg4BcAyDPmZ2uXElFO6aR Ls4JUNmqANvn1DrJ2eRTeDZ3EaKQWpWI5Y3AofHg= 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 C821C38BD247 for ; Wed, 8 Jun 2022 20:38:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C821C38BD247 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-272-sv2GbEsvOwC7qW4hwDuVCg-1; Wed, 08 Jun 2022 16:38:18 -0400 X-MC-Unique: sv2GbEsvOwC7qW4hwDuVCg-1 Received: by mail-qv1-f69.google.com with SMTP id r14-20020ad4576e000000b0046bbacd783bso3860937qvx.14 for ; Wed, 08 Jun 2022 13:38:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=H/AVk86XbgdS76p2nid//ULA0BEmthgBtAUivo+zUMc=; b=7b26tDnwjZ2Ciyp6Nlt7c8fx7kpWKUjBZ7FAHIWKLryQ6hJYD8RnjRVUZ968sZplah LaFeoNbNcx0dlA+bU6SenGBOm8Jiq0xcI46EFbsQvcfakIbdBPOP2BvlBWGjChBLciaL 71TXVEcr/T7mMVImQQMKJLPbKYGUeeaabiJtQIDnTNqf4OjfGcwTplMaSHfNC4G04BiG ovyHJY2vJIcwUCgFdJBu019RqOnSYfDnh85Yw4mxo7o4XPC2giyIf0b2i0YvtUh3DTH/ CXAmeubFt9Ay3DpaF2c9UXEgcxj51g8+jEiv4uk+IB8bVBhQ9gfIv+t416zpUEXEiACm Fehw== X-Gm-Message-State: AOAM533wW5tN4PG6Noh8PkV/9xyqwiTh3FncsbwNnKbzqAiYUIoR51m4 tYIi17mp1Tzr38K569jXqlYaTvAMa0BAII7eYnHXx8As0pX+8RS1+57uF8Kr2d7iPBcuMUmLK3t yc2KRhNYh/ZZtwhmm6YQbvSza/oFbKxfMNd/DCiqiBU+Yh+9etJwVQzc3Pcr62I8nrA== X-Received: by 2002:ad4:5bae:0:b0:46b:8c02:5dbc with SMTP id 14-20020ad45bae000000b0046b8c025dbcmr15586436qvq.94.1654720698052; Wed, 08 Jun 2022 13:38:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGB4awEJVlJlqxfeLrMvPenMDae5+geNY/aOMp1Uq1JkmgmL+xZATDsxtbrpiWYLtB6olYtQ== X-Received: by 2002:ad4:5bae:0:b0:46b:8c02:5dbc with SMTP id 14-20020ad45bae000000b0046b8c025dbcmr15586404qvq.94.1654720697445; Wed, 08 Jun 2022 13:38:17 -0700 (PDT) Received: from barrymore.redhat.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 ay33-20020a05620a17a100b006a6f68c8a87sm3666950qkb.126.2022.06.08.13.38.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 13:38:16 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: redeclared hidden friend take 2 [PR105852] Date: Wed, 8 Jun 2022 16:38:15 -0400 Message-Id: <20220608203815.2718646-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" My previous patch for 105761 avoided copying DECL_TEMPLATE_INFO from a friend to a later definition, but in this testcase we have first a non-friend declaration and then a definition, and we need to avoid copying in that case as well. But we do still want to set new_template_info to avoid GC trouble. With this change, the modules dump correctly identifies ::foo as a non-template function in tpl-friend-2_a.C. Along the way I noticed that the duplicate_decls handling of DECL_UNIQUE_FRIEND_P was backwards for templates, where we don't clobber DECL_LANG_SPECIFIC (olddecl) with DECL_LANG_SPECIFIC (newdecl) like we do for non-templates. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/105852 PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Avoid copying template info from non-templated friend even if newdecl isn't a definition. Correct handling of DECL_UNIQUE_FRIEND_P on templates. * pt.cc (non_templated_friend_p): New. * cp-tree.h (non_templated_friend_p): Declare it. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-2_a.C: Adjust expected dump. * g++.dg/template/friend74.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/decl.cc | 16 ++++------ gcc/cp/pt.cc | 29 ++++++++++++++++++- gcc/testsuite/g++.dg/modules/tpl-friend-2_a.C | 2 +- gcc/testsuite/g++.dg/template/friend74.C | 8 +++++ 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/friend74.C base-commit: b6e1373bd34aebbb512a03ea9a4e3c7acd955382 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index cc13809f38a..3d8a08b8dd7 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7395,6 +7395,7 @@ extern bool push_tinst_level_loc (tree, location_t); extern bool push_tinst_level_loc (tree, tree, location_t); extern void pop_tinst_level (void); extern struct tinst_level *outermost_tinst_level(void); +extern bool non_templated_friend_p (tree); extern void init_template_processing (void); extern void print_template_statistics (void); bool template_template_parameter_p (const_tree); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 7add82aa39e..3e869954ccb 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -2294,8 +2294,8 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) merge_default_template_args (new_parms, old_parms, /*class_p=*/false); } - if (!DECL_UNIQUE_FRIEND_P (old_result)) - DECL_UNIQUE_FRIEND_P (new_result) = false; + if (!DECL_UNIQUE_FRIEND_P (new_result)) + DECL_UNIQUE_FRIEND_P (old_result) = false; check_default_args (newdecl); @@ -2654,13 +2654,7 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) if (LANG_DECL_HAS_MIN (newdecl)) { DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); - if (new_defines_function - && DECL_TEMPLATE_INFO (olddecl) - && DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT - (DECL_TI_TEMPLATE (olddecl)))) - /* Don't copy template info from a non-template friend declaration - in a class template (PR105761). */; - else if (DECL_TEMPLATE_INFO (newdecl)) + if (DECL_TEMPLATE_INFO (newdecl)) { new_template_info = DECL_TEMPLATE_INFO (newdecl); if (DECL_TEMPLATE_INSTANTIATION (olddecl) @@ -2668,8 +2662,10 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) /* Remember the presence of explicit specialization args. */ TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl)) = TINFO_USED_TEMPLATE_ID (new_template_info); - DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); } + + if (non_templated_friend_p (olddecl)) + /* Don't copy tinfo from a non-templated friend (PR105761). */; else DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); } diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index dcacba56a1c..9c1b026857e 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -11165,6 +11165,33 @@ outermost_tinst_level (void) return level; } +/* True iff T is a friend function declaration that is not itself a template + and is not defined in a class template. */ + +bool +non_templated_friend_p (tree t) +{ + if (t && TREE_CODE (t) == FUNCTION_DECL + && DECL_UNIQUE_FRIEND_P (t)) + { + tree ti = DECL_TEMPLATE_INFO (t); + if (!ti) + return true; + /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */ + if (DECL_FRIEND_CONTEXT (t)) + return false; + /* Non-templated friends in a class template are still represented with a + TEMPLATE_DECL; check that its primary template is the befriending + class. Note that DECL_PRIMARY_TEMPLATE is null for + template friend A::f(); */ + tree tmpl = TI_TEMPLATE (ti); + tree primary = DECL_PRIMARY_TEMPLATE (tmpl); + return (primary && primary != tmpl); + } + else + return false; +} + /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the vector of template arguments, as for tsubst. @@ -14094,7 +14121,7 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, /* This special case arises when we have something like this: template struct S { - friend void f(int, double); + friend void f(int, double); }; Here, the DECL_TI_TEMPLATE for the friend declaration diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-2_a.C b/gcc/testsuite/g++.dg/modules/tpl-friend-2_a.C index 3acacf8ee34..c12857fbf69 100644 --- a/gcc/testsuite/g++.dg/modules/tpl-friend-2_a.C +++ b/gcc/testsuite/g++.dg/modules/tpl-friend-2_a.C @@ -16,5 +16,5 @@ template class TPL; // instantiate void foo (int, void *); // { dg-final { scan-lang-dump {Cluster members:\n \[0\]=decl definition '::template TPL'\n( \[.\]=[^\n]*'\n)* \[.\]=decl declaration '::template foo'\n( \[.\]=[^\n]*'\n)* \[.\]=binding '::TPL'} module } } -// { dg-final { scan-lang-dump {Cluster members:\n \[0\]=decl declaration '::foo'\n \[.\]=binding '::foo'} module } } +// { dg-final { scan-lang-dump {Cluster members:\n \[0\]=decl declaration '::foo'\n \[.\]=binding '::foo'} module } } // { dg-final { scan-lang-dump {Cluster members:\n \[0\]=specialization definition '::TPL'} module } } diff --git a/gcc/testsuite/g++.dg/template/friend74.C b/gcc/testsuite/g++.dg/template/friend74.C new file mode 100644 index 00000000000..51708334d81 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/friend74.C @@ -0,0 +1,8 @@ +// PR c++/105852 +// { dg-additional-options -w } + +template struct Local { friend Local False(int *); }; +Local loc; +Local False(int *); +void New() { False; } +Local False(int *) { return Local(); }