When defining a previously declared class template, we neglect to set
TPARMS_PRIMARY_TEMPLATE for the in-scope template parameters, which the
class members go on to inherit, and so the members' DECL_TEMPLATE_PARMS
will have empty TPARMS_PRIMARY_TEMPLATE at those levels as well.  This
causes us to crash when diagnosing a constraint mismatch for an
out-of-line declaration of a member of a constrained class template.

This patch fixes this by walking the context to get at the corresponding
primary template instead.  I spent a while trying to get us to set
TPARMS_PRIMARY_TEMPLATE for templated class definitions that are
redeclarations, but it proved to be hairy in particular for partial
specializations and nested templates.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/13?

        PR c++/109655

gcc/cp/ChangeLog:

        * pt.cc (push_template_decl): Handle TPARMS_PRIMARY_TEMPLATE
        being empty when diagnosing a constraint mismatch for an
        enclosing template scope.  Don't bother checking constraints
        if DECL_PARMS and SCOPE_PARMS are the same.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/concepts-class6.C: New test.
        * g++.dg/cpp2a/concepts-class6a.C: New test.
---
 gcc/cp/pt.cc                                  | 19 +++++++--
 gcc/testsuite/g++.dg/cpp2a/concepts-class6.C  | 30 ++++++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C | 40 +++++++++++++++++++
 3 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class6.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 17bf4d24151..f913b248345 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -6155,12 +6155,25 @@ push_template_decl (tree decl, bool is_friend)
              decl_parms = TREE_CHAIN (decl_parms);
              scope_parms = TREE_CHAIN (scope_parms);
            }
-         while (decl_parms)
+         while (decl_parms && decl_parms != scope_parms)
            {
              if (!template_requirements_equivalent_p (decl_parms, scope_parms))
                {
-                 error ("redeclaration of %qD with different constraints",
-                        TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
+                 tree td = TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms));
+                 if (!td)
+                   {
+                     /* FIXME: TPARMS_PRIMARY_TEMPLATE doesn't always get
+                        set for enclosing template scopes.  Work around
+                        this by walking the context to obtain the relevant
+                        (primary) template whose constraints we mismatch.  */
+                     int level = TMPL_PARMS_DEPTH (decl_parms);
+                     td = TYPE_TI_TEMPLATE (ctx);
+                     while (!PRIMARY_TEMPLATE_P (td)
+                            || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))
+                                != level))
+                       td = TYPE_TI_TEMPLATE (DECL_CONTEXT (td));
+                   }
+                 error ("redeclaration of %qD with different constraints", td);
                  break;
                }
              decl_parms = TREE_CHAIN (decl_parms);
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C 
b/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C
new file mode 100644
index 00000000000..dcef6a2c9d4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C
@@ -0,0 +1,30 @@
+// PR c++/109655
+// { dg-do compile { target c++20 } }
+
+class C {
+  template<class>
+  requires true
+  friend class D;
+
+  template<class>
+  requires true
+  class E;
+};
+
+template<class>
+requires true
+class D {
+  void f();
+};
+
+template<class T>
+void D<T>::f() { } // { dg-error "class D' with different constraints" }
+
+template<class>
+requires true
+class C::E {
+  void f();
+};
+
+template<class T>
+void C::E<T>::f() { } // { dg-error "class C::E' with different constraints" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C 
b/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C
new file mode 100644
index 00000000000..751d13cdf6c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C
@@ -0,0 +1,40 @@
+// PR c++/109655
+// { dg-do compile { target c++20 } }
+
+template<class T>
+requires true
+class C {
+  class D;
+
+  template<class>
+  requires (!!true)
+  class E;
+};
+
+template<class T>
+requires true
+class C<T>::D {
+  void f();
+};
+
+template<class T>  // missing "requires true"
+void C<T>::D::f() { } // { dg-error "class C' with different constraints" }
+
+template<class T>
+requires true
+template<class U>
+requires (!!true)
+class C<T>::E {
+  void f();
+  void g();
+};
+
+template<class T>
+requires true
+template<class U>
+void C<T>::E<U>::f() { } // { dg-error "class C<T>::E' with different 
constraints" }
+
+template<class T>
+template<class U>
+requires (!!true)
+void C<T>::E<U>::g() { } // { dg-error "class C' with different constraints" }
-- 
2.41.0.rc1.10.g9e49351c30

Reply via email to