From patchwork Thu Mar 23 21:18:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 66823 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 9D778385043C for ; Thu, 23 Mar 2023 21:18:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D778385043C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679606319; bh=ZGuSIkApzyAIwPHPg4yY5HXCeLuwFirNk/+E6qjH2VQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=rUAzg3PL7aL3LntHkW1z6nl1iPfCn1Ny/NU7ax9dqlbcL7YNSTb9ijfkC4aM4+UKY MXOZY5D6fRLNm3aFoV0NaZWImOuUs1gRrXinHKYUCf7x1ccfmx0I9JDbnzd5P145Xd 7yex/8G1ISRG2iLppCypXI3+Uv4+RHLw9TOzI6iM= 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 90CF53858C50 for ; Thu, 23 Mar 2023 21:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90CF53858C50 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-196-K1ikW2jXM1OtuYhaiZD75w-1; Thu, 23 Mar 2023 17:18:10 -0400 X-MC-Unique: K1ikW2jXM1OtuYhaiZD75w-1 Received: by mail-qk1-f197.google.com with SMTP id z187-20020a3765c4000000b007468706dfb7so6587392qkb.9 for ; Thu, 23 Mar 2023 14:18:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679606289; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZGuSIkApzyAIwPHPg4yY5HXCeLuwFirNk/+E6qjH2VQ=; b=XY6HgHbdqhKNU2iMfsqxg6P53mt5dy2KMsFzUNDxGx9bhA+bmiuVGKg6C7li0uaG6z tLP7C9rKbBIeczcLq5+4D4SyYl4bQ2ZWpSu/QYr/SkQypXP9H8JYnSJbLdi0KV3DHv+H Up3vmUlYX7H9Sz4QSZ1gkh6T8geZvNhlbH1RvCijNk8IYhi26qfS19vTSPgIfFg04zvo O9cXm6tSwlmdu8TjLkr5JKEC9d68yUzIuJySTzU5f8h9satmjYx+ToePf9CnL6khVDwA VF+IG4IEbVsKlt7JCmfdQJFGDY3ofTbnj1Aap9bwQ9YzM2SyszKlwpkZxheDtn/petR8 MSTw== X-Gm-Message-State: AAQBX9epjWBUw6bFLpJlCoWREo3629UBgCo9xlO18FYwZLEz0EOloa6V lv/l+2/Dh5vz6k2tbhNqFGoqpN2ip4Biv9E9qsTL3vj7TAhWXpjszkK1CXd7mqxtk000KlCIZJz qEorGe/d+hXHrmvug1NS0W5MpZT9/y2jsw8txfIf04GXQEH1VETcYhosKy+C7wTWwqUFwi8Tv/H A= X-Received: by 2002:a05:6214:1243:b0:56e:b1c8:381b with SMTP id r3-20020a056214124300b0056eb1c8381bmr474988qvv.31.1679606288715; Thu, 23 Mar 2023 14:18:08 -0700 (PDT) X-Google-Smtp-Source: AKy350ZmeYjDuMeJayfAjkduo2Ipi2y0vgT1gY246bmzT3GjIKE9DHVsLK8mNvWk2WrzhMMb2f9RuA== X-Received: by 2002:a05:6214:1243:b0:56e:b1c8:381b with SMTP id r3-20020a056214124300b0056eb1c8381bmr474939qvv.31.1679606288209; Thu, 23 Mar 2023 14:18:08 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id w10-20020a0cff0a000000b005dd8b934585sm159174qvt.29.2023.03.23.14.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 14:18:07 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH 1/2] c++: improve "NTTP argument considered unused" fix [PR53164, PR105848] Date: Thu, 23 Mar 2023 17:18:02 -0400 Message-Id: <20230323211803.396326-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.130.g27d43aaaf5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" r13-995-g733a792a2b2e16 worked around the problem of FUNCTION_DECL template arguments not always getting marked as odr-used by redundantly calling mark_used on the substituted ADDR_EXPR callee of a CALL_EXPR. This is just a narrow workaround however, since using a FUNCTION_DECL as a template argument alone should constitutes an odr-use; we shouldn't need to subsequently e.g. call the function or take its address. This patch fixes this in a more general way at template specialization time by walking the template arguments of the specialization and calling mark_used on all entities used within. As before, the call to mark_used as it worst a no-op, but it compensates for the situation where we end up forming a specialization from a template context in which mark_used is inhibited. Another approach would be to call mark_used whenever we substitute a TEMPLATE_PARM_INDEX, but that would result in many more redundant calls to mark_used compared to this approach. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/53164 PR c++/105848 gcc/cp/ChangeLog: * pt.cc (instantiate_class_template): Call mark_template_arguments_used. (tsubst_copy_and_build) : Revert r13-995 change. (mark_template_arguments_used): Define. (instantiate_template): Call mark_template_arguments_used. gcc/testsuite/ChangeLog: * g++.dg/template/fn-ptr3a.C: New test. * g++.dg/template/fn-ptr4.C: New test. --- gcc/cp/pt.cc | 51 ++++++++++++++++-------- gcc/testsuite/g++.dg/template/fn-ptr3a.C | 25 ++++++++++++ gcc/testsuite/g++.dg/template/fn-ptr4.C | 14 +++++++ 3 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/fn-ptr3a.C create mode 100644 gcc/testsuite/g++.dg/template/fn-ptr4.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 7e4a8de0c8b..9b3cc33331c 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -220,6 +220,7 @@ static tree make_argument_pack (tree); static tree enclosing_instantiation_of (tree tctx); static void instantiate_body (tree pattern, tree args, tree d, bool nested); static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree); +static void mark_template_arguments_used (tree); /* Make the current scope suitable for access checking when we are processing T. T can be FUNCTION_DECL for instantiated function @@ -12142,6 +12143,9 @@ instantiate_class_template (tree type) cp_unevaluated_operand = 0; c_inhibit_evaluation_warnings = 0; } + + mark_template_arguments_used (INNERMOST_TEMPLATE_ARGS (args)); + /* Use #pragma pack from the template context. */ saved_maximum_field_alignment = maximum_field_alignment; maximum_field_alignment = TYPE_PRECISION (pattern); @@ -21173,22 +21177,10 @@ tsubst_copy_and_build (tree t, } /* Remember that there was a reference to this entity. */ - if (function != NULL_TREE) - { - tree inner = function; - if (TREE_CODE (inner) == ADDR_EXPR - && TREE_CODE (TREE_OPERAND (inner, 0)) == FUNCTION_DECL) - /* We should already have called mark_used when taking the - address of this function, but do so again anyway to make - sure it's odr-used: at worst this is a no-op, but if we - obtained this FUNCTION_DECL as part of ahead-of-time overload - resolution then that call to mark_used wouldn't have marked it - odr-used yet (53164). */ - inner = TREE_OPERAND (inner, 0); - if (DECL_P (inner) - && !mark_used (inner, complain) && !(complain & tf_error)) - RETURN (error_mark_node); - } + if (function != NULL_TREE + && DECL_P (function) + && !mark_used (function, complain) && !(complain & tf_error)) + RETURN (error_mark_node); if (!maybe_fold_fn_template_args (function, complain)) return error_mark_node; @@ -21883,6 +21875,31 @@ check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) return result; } +/* Call mark_used on each entity within the template arguments ARGS of some + template specialization, to ensure that each such entity is considered + odr-used regardless of whether the specialization was first formed in a + template context. + + This function assumes push_to_top_level has been called beforehand, and + that processing_template_decl has been set iff the template arguments + are dependent. */ + +static void +mark_template_arguments_used (tree args) +{ + gcc_checking_assert (TMPL_ARGS_DEPTH (args) == 1); + + if (processing_template_decl) + return; + + auto mark_used_r = [](tree *tp, int *, void *) { + if (DECL_P (*tp)) + mark_used (*tp, tf_none); + return NULL_TREE; + }; + cp_walk_tree_without_duplicates (&args, mark_used_r, nullptr); +} + /* We're out of SFINAE context now, so generate diagnostics for the access errors we saw earlier when instantiating D from TMPL and ARGS. */ @@ -22012,6 +22029,8 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) push_nested_class (ctx); } + mark_template_arguments_used (INNERMOST_TEMPLATE_ARGS (targ_ptr)); + tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl); fndecl = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/template/fn-ptr3a.C b/gcc/testsuite/g++.dg/template/fn-ptr3a.C new file mode 100644 index 00000000000..7456be5d51f --- /dev/null +++ b/gcc/testsuite/g++.dg/template/fn-ptr3a.C @@ -0,0 +1,25 @@ +// PR c++/53164 +// PR c++/105848 +// A stricter version of fn-ptr3.C that verifies using f as a template +// argument alone constitutes an odr-use. + +template +void f(T) { T::fail; } // { dg-error "fail" } + +template +struct A { + static void wrap(); +}; + +template +void wrap(); + +template +void g() { + A::wrap(); // { dg-message "required from here" } + wrap(); // { dg-message "required from here" } +} + +int main() { + g<0>(); +} diff --git a/gcc/testsuite/g++.dg/template/fn-ptr4.C b/gcc/testsuite/g++.dg/template/fn-ptr4.C new file mode 100644 index 00000000000..e7425ba96cc --- /dev/null +++ b/gcc/testsuite/g++.dg/template/fn-ptr4.C @@ -0,0 +1,14 @@ +// { dg-do compile } + +template +void wrap() { + P(); // OK, despite A::g not being accessible from foo. +} + +struct A { + static void f() { + wrap(); + } +private: + static void g(); +}; From patchwork Thu Mar 23 21:18:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 66824 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 8CA37384D1B0 for ; Thu, 23 Mar 2023 21:19:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CA37384D1B0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679606376; bh=7eyHBgM6ze5nmX9/rUPTioLS0SiM/2R3T6eebV0Gwok=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=pGPn8grMEBF77xQcSt9eUVj3zSJhu4cIFnTcWwtWeqIj5/G0h82ph4Ui5/jggIl4P 3M9zyy4HiI+eUGcha4Voz6ym2L7Vy4OtZj62pWb0lbbWoLov1nie5pwrHWR0A71Dg2 V/hPRchGMH0225ko4KgXbonv8PcaKIYPAA5qQiiU= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 783F03858CDB for ; Thu, 23 Mar 2023 21:18:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 783F03858CDB Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-99-7FUZUzoFPLuK4D5MBUqjmg-1; Thu, 23 Mar 2023 17:18:11 -0400 X-MC-Unique: 7FUZUzoFPLuK4D5MBUqjmg-1 Received: by mail-qv1-f72.google.com with SMTP id px9-20020a056214050900b005d510cdfc41so70206qvb.7 for ; Thu, 23 Mar 2023 14:18:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679606290; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7eyHBgM6ze5nmX9/rUPTioLS0SiM/2R3T6eebV0Gwok=; b=0Kv+mNrwdVjTmFCY+aHYeXaw4bYR2htAxIvnExOPNBruKDyEUO29U7cvKefxrxkng0 EvfmdieWVdvnrc0k0BXUGIQf8zQSzaihsK4pR8WYXPgBCuwifNpgGLINITfhIHiOhu+Q WLPsi+ip/4yMEzT6+bAVFEmYyG0wFFPjkIQ9JqrLpAZJqMAzOaxVse/1h/6FTWAjNPuc vu5THePi1Lud6kv0sW+w3CYM8w0mZ78Gb99oCjtT6EAvzDHI7wBcP+XNyXJIqSAyasoX QkxKP6s8kzqKh7kLt9IoiDXjli1aea6JSOfL+G/MaAEiG9wqMi5uPqpBlSyFr3WIYubJ 1EPg== X-Gm-Message-State: AAQBX9cbMBkMhgDcEPcb6IHHiH1dal7v2Fbc02IkhWN3aVXZAYYrYLt6 HIP8HkzIHq6o2pMQEKy+XrGKN78+JcbatULcrEhnuMvfflbdyg6DFqUDrJrrM7JlafPS69l1/So gaQ1BrAAv1eiaNtM/+DoLP7WpQ5snjtONVCPq/BX1RlcpLeHAgDSrPjT6hpExEwo1NBPdqcm/JT U= X-Received: by 2002:a05:6214:2507:b0:56e:bc62:e151 with SMTP id gf7-20020a056214250700b0056ebc62e151mr683046qvb.7.1679606289721; Thu, 23 Mar 2023 14:18:09 -0700 (PDT) X-Google-Smtp-Source: AKy350Y/grma0O+GzgDFVG0Ai9fFBXYDTCYnc8rP+DfSWSKTTjCofq4t5BLbR1B0N2UMiiuYVFV3Eg== X-Received: by 2002:a05:6214:2507:b0:56e:bc62:e151 with SMTP id gf7-20020a056214250700b0056ebc62e151mr683015qvb.7.1679606289398; Thu, 23 Mar 2023 14:18:09 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id w10-20020a0cff0a000000b005dd8b934585sm159174qvt.29.2023.03.23.14.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 14:18:09 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH 2/2] c++: duplicate "use of deleted fn" diagnostic [PR106880] Date: Thu, 23 Mar 2023 17:18:03 -0400 Message-Id: <20230323211803.396326-2-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.130.g27d43aaaf5 In-Reply-To: <20230323211803.396326-1-ppalka@redhat.com> References: <20230323211803.396326-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we're issuing a duplicate diagnostic for the use of the deleted foo, first from the CALL_EXPR case of tsubst_copy_and_build (which doesn't exit early upon failure), and again from from build_over_call when rebuilding the substituted CALL_EXPR. We can fix this by exiting early upon failure of the first call, but this first call should always be redundant since build_over_call (or another subroutine of finish_call_expr) ought to reliably call mark_used for a suitable DECL_P callee anyway. So this patch just gets rid of the first call to mark_used. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/106880 gcc/cp/ChangeLog: * pt.cc (tsubst_copy_and_build) : Don't call mark_used. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/deleted16.C: New test. --- gcc/cp/pt.cc | 6 ------ gcc/testsuite/g++.dg/cpp0x/deleted16.C | 11 +++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 9b3cc33331c..060d2d38504 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21176,12 +21176,6 @@ tsubst_copy_and_build (tree t, } } - /* Remember that there was a reference to this entity. */ - if (function != NULL_TREE - && DECL_P (function) - && !mark_used (function, complain) && !(complain & tf_error)) - RETURN (error_mark_node); - if (!maybe_fold_fn_template_args (function, complain)) return error_mark_node; diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C new file mode 100644 index 00000000000..93cfb51eb3d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C @@ -0,0 +1,11 @@ +// PR c++/106880 +// Verify we don't emit a "use of deleted function" diagnostic twice. +// { dg-do compile { target c++11 } } + +void foo() = delete; + +template +void f(T t) { foo(t); } // { dg-bogus "deleted function.*deleted function" } + // { dg-error "deleted function" "" { target *-*-*} .-1 } + +template void f(int);