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 4082B3858CDB for ; Thu, 23 Mar 2023 21:27:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4082B3858CDB 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=1679606833; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FYqvaJZDXiAqEqLhhSUBko/4/JXVgIZnwyLjB42+9sw=; b=XkHgpENnlNe8iMTGVjn3WeEW1Ijz69H2F7lTDURJovEDELMKNX7iB+edJNpLUxdfSm7Wsw FXKi5s9SR29xvBqrcQfdKPkRuC+me/o7kaPz5Hr9YI6yRjEbD0QEwhlnfK3ISuqiM7b20C E2eD1sFOP5+gDyPzZ/cRPRC6MS1g07g= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-118-q_1pqZI3MKuJ28giKx0_xw-1; Thu, 23 Mar 2023 17:27:12 -0400 X-MC-Unique: q_1pqZI3MKuJ28giKx0_xw-1 Received: by mail-ed1-f71.google.com with SMTP id fi8-20020a056402550800b004a26cc7f6cbso202748edb.4 for ; Thu, 23 Mar 2023 14:27:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679606831; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FYqvaJZDXiAqEqLhhSUBko/4/JXVgIZnwyLjB42+9sw=; b=o3wQd3Yohlsnpa8snRuYAxNsCxjffcjrrfi7TUUSuvpXSbPZYrtNR9zl6DldwJR9x1 gBma4fGEWlJfIUSLfwHTus2MQIFE6PfII9rSUuhx8HTtquqdpfs+I5u1Ln8kq6B1iHPB 7aOX6SDcNZngdVEIvJRXhlHtNTDT/d092Yp//KgYnSYNaNpmm3ERXlZwgWnDGVZGUlo8 7cBX2TGQhQOnjZCsTz9gfKKNWhp2GK02c89483kmET5X9ErUpGnjkW3ERrgvYQhbyZGm LnBrpD/2HDpW6arT6McB8K1wrcPPXHZiycBCsNtV70Xlkqh/SCIXjo1aCaNAW3wyhIvN gtWA== X-Gm-Message-State: AAQBX9e4g7zI2zCUMgV9HFO997iGwiiOTzaeaBcNtQbqaBcC4MBuwbhL MMTyQeH4yBjRbPNantNCB/G0ExhIW34FN6lEDQRmqYe0VD5cW4SWjev1Mq9z0RpfSSkY2C6n120 6wsuW1VdKAvu/vaokvSQOBs3DzXPDm7wUzSFeDCpfBqVN X-Received: by 2002:a17:906:2357:b0:931:cac0:60ff with SMTP id m23-20020a170906235700b00931cac060ffmr212589eja.13.1679606830894; Thu, 23 Mar 2023 14:27:10 -0700 (PDT) X-Google-Smtp-Source: AKy350YGuLewKCxAGC1EVTKdO/rwWSUIf1EO5yjP68GwLzZAeUWvXIt17LtU+OkgCTh3roWvKoy239ndjFgFZerKo3E= X-Received: by 2002:a17:906:2357:b0:931:cac0:60ff with SMTP id m23-20020a170906235700b00931cac060ffmr212584eja.13.1679606830544; Thu, 23 Mar 2023 14:27:10 -0700 (PDT) MIME-Version: 1.0 References: <20230323211803.396326-1-ppalka@redhat.com> In-Reply-To: <20230323211803.396326-1-ppalka@redhat.com> From: Patrick Palka Date: Thu, 23 Mar 2023 17:26:59 -0400 Message-ID: Subject: Re: [PATCH 1/2] c++: improve "NTTP argument considered unused" fix [PR53164, PR105848] To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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: On Thu, Mar 23, 2023 at 5:18=E2=80=AFPM Patrick Palka w= rote: > > 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. Note we previously discussed this TEMPLATE_PARM_INDEX approach here https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596257.html though I never pushed it since it felt somewhat overkill to me every single substituted use of an NTTP would be considered for marking; perhaps this approach might be preferable? Yet another approach would be to do it from tsubst_template_args.. > > 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 nest= ed); > 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 =3D 0; > c_inhibit_evaluation_warnings =3D 0; > } > + > + mark_template_arguments_used (INNERMOST_TEMPLATE_ARGS (args)); > + > /* Use #pragma pack from the template context. */ > saved_maximum_field_alignment =3D maximum_field_alignment; > maximum_field_alignment =3D TYPE_PRECISION (pattern); > @@ -21173,22 +21177,10 @@ tsubst_copy_and_build (tree t, > } > > /* Remember that there was a reference to this entity. */ > - if (function !=3D NULL_TREE) > - { > - tree inner =3D function; > - if (TREE_CODE (inner) =3D=3D ADDR_EXPR > - && TREE_CODE (TREE_OPERAND (inner, 0)) =3D=3D FUNCTION_DE= CL) > - /* 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 ove= rload > - resolution then that call to mark_used wouldn't have mar= ked it > - odr-used yet (53164). */ > - inner =3D TREE_OPERAND (inner, 0); > - if (DECL_P (inner) > - && !mark_used (inner, complain) && !(complain & tf_error)= ) > - RETURN (error_mark_node); > - } > + if (function !=3D 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, t= subst_flags_t complain) > return result; > } > > +/* Call mark_used on each entity within the template arguments ARGS of s= ome > + template specialization, to ensure that each such entity is considere= d > + 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, a= nd > + 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) =3D=3D 1); > + > + if (processing_template_decl) > + return; > + > + auto mark_used_r =3D [](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 acce= ss > 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 =3D DECL_TEMPLATE_RESULT (gen_tmpl); > > fndecl =3D 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(); > +}; > -- > 2.40.0.130.g27d43aaaf5 >