Re: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

2022-05-12 Thread Patrick Palka via Gcc-patches
On Thu, 12 May 2022, Jason Merrill wrote:

> On 5/12/22 14:14, Patrick Palka wrote:
> > On Thu, 12 May 2022, Patrick Palka wrote:
> > 
> > > After r13-332-g88459c3965e2a2, it looks like we can safely remove the
> > > NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its
> > > semantics.
> > > 
> > > And TMPL_ARGS_LEVEL should verify the level argument is sane in the
> > > one-dimensional vector case.  This change uncovered a couple of latent
> > > bugs: in try_class_unification, we weren't correctly copying
> > > multidimensional targs, and in unify_pack_expansion it seems an
> > > inequality test needs to be reversed (not sure about this latter fix,
> > > the test was added in r8-6178-g2625472ffa519e FWIW).
> > > 
> > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> > > trunk?  Also tested on cmcstl2 and range-v3.
> > > 
> > > gcc/cp/ChangeLog:
> > > 
> > >   * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
> > >   (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
> > >   TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
> > >   * pt.cc (try_class_unification): Correctly copy multidimensional
> > >   targs.
> > >   (unify_pack_expansion): Fix level comparison.
> > > ---
> > >   gcc/cp/cp-tree.h |  5 +++--
> > >   gcc/cp/pt.cc | 12 ++--
> > >   2 files changed, 9 insertions(+), 8 deletions(-)
> > > 
> > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> > > index b6961a796af..f681d32ac93 100644
> > > --- a/gcc/cp/cp-tree.h
> > > +++ b/gcc/cp/cp-tree.h
> > > @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
> > >   /* Nonzero if the template arguments is actually a vector of vectors,
> > >  rather than just a vector.  */
> > >   #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
> > > -  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
> > > +  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
> > >  && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
> > > /* The depth of a template argument vector.  When called directly by
> > > @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
> > >  args is level 1, not level 0.  */
> > >   #define TMPL_ARGS_LEVEL(ARGS, LEVEL)\
> > > (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS)\
> > > -   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
> > > +   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)\
> > > +   : (gcc_checking_assert (LEVEL == 1), (ARGS)))
> > > /* Set the LEVELth level of the template ARGS to VAL.  This macro does
> > >  not work with single-level argument vectors.  */
> > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> > > index 2c7c5f8bb5d..75b21e5c88a 100644
> > > --- a/gcc/cp/pt.cc
> > > +++ b/gcc/cp/pt.cc
> > > @@ -23398,8 +23398,6 @@ static tree
> > >   try_class_unification (tree tparms, tree targs, tree parm, tree arg,
> > >  bool explain_p)
> > >   {
> > > -  tree copy_of_targs;
> > > -
> > > if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
> > >   return NULL_TREE;
> > > else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
> > > @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs,
> > > tree parm, tree arg,
> > >because there are two ways to unify base classes of S<0, 1, 2>
> > >with S.  If we kept the already deduced knowledge, we
> > >would reject the possibility I=1.  */
> > > -  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
> > > +  targs = copy_template_args (targs);
> > > +  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
> > > +targ = NULL_TREE;
> > 
> > It occurred to me that we don't need to make a copy of (the TREE_VECs
> > for) the outer template arguments, so it might be better to use
> > copy_node manually instead of copy_template_args.  But this redundant
> > copying shouldn't matter much if we make sure to ggc_free it afterwards.
> > So here's v2 which additionally makes try_class_unification ggc_free
> > this copy of targs (bootstrap and regtest in progress):
> > 
> > -- >8 --
> > 
> > Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros
> > 
> > After r13-332-g88459c3965e2a2, it looks like we can safely remove the
>

Re: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

2022-05-12 Thread Jason Merrill via Gcc-patches

On 5/12/22 14:14, Patrick Palka wrote:

On Thu, 12 May 2022, Patrick Palka wrote:


After r13-332-g88459c3965e2a2, it looks like we can safely remove the
NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its
semantics.

And TMPL_ARGS_LEVEL should verify the level argument is sane in the
one-dimensional vector case.  This change uncovered a couple of latent
bugs: in try_class_unification, we weren't correctly copying
multidimensional targs, and in unify_pack_expansion it seems an
inequality test needs to be reversed (not sure about this latter fix,
the test was added in r8-6178-g2625472ffa519e FWIW).

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?  Also tested on cmcstl2 and range-v3.

