]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Stream DECL_CONTEXT for template template parms [PR98881]
authorNathaniel Shead <nathanieloshead@gmail.com>
Tue, 5 Mar 2024 04:17:09 +0000 (15:17 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Thu, 7 Mar 2024 10:59:31 +0000 (21:59 +1100)
When streaming in a nested template-template parameter as in the
attached testcase, we end up reaching the containing template-template
parameter in 'tpl_parms_fini'. We should not set the DECL_CONTEXT to
this (nested) template-template parameter, as it should already be the
struct that the outer template-template parameter is declared on.

The precise logic for what DECL_CONTEXT should be for a template
template parameter in various situations seems rather obscure. Rather
than trying to determine the assumptions that need to hold, it seems
simpler to just always re-stream the DECL_CONTEXT as needed for now.

PR c++/98881

gcc/cp/ChangeLog:

* module.cc (trees_out::tpl_parms_fini): Stream out DECL_CONTEXT
for template template parameters.
(trees_in::tpl_parms_fini): Read it.

gcc/testsuite/ChangeLog:

* g++.dg/modules/tpl-tpl-parm-3.h: New test.
* g++.dg/modules/tpl-tpl-parm-3_a.H: New test.
* g++.dg/modules/tpl-tpl-parm-3_b.C: New test.
* g++.dg/modules/tpl-tpl-parm-3_c.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Patrick Palka <ppalka@redhat.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3.h [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_c.C [new file with mode: 0644]

index 80b63a70a62ced6c49a8004a1da85d606f9a34ec..f7e8b357fc2d5d6a5f0b5ef74bba8ce24c7b7853 100644 (file)
@@ -10126,23 +10126,12 @@ trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
          tree dflt = TREE_PURPOSE (parm);
          tree_node (dflt);
 
-         if (streaming_p ())
-           {
-             tree decl = TREE_VALUE (parm);
-             if (TREE_CODE (decl) == TEMPLATE_DECL)
-               {
-                 tree ctx = DECL_CONTEXT (decl);
-                 tree inner = DECL_TEMPLATE_RESULT (decl);
-                 tree tpi = (TREE_CODE (inner) == TYPE_DECL
-                             ? TEMPLATE_TYPE_PARM_INDEX (TREE_TYPE (decl))
-                             : DECL_INITIAL (inner));
-                 bool original = (TEMPLATE_PARM_LEVEL (tpi)
-                                  == TEMPLATE_PARM_ORIG_LEVEL (tpi));
-                 /* Original template template parms have a context
-                    of their owning template.  Reduced ones do not.  */
-                 gcc_checking_assert (original ? ctx == tmpl : !ctx);
-               }
-           }
+         /* Template template parameters need a context of their owning
+            template. This is quite tricky to infer correctly on stream-in
+            (see PR c++/98881) so we'll just provide it directly.  */
+         tree decl = TREE_VALUE (parm);
+         if (TREE_CODE (decl) == TEMPLATE_DECL)
+           tree_node (DECL_CONTEXT (decl));
        }
     }
 }
@@ -10160,24 +10149,14 @@ trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
        {
          tree parm = TREE_VEC_ELT (vec, ix);
          tree dflt = tree_node ();
-         if (get_overrun ())
-           return false;
          TREE_PURPOSE (parm) = dflt;
 
          tree decl = TREE_VALUE (parm);
          if (TREE_CODE (decl) == TEMPLATE_DECL)
-           {
-             tree inner = DECL_TEMPLATE_RESULT (decl);
-             tree tpi = (TREE_CODE (inner) == TYPE_DECL
-                         ? TEMPLATE_TYPE_PARM_INDEX (TREE_TYPE (decl))
-                         : DECL_INITIAL (inner));
-             bool original = (TEMPLATE_PARM_LEVEL (tpi)
-                              == TEMPLATE_PARM_ORIG_LEVEL (tpi));
-             /* Original template template parms have a context
-                of their owning template.  Reduced ones do not.  */
-             if (original)
-               DECL_CONTEXT (decl) = tmpl;
-           }
+           DECL_CONTEXT (decl) = tree_node ();
+
+         if (get_overrun ())
+           return false;
        }
     }
   return true;
diff --git a/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3.h b/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3.h
new file mode 100644 (file)
index 0000000..b0dcf35
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/98881
+
+template <typename P> struct X {};
+
+template<template <typename> typename TT>
+struct X<TT<int>> {
+  template<template <typename> typename UU>
+  void f (X<UU<int>>&);
+};
+
+template<template<class> class TT> struct Y;
+template<template<class> class UU> struct Y { };
diff --git a/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_a.H b/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_a.H
new file mode 100644 (file)
index 0000000..4746408
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/98881
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+#include "tpl-tpl-parm-3.h"
diff --git a/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_b.C b/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_b.C
new file mode 100644 (file)
index 0000000..1e3cc6c
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/98881
+// { dg-additional-options "-fmodules-ts -fno-module-lazy" }
+
+#include "tpl-tpl-parm-3.h"
+import "tpl-tpl-parm-3_a.H";
diff --git a/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_c.C b/gcc/testsuite/g++.dg/modules/tpl-tpl-parm-3_c.C
new file mode 100644 (file)
index 0000000..5edba6c
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/98881
+// { dg-additional-options "-fmodules-ts" }
+
+import "tpl-tpl-parm-3_a.H";
+
+template <typename T> struct A {};
+template <typename T> struct B {};
+
+void foo() {
+  X<A<int>> a;
+  X<B<int>> b;
+  a.f(b);
+
+  Y<A> y;
+}