public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Seyed Sajad Kahani <sska1377@gmail.com>
Cc: gcc-patches@gcc.gnu.org, ppalka@redhat.com, jaon@redhat.com
Subject: Re: [PATCH v3] c++: Fix auto deduction for template specialization scopes [PR114915]
Date: Wed, 15 May 2024 11:07:19 -0400 (EDT)	[thread overview]
Message-ID: <78c1f983-6559-414d-b533-e354fa5cafa4@idea> (raw)
In-Reply-To: <20240510124747.289793-1-sska1377@gmail.com>


On Fri, 10 May 2024, Seyed Sajad Kahani wrote:

> This patch resolves PR114915 by replacing the logic that fills in the missing levels in do_auto_deduction in cp/pt.cc.
> The new approach now trims targs if the depth of targs is deeper than desired (this will only happen in specific contexts), and still fills targs with empty layers if it has fewer depths than expected.

The logic looks good to me, thanks!  Note that as per
https://gcc.gnu.org/contribute.html patches need a ChangeLog entry in
the commit message, for example let's use:

	PR c++/114915

gcc/cp/ChangeLog:

	* pt.cc (do_auto_deduction): Handle excess outer template
	arguments during constrained auto satisfaction.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-placeholder14.C: New test.
	* g++.dg/cpp2a/concepts-placeholder15.C: New test.
	* g++.dg/cpp2a/concepts-placeholder16.C: New test.

Jason, what do you think?

> ---
>  gcc/cp/pt.cc                                  | 20 ++++++++---
>  .../g++.dg/cpp2a/concepts-placeholder14.C     | 19 +++++++++++
>  .../g++.dg/cpp2a/concepts-placeholder15.C     | 15 +++++++++
>  .../g++.dg/cpp2a/concepts-placeholder16.C     | 33 +++++++++++++++++++
>  4 files changed, 83 insertions(+), 4 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 3b2106dd3..479b2a5bd 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -31253,6 +31253,19 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>  	full_targs = add_outermost_template_args (tmpl, full_targs);
>        full_targs = add_to_template_args (full_targs, targs);
>  
> +      int want = TEMPLATE_TYPE_ORIG_LEVEL (auto_node);
> +      int have = TMPL_ARGS_DEPTH (full_targs);
> +
> +      if (want < have)
> +	{
> +	  // if a constrained auto is declared in an explicit specialization
> +	  gcc_assert (context == adc_variable_type || context == adc_return_type
> +			  || context == adc_decomp_type);
> +	  tree trimmed_full_args = get_innermost_template_args
> +	    (full_targs, want);
> +	  full_targs = trimmed_full_args;
> +	}
> +      
>        /* HACK: Compensate for callers not always communicating all levels of
>  	 outer template arguments by filling in the outermost missing levels
>  	 with dummy levels before checking satisfaction.  We'll still crash
> @@ -31260,11 +31273,10 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>  	 these missing levels, but this hack otherwise allows us to handle a
>  	 large subset of possible constraints (including all non-dependent
>  	 constraints).  */
> -      if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
> -				- TMPL_ARGS_DEPTH (full_targs)))
> +      if (want > have)
>  	{
> -	  tree dummy_levels = make_tree_vec (missing_levels);
> -	  for (int i = 0; i < missing_levels; ++i)
> +	  tree dummy_levels = make_tree_vec (want - have);
> +	  for (int i = 0; i < want - have; ++i)
>  	    TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
>  	  full_targs = add_to_template_args (dummy_levels, full_targs);
>  	}
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C
> new file mode 100644
> index 000000000..fcdbd7608
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C
> @@ -0,0 +1,19 @@
> +// PR c++/114915
> +// { dg-do compile { target c++20 } }
> +
> +template<typename T>
> +concept C = __is_same(T, int);
> +
> +template<typename T>
> +void f() {
> +}
> +
> +template<>
> +void f<int>() {
> +  C auto x = 1;
> +}
> +
> +int main() {
> +  f<int>();
> +  return 0;
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C
> new file mode 100644
> index 000000000..b4f73f407
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C
> @@ -0,0 +1,15 @@
> +// PR c++/114915
> +// { dg-do compile { target c++20 } }
> +
> +template<typename T, typename U>
> +concept C = __is_same(T, U);
> +
> +template<typename T>
> +int x = 0;
> +
> +template<>
> +C<double> auto x<double> = 1.0;
> +
> +int main() {
> +  return 0;
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C
> new file mode 100644
> index 000000000..f808ef1b6
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C
> @@ -0,0 +1,33 @@
> +// PR c++/114915
> +// { dg-do compile { target c++20 } }
> +
> +template<typename T, typename U>
> +concept C = __is_same(T, U);
> +
> +template<typename T>
> +struct A
> +{ 
> +    template<typename U>
> +    void f() {
> +    }
> +};
> + 
> +template<>
> +template<>
> +void A<int>::f<int>() {
> +  C<int> auto x = 1;
> +}
> +
> +template<>
> +template<typename U>
> +void A<bool>::f() {
> +  C<int> auto x = 1;
> +}
> +
> +int main() {
> +  A<bool> a;
> +  a.f<char>();
> +  A<int> b;
> +  b.f<int>();
> +  return 0;
> +}
> -- 
> 2.45.0
> 
> 


  reply	other threads:[~2024-05-15 15:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-01 22:52 [PATCH] Fix auto deduction for template specialization scopes [114915] Seyed Sajad Kahani
2024-05-03 16:24 ` Patrick Palka
2024-05-04 14:11   ` Seyed Sajad Kahani
2024-05-04 15:15     ` [PATCH v2] Fix auto deduction for template specialization scopes [PR114915] Seyed Sajad Kahani
2024-05-06 18:46       ` Patrick Palka
2024-05-10 12:43         ` Seyed Sajad Kahani
2024-05-10 12:47         ` [PATCH v3] " Seyed Sajad Kahani
2024-05-15 15:07           ` Patrick Palka [this message]
2024-05-15 15:09             ` [PATCH v3] c++: " Patrick Palka
2024-05-15 17:27               ` [PATCH v4] c++: fix constained auto deduction in templ spec " Seyed Sajad Kahani
2024-05-22 20:30                 ` Jason Merrill
2024-06-14 14:15                   ` sska1377
2024-06-14 14:18                   ` [PATCH v5] " Seyed Sajad Kahani
2024-05-22 15:41 ` [PATCH] Fix auto deduction for template specialization scopes [114915] Jason Merrill
2024-05-22 16:48   ` Patrick Palka
2024-05-22 17:32     ` 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=78c1f983-6559-414d-b533-e354fa5cafa4@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jaon@redhat.com \
    --cc=sska1377@gmail.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).