public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH 1/2] c++: improve "NTTP argument considered unused" fix [PR53164, PR105848]
Date: Wed, 29 Mar 2023 17:36:13 -0400	[thread overview]
Message-ID: <a22ddfe6-7f14-ead4-6787-a1d7db03e56a@redhat.com> (raw)
In-Reply-To: <4a04306f-faa9-5dd8-c850-05aa4a21d36e@idea>

On 3/27/23 09:30, Patrick Palka wrote:
> On Thu, 23 Mar 2023, Patrick Palka wrote:
> 
>> 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.

Agreed.  But why didn't we already wrap it in an ADDR_EXPR?  Even for 
reference tparms convert_nontype_argument should do that.

>> 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) <case CALL_EXPR>: 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);
> 
> Here's v2 which optimizes this function by not walking if
> !PRIMARY_TEMPLATE_P (since in that case the innermost arguments have
> already been walked when instantiating the context), and walking only
> non-type arguments:
> 
> -- >8
> 
> Subject: [PATCH] c++: improve "NTTP argument considered unused" fix [PR53164,
>   PR105848]
> 
> 	PR c++/53164
> 	PR c++/105848
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (instantiate_class_template): Call
> 	mark_template_arguments_used.
> 	(tsubst_copy_and_build) <case CALL_EXPR>: 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                             | 57 +++++++++++++++++-------
>   gcc/testsuite/g++.dg/template/fn-ptr3a.C | 25 +++++++++++
>   gcc/testsuite/g++.dg/template/fn-ptr4.C  | 14 ++++++
>   3 files changed, 80 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 3bb98ebeac1..a87366bb616 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, tree);
>   
>   /* Make the current scope suitable for access checking when we are
>      processing T.  T can be FUNCTION_DECL for instantiated function
> @@ -12152,6 +12153,9 @@ instantiate_class_template (tree type)
>         cp_unevaluated_operand = 0;
>         c_inhibit_evaluation_warnings = 0;
>       }
> +
> +  mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
> +
>     /* Use #pragma pack from the template context.  */
>     saved_maximum_field_alignment = maximum_field_alignment;
>     maximum_field_alignment = TYPE_PRECISION (pattern);
> @@ -21192,22 +21196,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;
> @@ -21902,6 +21894,37 @@ check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
>     return result;
>   }
>   
> +/* Call mark_used on each entity within the non-type template arguments in
> +   ARGS for a specialization of TMPL, 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 is set iff the template arguments are
> +   dependent.  */
> +
> +static void
> +mark_template_arguments_used (tree tmpl, tree args)
> +{
> +  /* It suffices to do this only when fully specializing a primary template.  */
> +  if (processing_template_decl || !PRIMARY_TEMPLATE_P (tmpl))
> +    return;
> +
> +  /* We already marked outer arguments when specializing the context.  */
> +  args = INNERMOST_TEMPLATE_ARGS (args);
> +
> +  for (tree arg : tree_vec_range (args))
> +    if (!TYPE_P (arg))
> +      {
> +	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);

Do we really need to walk into the args?

> +      }
> +}
> +
>   /* We're out of SFINAE context now, so generate diagnostics for the access
>      errors we saw earlier when instantiating D from TMPL and ARGS.  */
>   
> @@ -22031,6 +22054,8 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
>         push_nested_class (ctx);
>       }
>   
> +  mark_template_arguments_used (gen_tmpl, 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<class T>
> +void f(T) { T::fail; } // { dg-error "fail" }
> +
> +template<void (*P)(int)>
> +struct A {
> +  static void wrap();
> +};
> +
> +template<void (&P)(char)>
> +void wrap();
> +
> +template<int>
> +void g() {
> +  A<f>::wrap(); // { dg-message "required from here" }
> +  wrap<f>(); // { 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..36551ec5d7f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/fn-ptr4.C
> @@ -0,0 +1,14 @@
> +// { dg-do compile }
> +
> +template<void (*P)()>
> +void wrap() {
> +  P(); // OK, despite A::g not being accessible from here.
> +}
> +
> +struct A {
> +  static void f() {
> +    wrap<A::g>();
> +  }
> +private:
> +  static void g();
> +};


  reply	other threads:[~2023-03-29 21:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-23 21:18 Patrick Palka
2023-03-23 21:18 ` [PATCH 2/2] c++: duplicate "use of deleted fn" diagnostic [PR106880] Patrick Palka
2023-03-30  2:58   ` Jason Merrill
2023-03-23 21:26 ` [PATCH 1/2] c++: improve "NTTP argument considered unused" fix [PR53164, PR105848] Patrick Palka
2023-03-27 13:30 ` Patrick Palka
2023-03-29 21:36   ` Jason Merrill [this message]
2023-03-30 18:53     ` Patrick Palka
2023-03-30 21:39       ` Jason Merrill
2023-03-31 15:25         ` Patrick Palka
2023-03-31 15:55           ` Patrick Palka
2023-03-31 17:28             ` Jason Merrill
2023-03-31 19:06               ` Patrick Palka
2023-04-01  3:50                 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a22ddfe6-7f14-ead4-6787-a1d7db03e56a@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).