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<I, I, I>. 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.
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
@@ -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..3df40f0d22f 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,21 +23436,23 @@ 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<I, I, I>. 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;
+ int err;
if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
- {
- if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
- return NULL_TREE;
- return arg;
- }
+ err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
+ else
+ err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
+ CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p);
- /* If unification failed, we're done. */
- if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
- CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
- return NULL_TREE;
+ if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
+ for (tree level : tree_vec_range (targs))
+ ggc_free (level);
+ ggc_free (targs);
- return arg;
+ return err ? NULL_TREE : arg;
}
/* Given a template type PARM and a class type ARG, find the unique
@@ -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, &level, &idx);
- if (level < levels)
+ if (level > levels)
continue;
/* Keep track of the parameter packs and their corresponding