public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: fix TTP level reduction cache
@ 2023-05-03  4:30 Jason Merrill
  2023-05-15 14:41 ` Patrick Palka
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2023-05-03  4:30 UTC (permalink / raw)
  To: gcc-patches

Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

We try to cache the result of reduce_template_parm_level so that when we
reduce the same parm multiple times we get the same result, but this wasn't
working for template template parms because in that case TYPE is a
TEMPLATE_TEMPLATE_PARM, and so same_type_p was false because of the same
level mismatch that we're trying to adjust for.  So in that case compare the
template parms of the template template parms instead.

The result can be seen in nontype12.C, where we previously gave three
duplicate errors on line 7 and now give only one because subsequent
substitutions use the cache.

gcc/cp/ChangeLog:

	* pt.cc (reduce_template_parm_level): Fix comparison of
	template template parm to cached version.

gcc/testsuite/ChangeLog:

	* g++.dg/template/nontype12.C: Check for duplicate error.
---
 gcc/cp/pt.cc                              | 7 ++++++-
 gcc/testsuite/g++.dg/template/nontype12.C | 3 ++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 471fc20bc5b..5446b5058b7 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -4550,7 +4550,12 @@ reduce_template_parm_level (tree index, tree type, int levels, tree args,
   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
 	  != TEMPLATE_PARM_LEVEL (index) - levels)
-      || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
+      || !(TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
+	   ? (comp_template_parms
+	      (DECL_TEMPLATE_PARMS (TYPE_NAME (type)),
+	       DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL
+				    (TEMPLATE_PARM_DESCENDANTS (index)))))
+	   : same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))))
     {
       tree orig_decl = TEMPLATE_PARM_DECL (index);
 
diff --git a/gcc/testsuite/g++.dg/template/nontype12.C b/gcc/testsuite/g++.dg/template/nontype12.C
index e37cf8f7646..6642ffd0a13 100644
--- a/gcc/testsuite/g++.dg/template/nontype12.C
+++ b/gcc/testsuite/g++.dg/template/nontype12.C
@@ -4,7 +4,8 @@
 template<typename T> struct A
 {
   template<T> int foo();                        // { dg-error "double" "" { target c++17_down } }
-  template<template<T> class> int bar();        // { dg-error "double" "" { target c++17_down } }
+  template<template<T> class> int bar();        // { dg-bogus {double.*C:7:[^\n]*double} }
+  // { dg-error "double" "" { target c++17_down } .-1 }
   template<T> struct X;                         // { dg-error "double" "" { target c++17_down } }
 };
 

base-commit: d7cb9720ed54687bd1135c5e6ef90776a9db0bd5
-- 
2.31.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [pushed] c++: fix TTP level reduction cache
  2023-05-03  4:30 [pushed] c++: fix TTP level reduction cache Jason Merrill
@ 2023-05-15 14:41 ` Patrick Palka
  0 siblings, 0 replies; 2+ messages in thread
From: Patrick Palka @ 2023-05-15 14:41 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Wed, 3 May 2023, Jason Merrill via Gcc-patches wrote:

> Tested x86_64-pc-linux-gnu, applying to trunk.
> 
> -- 8< --
> 
> We try to cache the result of reduce_template_parm_level so that when we
> reduce the same parm multiple times we get the same result, but this wasn't
> working for template template parms because in that case TYPE is a
> TEMPLATE_TEMPLATE_PARM, and so same_type_p was false because of the same
> level mismatch that we're trying to adjust for.  So in that case compare the
> template parms of the template template parms instead.
> 
> The result can be seen in nontype12.C, where we previously gave three
> duplicate errors on line 7 and now give only one because subsequent
> substitutions use the cache.
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (reduce_template_parm_level): Fix comparison of
> 	template template parm to cached version.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/nontype12.C: Check for duplicate error.
> ---
>  gcc/cp/pt.cc                              | 7 ++++++-
>  gcc/testsuite/g++.dg/template/nontype12.C | 3 ++-
>  2 files changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 471fc20bc5b..5446b5058b7 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -4550,7 +4550,12 @@ reduce_template_parm_level (tree index, tree type, int levels, tree args,
>    if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
>        || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
>  	  != TEMPLATE_PARM_LEVEL (index) - levels)
> -      || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
> +      || !(TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
> +	   ? (comp_template_parms
> +	      (DECL_TEMPLATE_PARMS (TYPE_NAME (type)),
> +	       DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL
> +				    (TEMPLATE_PARM_DESCENDANTS (index)))))

Isn't this comparing the unsubstituted/unlowered template parameters of
the ttp vs the substituted/lowered template parameters?  So this test
should always return false because the depth of the two sets of tparms
will always be different.

I'm surprised then that this has an effect for g++.dg/template/nontype12.C.
Ah, seems it's because compare_template_parms returns true if either set
of template parameters contains error_mark_node, for sake of error
recovery.  In this case, the two sets of template parameters for the
ttp in

  template<template<T> class> int bar();

are

  3 D.2778, 2 , 1 T          [original template parameters]
  2 <<< error >>>, 1         [substituted/lowered via T=double]

where the error_mark_node is due to double not being a valid template
parameter before C++20.

Perhaps we should be comparing only the innermost parameters instead?
That way the test will work in non-erroneous cases for at least for ttps with
non-dependent template parameters.

We might also want to consider caching the TEMPLATE_TEMPLATE_PARM node as well,
by adjusting the hunk in 'tsubst':

pt.cc:16234
            if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
                && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
                    r = TEMPLATE_PARM_DESCENDANTS (arg))
                && (TEMPLATE_PARM_LEVEL (r)
                    == TEMPLATE_PARM_LEVEL (arg) - levels))
              /* Cache the simple case of lowering a type parameter.  */
              r = TREE_TYPE (r);

> +	   : same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))))
>      {
>        tree orig_decl = TEMPLATE_PARM_DECL (index);
>  
> diff --git a/gcc/testsuite/g++.dg/template/nontype12.C b/gcc/testsuite/g++.dg/template/nontype12.C
> index e37cf8f7646..6642ffd0a13 100644
> --- a/gcc/testsuite/g++.dg/template/nontype12.C
> +++ b/gcc/testsuite/g++.dg/template/nontype12.C
> @@ -4,7 +4,8 @@
>  template<typename T> struct A
>  {
>    template<T> int foo();                        // { dg-error "double" "" { target c++17_down } }
> -  template<template<T> class> int bar();        // { dg-error "double" "" { target c++17_down } }
> +  template<template<T> class> int bar();        // { dg-bogus {double.*C:7:[^\n]*double} }
> +  // { dg-error "double" "" { target c++17_down } .-1 }
>    template<T> struct X;                         // { dg-error "double" "" { target c++17_down } }
>  };
>  
> 
> base-commit: d7cb9720ed54687bd1135c5e6ef90776a9db0bd5
> -- 
> 2.31.1
> 
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-05-15 14:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03  4:30 [pushed] c++: fix TTP level reduction cache Jason Merrill
2023-05-15 14:41 ` Patrick Palka

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).