public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9992] c++: deduction guides and ttp rewriting [PR102479]
@ 2022-05-13 13:40 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-05-13 13:40 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f705d0f2d03cc4d9f890832798b3352553f2262f

commit r11-9992-gf705d0f2d03cc4d9f890832798b3352553f2262f
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Sep 27 16:01:10 2021 -0400

    c++: deduction guides and ttp rewriting [PR102479]
    
    The problem here is ultimately that rewrite_tparm_list when rewriting a
    TEMPLATE_TEMPLATE_PARM introduces a tree cycle in the rewritten
    ttp that structural_comptypes can't cope with.  In particular the
    DECL_TEMPLATE_PARMS of a ttp's TEMPLATE_DECL normally captures an empty
    parameter list at its own level (and so the TEMPLATE_DECL doesn't appear
    in its own DECL_TEMPLATE_PARMS), but rewrite_tparm_list ends up giving
    it a complete parameter list.  In the new testcase below, this causes
    infinite recursion from structural_comptypes when comparing Tmpl<char>
    with Tmpl<long> (where both 'Tmpl's are rewritten ttps).
    
    This patch fixes this by making rewrite_template_parm give a rewritten
    template template parm an empty parameter list at its own level, thereby
    avoiding the tree cycle.  Testing the alias CTAD case revealed that
    we're not setting current_template_parms in alias_ctad_tweaks, which
    this patch also fixes.
    
            PR c++/102479
    
    gcc/cp/ChangeLog:
    
            * pt.c (rewrite_template_parm): Handle single-level tsubst_args.
            Avoid a tree cycle when assigning the DECL_TEMPLATE_PARMS for a
            rewritten ttp.
            (alias_ctad_tweaks): Set current_template_parms accordingly.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/class-deduction12.C: Also test alias CTAD in the
            same way.
            * g++.dg/cpp1z/class-deduction99.C: New test.
    
    (cherry picked from commit 51018dd1395c72b3681ae5f84eceb94320472922)

Diff:
---
 gcc/cp/pt.c                                    | 20 ++++++++++++---
 gcc/testsuite/g++.dg/cpp1z/class-deduction12.C |  6 +++++
 gcc/testsuite/g++.dg/cpp1z/class-deduction99.C | 35 ++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index a8fa2347c8a..ba505820c6a 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -28733,7 +28733,7 @@ rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
 	  const int depth = TMPL_ARGS_DEPTH (tsubst_args);
 	  tree ttargs = make_tree_vec (depth + 1);
 	  for (int i = 0; i < depth; ++i)
-	    TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
+	    TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
 	  TREE_VEC_ELT (ttargs, depth)
 	    = template_parms_level_to_args (ttparms);
 	  // Substitute ttargs into ttparms to fix references to
@@ -28746,8 +28746,17 @@ rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
 	  ttparms = tsubst_template_parms_level (ttparms, ttargs,
 						 complain);
 	  // Finally, tack the adjusted parms onto tparms.
-	  ttparms = tree_cons (size_int (depth), ttparms,
-			       current_template_parms);
+	  ttparms = tree_cons (size_int (level + 1), ttparms,
+			       copy_node (current_template_parms));
+	  // As with all template template parms, the parameter list captured
+	  // by this template template parm that corresponds to its own level
+	  // should be empty.  This avoids infinite recursion when structurally
+	  // comparing two such rewritten template template parms (PR102479).
+	  gcc_assert (!TREE_VEC_LENGTH
+		      (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
+	  gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
+	  TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
+	  // All done.
 	  DECL_TEMPLATE_PARMS (newdecl) = ttparms;
 	}
     }
@@ -29245,6 +29254,11 @@ alias_ctad_tweaks (tree tmpl, tree uguides)
 	      ++ndlen;
 	  tree gtparms = make_tree_vec (natparms + ndlen);
 
+	  /* Set current_template_parms as in build_deduction_guide.  */
+	  auto ctp = make_temp_override (current_template_parms);
+	  current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl));
+	  TREE_VALUE (current_template_parms) = gtparms;
+
 	  /* First copy over the parms of A.  */
 	  for (j = 0; j < natparms; ++j)
 	    TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C
index a31cc1526db..f0d7ea0e16b 100644
--- a/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C
@@ -15,3 +15,9 @@ A a(&i,2,B<42>());
 template <class,class> class same;
 template <class T> class same<T,T> {};
 same<decltype(a), A<int*>> s;
+
+#if __cpp_deduction_guides >= 201907
+template <class T> using C = A<const T*>;
+
+same<decltype(C(&i, 2, B<42>())), A<const int*>> t;
+#endif
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C
new file mode 100644
index 00000000000..6daa4b7c55c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C
@@ -0,0 +1,35 @@
+// PR c++/102479
+// { dg-do compile { target c++17 } }
+
+template<class T> struct A;
+
+template<class T>
+struct tuple {
+  tuple(T);
+
+  template<template<class> class Tmpl>
+  tuple(Tmpl<T>);
+
+  template<template<class> class Tmpl, typename A<Tmpl<char>>::type = 0>
+  tuple(Tmpl<T>);
+
+  template<template<class> class Tmpl, typename A<Tmpl<long>>::type = 0>
+  tuple(Tmpl<T>);
+};
+
+template<class T> struct B { };
+
+using ty1 = tuple<int>;
+using ty1 = decltype(tuple(0));
+using ty1 = decltype(tuple(B<int>{}));
+
+#if __cpp_deduction_guides >= 201907
+template<class T> using const_tuple = tuple<const T>;
+
+using ty2 = const_tuple<int>;
+using ty2 = decltype(const_tuple(0));
+using ty2 = decltype(const_tuple(B<const int>{}));
+
+using ty3 = const_tuple<B<int>>;
+using ty3 = decltype(const_tuple(B<int>{}));
+#endif


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

only message in thread, other threads:[~2022-05-13 13:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13 13:40 [gcc r11-9992] c++: deduction guides and ttp rewriting [PR102479] 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).