gcc/cp/ChangeLog:

* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
* pt.cc (try_class_unification): Correctly copy multidimensional
targs.
(unify_pack_expansion): Fix level comparison.
---
  gcc/cp/cp-tree.h |  5 +++--
  gcc/cp/pt.cc | 12 ++--
  2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b6961a796af..f681d32ac93 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
  /* Nonzero if the template arguments is actually a vector of vectors,
 rather than just a vector.  */
  #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE)   \
-  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
+  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
  
  /* The depth of a template argument vector.  When called directly by

@@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
 args is level 1, not level 0.  */
  #define TMPL_ARGS_LEVEL(ARGS, LEVEL)  \
(TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS)  \
-   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
+   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)  \
+   : (gcc_checking_assert (LEVEL == 1), (ARGS)))
  
  /* Set the LEVELth level of the template ARGS to VAL.  This macro does

 not work with single-level argument vectors.  */
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2c7c5f8bb5d..75b21e5c88a 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -23398,8 +23398,6 @@ static tree
  try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   bool explain_p)
  {
-  tree copy_of_targs;
-
if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
  return NULL_TREE;
else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
@@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree 
parm, tree arg,
   because there are two ways to unify base classes of S<0, 1, 2>
   with S.  If we kept the already deduced knowledge, we
   would reject the possibility I=1.  */
-  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
+  targs = copy_template_args (targs);
+  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
+targ = NULL_TREE;


It occurred to me that we don't need to make a copy of (the TREE_VECs
for) the outer template arguments, so it might be better to use
copy_node manually instead of copy_template_args.  But this redundant
copying shouldn't matter much if we make sure to ggc_free it afterwards.
So here's v2 which additionally makes try_class_unification ggc_free
this copy of targs (bootstrap and regtest in progress):

-- >8 --

Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

After r13-332-g88459c3965e2a2, it looks like we can safely remove the
NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its
uses easier to reason about.


because we should have given up before trying to check that?


And TMPL_ARGS_LEVEL should verify the level argument is valid in the
one-dimensional vector case.  This change uncovered a couple of latent
issues: in try_class_unification, we weren't correctly copying
multidimensional targs, and in unify_pack_expansion it seems an
inequality test needs to be reversed.  This patch fixes both these
issues, and in passing makes the former function ggc_free the copy of
targs.


OK.


gcc/cp/ChangeLog:

* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
* pt.cc (try_class_unification): Correctly copy multidimensional
targs.  Free the copy of targs.
(unify_pack_expansion): Fix level comparison.
---
  gcc/cp/cp-tree.h |  5 +++--
  gcc/cp/pt.cc | 28 ++--
  2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b6961a796af..c28a3311dde 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@

Re: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

2022-05-12 Thread Patrick Palka via Gcc-patches
On Thu, 12 May 2022, Patrick Palka wrote:

> After r13-332-g88459c3965e2a2, it looks like we can safely remove the
> NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its
> semantics.
> 
> And TMPL_ARGS_LEVEL should verify the level argument is sane in the
> one-dimensional vector case.  This change uncovered a couple of latent
> bugs: in try_class_unification, we weren't correctly copying
> multidimensional targs, and in unify_pack_expansion it seems an
> inequality test needs to be reversed (not sure about this latter fix,
> the test was added in r8-6178-g2625472ffa519e FWIW).
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?  Also tested on cmcstl2 and range-v3.
> 
> gcc/cp/ChangeLog:
> 
>   * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
>   (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
>   TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
>   * pt.cc (try_class_unification): Correctly copy multidimensional
>   targs.
>   (unify_pack_expansion): Fix level comparison.
> ---
>  gcc/cp/cp-tree.h |  5 +++--
>  gcc/cp/pt.cc | 12 ++--
>  2 files changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index b6961a796af..f681d32ac93 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
>  /* Nonzero if the template arguments is actually a vector of vectors,
> rather than just a vector.  */
>  #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE)  \
> -  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
> +  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
> && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
>  
>  /* The depth of a template argument vector.  When called directly by
> @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
> args is level 1, not level 0.  */
>  #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
>(TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
> -   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
> +   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)\
> +   : (gcc_checking_assert (LEVEL == 1), (ARGS)))
>  
>  /* Set the LEVELth level of the template ARGS to VAL.  This macro does
> not work with single-level argument vectors.  */
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 2c7c5f8bb5d..75b21e5c88a 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -23398,8 +23398,6 @@ static tree
>  try_class_unification (tree tparms, tree targs, tree parm, tree arg,
>  bool explain_p)
>  {
> -  tree copy_of_targs;
> -
>if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
>  return NULL_TREE;
>else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
> @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, 
> tree parm, tree arg,
>   because there are two ways to unify base classes of S<0, 1, 2>
>   with S.  If we kept the already deduced knowledge, we
>   would reject the possibility I=1.  */
> -  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
> +  targs = copy_template_args (targs);
> +  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
> +targ = NULL_TREE;

It occurred to me that we don't need to make a copy of (the TREE_VECs
for) the outer template arguments, so it might be better to use
copy_node manually instead of copy_template_args.  But this redundant
copying shouldn't matter much if we make sure to ggc_free it afterwards.
So here's v2 which additionally makes try_class_unification ggc_free
this copy of targs (bootstrap and regtest in progress):

