public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-377] c++: tighten TMPL_ARGS_LEVEL macro
@ 2022-05-12 20:52 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-05-12 20:52 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:3e948d645bc9086eee28407fcc67d05b3450bb78

commit r13-377-g3e948d645bc9086eee28407fcc67d05b3450bb78
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu May 12 16:52:06 2022 -0400

    c++: tighten TMPL_ARGS_LEVEL macro
    
    This patch makes TMPL_ARGS_LEVEL verify the level argument is valid when
    the one-dimensional vector case.  Doing so uncovered a couple of latent
    issues: in try_class_unification, we weren't correctly copying targs
    when it's two-dimensional, and in unify_pack_expansion it seems an
    inequality test needs to be reversed.  This patch fixes both issues, and
    in passing makes the former function free the temporary copy of targs.
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (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.

Diff:
---
 gcc/cp/cp-tree.h |  3 ++-
 gcc/cp/pt.cc     | 28 ++++++++++++++--------------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b6961a796af..b2df6fc0ad4 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -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 06b4a7d197a..fa05e9134df 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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-12 20:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-12 20:52 [gcc r13-377] c++: tighten TMPL_ARGS_LEVEL macro 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).