public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: diagnostic ICE b/c of empty TPARMS_PRIMARY_TEMPLATE [PR109655]
Date: Fri,  9 Jun 2023 11:19:09 -0400	[thread overview]
Message-ID: <20230609151909.3628123-1-ppalka@redhat.com> (raw)

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	other threads:[~2023-06-09 15:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09 15:19 Patrick Palka [this message]
2023-06-16 13:52 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230609151909.3628123-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).