-- >8 --

Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

After r13-332-g88459c3965e2a2, it looks like we can safely remove the
NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its
uses easier to reason about.

And TMPL_ARGS_LEVEL should verify the level argument is valid in the
one-dimensional vector case.  This change uncovered a couple of latent
issues: in try_class_unification, we weren't correctly copying
multidimensional targs, and in unify_pack_expansion it seems an
inequality test needs to be reversed.  This patch fixes both these
issues, and in passing makes the former function ggc_free the copy of
targs.

gcc/cp/ChangeLog:

* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
* pt.cc (try_class_unification): Correctly copy multidimensional
targs.  Free the copy of targs.
(unify_pack_expansion): Fix level comparison.
---
 gcc/cp/cp-tree.h |  5 +++--
 gcc/cp/pt.cc | 28 ++

[PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

2022-05-12 Thread Patrick Palka via Gcc-patches
After r13-332-g88459c3965e2a2, it looks like we can safely remove the
NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its
semantics.

And TMPL_ARGS_LEVEL should verify the level argument is sane in the
one-dimensional vector case.  This change uncovered a couple of latent
bugs: in try_class_unification, we weren't correctly copying
multidimensional targs, and in unify_pack_expansion it seems an
inequality test needs to be reversed (not sure about this latter fix,
the test was added in r8-6178-g2625472ffa519e FWIW).

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?  Also tested on cmcstl2 and range-v3.

gcc/cp/ChangeLog:

* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
* pt.cc (try_class_unification): Correctly copy multidimensional
targs.
(unify_pack_expansion): Fix level comparison.
---
 gcc/cp/cp-tree.h |  5 +++--
 gcc/cp/pt.cc | 12 ++--
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b6961a796af..f681d32ac93 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
 /* Nonzero if the template arguments is actually a vector of vectors,
rather than just a vector.  */
 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE)\
-  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
+  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
&& TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
 
 /* The depth of a template argument vector.  When called directly by
@@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
args is level 1, not level 0.  */
 #define TMPL_ARGS_LEVEL(ARGS, LEVEL)   \
   (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS)   \
-   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
+   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)  \
+   : (gcc_checking_assert (LEVEL == 1), (ARGS)))
 
 /* Set the LEVELth level of the template ARGS to VAL.  This macro does
not work with single-level argument vectors.  */
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2c7c5f8bb5d..75b21e5c88a 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -23398,8 +23398,6 @@ static tree
 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   bool explain_p)
 {
-  tree copy_of_targs;
-
   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
 return NULL_TREE;
   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
@@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree 
parm, tree arg,
  because there are two ways to unify base classes of S<0, 1, 2>
  with S.  If we kept the already deduced knowledge, we
  would reject the possibility I=1.  */
-  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
+  targs = copy_template_args (targs);
+  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
+targ = NULL_TREE;
 
   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
 {
-  if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
+  if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
return NULL_TREE;
   return arg;
 }
 
   /* If unification failed, we're done.  */
-  if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
+  if (unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
 return NULL_TREE;
 
@@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree 
packed_parms,
 
   /* Determine the index and level of this parameter pack.  */
   template_parm_level_and_index (parm_pack, , );
-  if (level < levels)
+  if (level > levels)
continue;
 
   /* Keep track of the parameter packs and their corresponding
-- 
2.36.1.63.gef9b086d95