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>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: return-type-req in constraint using only outer tparms [PR104527]
Date: Tue, 15 Feb 2022 18:39:33 -0500	[thread overview]
Message-ID: <4c1b4638-0ad6-f229-4d9b-ac2fb362d058@redhat.com> (raw)
In-Reply-To: <20220214163250.3454164-1-ppalka@redhat.com>

On 2/14/22 11:32, Patrick Palka wrote:
> Here the template context for the atomic constraint has two levels of
> template arguments, but since it depends only on the innermost argument
> T we use a single-level argument vector during substitution into the
> constraint (built by get_mapped_args).  We eventually pass this vector
> to do_auto_deduction as part of checking the return-type-requirement
> inside the atom, but do_auto_deduction expects outer_targs to be a full
> set of arguments for sake of satisfaction.

Could we note the current number of levels in the map and use that in 
get_mapped_args instead of the highest level parameter we happened to use?

> do_auto_deduction has a workaround in place to compensate for callers
> that pass only the innermost arguments as outer_targs, but here we're
> passing the _outermost_ arguments.  Since the former situation should
> now (after r12-7101) only occur with adc_unify callers and the latter
> only with adc_requirement callers, this patch conditions the existing
> workaround according to the auto_deduction_context: if the context is
> adc_requirement, we add dummy innermost levels, otherwise we add dummy
> outermost levels as before and also assert that the context is adc_unify.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu and tested on cmcstl2
> and range-v3, does this look OK for trunk?
> 
> 	PR c++/104527
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (do_auto_deduction): When template argument levels are
> 	missing from outer_targs, fill in the innermost rather than the
> 	outermost levels with dummy args if the context is
> 	adc_requirement, otherwise also assert that the context is
> 	adc_unify.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-return-req4.C: New test.
> ---
>   gcc/cp/pt.cc                                  | 28 +++++++++++++------
>   .../g++.dg/cpp2a/concepts-return-req4.C       | 24 ++++++++++++++++
>   2 files changed, 44 insertions(+), 8 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 1b18e2a7787..4ff2710b8ba 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -30215,20 +30215,32 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>   
>         tree full_targs = add_to_template_args (outer_targs, targs);
>   
> -      /* 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
> -	 if the constraint depends on a template argument belonging to one of
> -	 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)))
>   	{
>   	  tree dummy_levels = make_tree_vec (missing_levels);
>   	  for (int i = 0; i < missing_levels; ++i)
>   	    TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
> -	  full_targs = add_to_template_args (dummy_levels, full_targs);
> +	  if (context == adc_requirement)
> +	    /* We're checking a requires-expr's return-type-requirement that's
> +	       part of an atomic constraint that doesn't depend on any innermost
> +	       template arguments, so OUTER_TARGS (built by get_mapped_args) is
> +	       missing at least one innermost level.  Fill in the innermost
> +	       levels of OUTER_TARGS with dummy levels.  */
> +	    full_targs = add_to_template_args
> +	      (add_to_template_args (outer_targs, dummy_levels), targs);
> +	  else
> +	    {
> +	      /* Otherwise, fill in the _outermost_ levels with dummy levels.
> +		 This compensates for adc_unify callers that only pass the
> +		 innermost level of template arguments as OUTER_TARGS.  We'll
> +		 still crash if the constraint depends on a template argument
> +		 belonging to one of these missing levels, but this hack
> +		 otherwise allows us to handle a large subset of possible
> +		 constraints (including all non-dependent constraints).  */
> +	      gcc_checking_assert (context == adc_unify);
> +	      full_targs = add_to_template_args (dummy_levels, full_targs);
> +	    }
>   	}
>   
>         if (!constraints_satisfied_p (auto_node, full_targs))
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C
> new file mode 100644
> index 00000000000..471946bc8eb
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C
> @@ -0,0 +1,24 @@
> +// PR c++/104527
> +// { dg-do compile { target c++20 } }
> +
> +template<class T, class U>
> +concept is_same = __is_same(T, U);
> +
> +template<class T>
> +struct A {
> +  template<class...>
> +    requires requires { { 0 } -> is_same<T>; }
> +  struct B {};
> +
> +  template<class...>
> +    requires requires { { 1 } -> is_same<T>; }
> +  static void f();
> +};
> +
> +A<int>::B<> a1;
> +A<bool>::B<> a2; // { dg-error "constraint" }
> +
> +int main() {
> +  A<int>::f();
> +  A<bool>::f(); // { dg-error "no match" }
> +}


  parent reply	other threads:[~2022-02-15 23:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-14 16:32 Patrick Palka
2022-02-14 16:37 ` Patrick Palka
2022-02-15 23:39 ` Jason Merrill [this message]
2022-02-16 19:56   ` Patrick Palka
2022-03-01 13:13     ` Patrick Palka
2022-03-10 19:43     ` Jason Merrill
2022-03-10 20:57       ` Patrick Palka
2022-03-11 22:19         ` 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=4c1b4638-0ad6-f229-4d9b-ac2fb362d058@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).