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++: find_template_parameters and PARM_DECLs [PR105797]
Date: Thu, 2 Jun 2022 16:43:21 -0400	[thread overview]
Message-ID: <3307a430-2c74-5002-489c-f30c3208cefb@redhat.com> (raw)
In-Reply-To: <20220601164437.1963270-1-ppalka@redhat.com>

On 6/1/22 12:44, Patrick Palka wrote:
> As explained in r11-4959-gde6f64f9556ae3, the atom cache assumes two
> equivalent expressions (according to cp_tree_equal) must use the same
> template parameters (according to find_template_parameters).  This
> assumption turned out to not hold for TARGET_EXPR, which was addressed
> by that commit.
> 
> But this assumption apparently doesn't hold for PARM_DECL either:
> find_template_parameters walks its DECL_CONTEXT but cp_tree_equal by
> default doesn't consider DECL_CONTEXT unless comparing_specializations
> is set.  Thus in the first testcase below, the atomic constraints of #1
> and #2 are equivalent according to cp_tree_equal, but according to
> find_template_parameters the former uses T and the latter uses both T
> and U.
> 
> I suppose we can fix this assumption violation by setting
> comparing_specializations in the atom_hasher, which would make
> cp_tree_equal return false for the two atoms, but that seems overly
> pessimistic here.  Ideally the atoms should be considered equivalent
> and we should fix find_template_paremeters to return just T for #2's
> atom.
> 
> To that end this patch makes for_each_template_parm_r stop walking the
> DECL_CONTEXT of a PARM_DECL.  This should be safe to do because
> tsubst_copy / tsubst_decl only cares about the TREE_TYPE of a PARM_DECL
> and doesn't bother substituting the DECL_CONTEXT, thus the only relevant
> template parameters are those used in its type.  any_template_parm_r is
> currently responsible for walking its TREE_TYPE, but I suppose it now makes
> sense make for_each_template_parm_r do so instead.
> 
> In passing this patch also makes for_each_template_parm_r stop walking
> the DECL_CONTEXT of a VAR_/FUNCTION_DECL since it should be unnecessary
> after walking DECL_TI_ARGS.
> 
> I experimented with not walking DECL_CONTEXT for CONST_DECL, but the
> second testcase below demonstrates it's necessary to walk it.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> 	PR c++/105797
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (for_each_template_parm_r) <case FUNCTION_DECL, VAR_DECL>:
> 	Don't walk DECL_CONTEXT.
> 	<case PARM_DECL>: Likewise.  Walk TREE_TYPE.
> 	<case CONST_DECL>: Simplify accordingly.
> 	(any_template_parm_r) <case PARM_DECL>: Don't walk TREE_TYPE.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-decltype4.C: New test.
> ---
>   gcc/cp/pt.cc                                    | 10 +++++-----
>   gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C | 16 ++++++++++++++++
>   gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C   | 12 ++++++++++++
>   3 files changed, 33 insertions(+), 5 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 4f0ace2644b..e4a473002a0 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -10561,11 +10561,14 @@ for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
>       case VAR_DECL:
>         if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
>   	WALK_SUBTREE (DECL_TI_ARGS (t));
> -      /* Fall through.  */
> +      break;
>   
>       case PARM_DECL:
> +      WALK_SUBTREE (TREE_TYPE (t));
> +      break;
> +
>       case CONST_DECL:
> -      if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
> +      if (DECL_TEMPLATE_PARM_P (t))
>   	WALK_SUBTREE (DECL_INITIAL (t));
>         if (DECL_CONTEXT (t)
>   	  && pfd->include_nondeduced_p)
> @@ -10824,9 +10827,6 @@ any_template_parm_r (tree t, void *data)
>         break;
>   
>       case TEMPLATE_PARM_INDEX:
> -    case PARM_DECL:
> -      /* A parameter or constraint variable may also depend on a template
> -	 parameter without explicitly naming it.  */
>         WALK_SUBTREE (TREE_TYPE (t));
>         break;
>   
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C
> new file mode 100644
> index 00000000000..6683d224cf8
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C
> @@ -0,0 +1,16 @@
> +// PR c++/105797
> +// { dg-do compile { target c++20 } }
> +
> +template<class T>
> +concept C = requires { T(); };
> +
> +template<class T>
> +void f(T v) requires C<decltype(v)>; // #1
> +
> +template<class T, class U>
> +void f(T v) requires C<decltype(v)>; // #2
> +
> +int main() {
> +  f<int, int>(0);
> +  f<int>(0);
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C b/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C
> new file mode 100644
> index 00000000000..3fa4fb82818
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C
> @@ -0,0 +1,12 @@
> +// { dg-do compile { target c++20 } }
> +
> +template<class T, int I>
> +struct A {
> +  enum E { e = I };
> +  static void f() requires (e != 0);
> +};
> +
> +int main() {
> +  A<int, 1>::f();
> +  A<int, 0>::f(); // { dg-error "no match" }
> +}


      parent reply	other threads:[~2022-06-02 20:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-01 16:44 Patrick Palka
2022-06-01 16:46 ` Patrick Palka
2022-06-02 20:43 ` Jason Merrill [this message]

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=3307a430-2c74-5002-489c-f30c3208cefb@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).