public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] c++: constrained template friend class matching [PR96830]
@ 2023-03-14 16:41 Patrick Palka
  2023-03-14 16:41 ` [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830] Patrick Palka
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2023-03-14 16:41 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Here when instantiating a constrained template friend naming an already
declared class template, we erroneously pass the existing template's
constraints instead of the friend declaration's constraints to
redeclare_class_template, which causes the constraint comparison check
therein to always be vacuously true, and we fail to diagnose legitimate
constraint mismatches.

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

	PR c++/96830

gcc/cp/ChangeLog:

	* pt.cc (redeclare_class_template): Add missing "of" in
	constraint mismatch diagnostic.
	(tsubst_friend_class): For an already declared class template,
	substitute and pass the friend declaration's constraints to
	redeclare_class_template instead of passing the existing
	template's constraints.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-friend14.C: New test.
---
 gcc/cp/pt.cc                                  |  8 ++++--
 .../g++.dg/cpp2a/concepts-friend14.C          | 26 +++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 85136df1730..053cc52b285 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -6377,7 +6377,7 @@ redeclare_class_template (tree type, tree parms, tree cons)
   if (!cp_tree_equal (req1, req2))
     {
       auto_diagnostic_group d;
-      error_at (input_location, "redeclaration %q#D with different "
+      error_at (input_location, "redeclaration of %q#D with different "
                                 "constraints", tmpl);
       inform (DECL_SOURCE_LOCATION (tmpl),
               "original declaration appeared here");
@@ -11503,7 +11503,11 @@ tsubst_friend_class (tree friend_tmpl, tree args)
 						 tf_warning_or_error);
           location_t saved_input_location = input_location;
           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
-          tree cons = get_constraints (tmpl);
+	  tree cons = get_constraints (friend_tmpl);
+	  ++processing_template_decl;
+	  cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
+					 DECL_FRIEND_CONTEXT (friend_tmpl));
+	  --processing_template_decl;
           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
           input_location = saved_input_location;
 	}
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C
new file mode 100644
index 00000000000..f46c7e4bdfb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C
@@ -0,0 +1,26 @@
+// PR c++/96830
+// { dg-do compile { target c++20 } }
+
+template<class T> requires true
+struct A {
+  template<class U> friend struct A;                   // { dg-error "different constraints" }
+  template<class U> requires (!!true) friend struct A; // { dg-error "different constraints" }
+};
+
+template<class T>
+struct B {
+  template<class U> requires true friend struct A;
+  template<class U> requires true friend struct B; // { dg-error "different constraints" }
+};
+
+template<class T> concept C = true;
+
+template<C T>
+struct D {
+  template<class U> friend struct D; // { dg-error "different constraints" }
+  template<C U> friend struct D;
+};
+
+template struct A<int>;
+template struct B<int>;
+template struct D<int>;
-- 
2.40.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830]
  2023-03-14 16:41 [PATCH 1/2] c++: constrained template friend class matching [PR96830] Patrick Palka
@ 2023-03-14 16:41 ` Patrick Palka
  2023-03-14 21:21   ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2023-03-14 16:41 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

When redeclaring a member of a constrained class template, we need to
repeat the class's constraints, but it turns out we don't verify
anywhere that the repeated constraints match the class's constraints.
A safe place to do so seems to be in push_template_decl, nearby a
similar consistency check for the template parameter list length of a
redeclaration.

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

	PR c++/96830

gcc/cp/ChangeLog:

	* pt.cc (push_inline_template_parms_recursive): Set
	TEMPLATE_PARMS_CONSTRAINTS.
	(push_template_decl): For a redeclaration, verify constraints
	for each enclosing template scope match those of the original
	template declaratation.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-class5.C: New test.
	* g++.dg/cpp2a/concepts-class5a.C: New test.
---
 gcc/cp/pt.cc                                  | 26 +++++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-class5.C  | 37 ++++++++++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C | 38 +++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 053cc52b285..370a4f70f08 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -471,6 +471,8 @@ push_inline_template_parms_recursive (tree parmlist, int levels)
   current_template_parms
     = tree_cons (size_int (current_template_depth + 1),
 		 parms, current_template_parms);
+  TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
+    = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
 
   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
@@ -6134,6 +6136,30 @@ push_template_decl (tree decl, bool is_friend)
 	  DECL_INTERFACE_KNOWN (decl) = 1;
 	  return error_mark_node;
 	}
+
+      /* Check that the constraints for each enclosing template scope are
+	 consistent with the original template declaration.  */
+      if (flag_concepts)
+	{
+	  tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
+	  tree scope_parms = current_template_parms;
+	  if (PRIMARY_TEMPLATE_P (tmpl))
+	    {
+	      decl_parms = TREE_CHAIN (decl_parms);
+	      scope_parms = TREE_CHAIN (scope_parms);
+	    }
+	  while (decl_parms)
+	    {
+	      if (!template_requirements_equivalent_p (decl_parms, scope_parms))
+		{
+		  error ("redeclaration of %qD with different constraints",
+			 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
+		  break;
+		}
+	      decl_parms = TREE_CHAIN (decl_parms);
+	      scope_parms = TREE_CHAIN (scope_parms);
+	    }
+	}
     }
 
   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
new file mode 100644
index 00000000000..5d893d9f6a7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
@@ -0,0 +1,37 @@
+// PR c++/96830
+// { dg-do compile { target c++20 } }
+
+template<class T> concept C = true;
+
+template<C T> requires true
+struct A {
+  void f();
+  template<class U> void g();
+
+  struct B;
+  template<class U> struct C;
+
+  static int D;
+  template<class U> static int E;
+};
+
+template<C T>
+void A<T>::f() { }  // { dg-error "different constraints" }
+
+template<class T> requires true
+template<class U>
+void A<T>::g() { }  // { dg-error "different constraints" }
+
+template<C T> requires (!!true)
+struct A<T>::B { }; // { dg-error "different constraints" }
+
+template<class T> requires true
+template<class U>
+struct A<T>::C { }; // { dg-error "different constraints" }
+
+template<C T>
+int A<T>::D = 0;    // { dg-error "different constraints" }
+
+template<class T> requires true
+template<class U>
+int A<T>::E = 0;    // { dg-error "different constraints" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C
new file mode 100644
index 00000000000..a6a3684c258
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C
@@ -0,0 +1,38 @@
+// PR c++/96830
+// A valid version of concepts-class5.C.
+// { dg-do compile { target c++20 } }
+
+template<class T> concept C = true;
+
+template<C T> requires true
+struct A {
+  void f();
+  template<class U> void g();
+
+  struct B;
+  template<class U> struct C;
+
+  static int D;
+  template<class U> static int E;
+};
+
+template<C T> requires true
+void A<T>::f() { }
+
+template<C T> requires true
+template<class U>
+void A<T>::g() { }
+
+template<C T> requires true
+struct A<T>::B { };
+
+template<C T> requires true
+template<class U>
+struct A<T>::C { };
+
+template<C T> requires true
+int A<T>::D = 0;
+
+template<C T> requires true
+template<class U>
+int A<T>::E = 0;
-- 
2.40.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830]
  2023-03-14 16:41 ` [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830] Patrick Palka
@ 2023-03-14 21:21   ` Jason Merrill
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2023-03-14 21:21 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 3/14/23 12:41, Patrick Palka wrote:
> When redeclaring a member of a constrained class template, we need to
> repeat the class's constraints, but it turns out we don't verify
> anywhere that the repeated constraints match the class's constraints.
> A safe place to do so seems to be in push_template_decl, nearby a
> similar consistency check for the template parameter list length of a
> redeclaration.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/12?

Both patches are OK for trunk, but not 12; we tend not to backport fixes 
for accepts-invalid bugs, especially if they aren't regressions.  We 
don't want to add new diagnostics to an active release branch.

> 	PR c++/96830
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (push_inline_template_parms_recursive): Set
> 	TEMPLATE_PARMS_CONSTRAINTS.
> 	(push_template_decl): For a redeclaration, verify constraints
> 	for each enclosing template scope match those of the original
> 	template declaratation.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-class5.C: New test.
> 	* g++.dg/cpp2a/concepts-class5a.C: New test.
> ---
>   gcc/cp/pt.cc                                  | 26 +++++++++++++
>   gcc/testsuite/g++.dg/cpp2a/concepts-class5.C  | 37 ++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C | 38 +++++++++++++++++++
>   3 files changed, 101 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 053cc52b285..370a4f70f08 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -471,6 +471,8 @@ push_inline_template_parms_recursive (tree parmlist, int levels)
>     current_template_parms
>       = tree_cons (size_int (current_template_depth + 1),
>   		 parms, current_template_parms);
> +  TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
> +    = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
>     TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
>   
>     begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
> @@ -6134,6 +6136,30 @@ push_template_decl (tree decl, bool is_friend)
>   	  DECL_INTERFACE_KNOWN (decl) = 1;
>   	  return error_mark_node;
>   	}
> +
> +      /* Check that the constraints for each enclosing template scope are
> +	 consistent with the original template declaration.  */
> +      if (flag_concepts)
> +	{
> +	  tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
> +	  tree scope_parms = current_template_parms;
> +	  if (PRIMARY_TEMPLATE_P (tmpl))
> +	    {
> +	      decl_parms = TREE_CHAIN (decl_parms);
> +	      scope_parms = TREE_CHAIN (scope_parms);
> +	    }
> +	  while (decl_parms)
> +	    {
> +	      if (!template_requirements_equivalent_p (decl_parms, scope_parms))
> +		{
> +		  error ("redeclaration of %qD with different constraints",
> +			 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
> +		  break;
> +		}
> +	      decl_parms = TREE_CHAIN (decl_parms);
> +	      scope_parms = TREE_CHAIN (scope_parms);
> +	    }
> +	}
>       }
>   
>     gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
> new file mode 100644
> index 00000000000..5d893d9f6a7
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C
> @@ -0,0 +1,37 @@
> +// PR c++/96830
> +// { dg-do compile { target c++20 } }
> +
> +template<class T> concept C = true;
> +
> +template<C T> requires true
> +struct A {
> +  void f();
> +  template<class U> void g();
> +
> +  struct B;
> +  template<class U> struct C;
> +
> +  static int D;
> +  template<class U> static int E;
> +};
> +
> +template<C T>
> +void A<T>::f() { }  // { dg-error "different constraints" }
> +
> +template<class T> requires true
> +template<class U>
> +void A<T>::g() { }  // { dg-error "different constraints" }
> +
> +template<C T> requires (!!true)
> +struct A<T>::B { }; // { dg-error "different constraints" }
> +
> +template<class T> requires true
> +template<class U>
> +struct A<T>::C { }; // { dg-error "different constraints" }
> +
> +template<C T>
> +int A<T>::D = 0;    // { dg-error "different constraints" }
> +
> +template<class T> requires true
> +template<class U>
> +int A<T>::E = 0;    // { dg-error "different constraints" }
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C
> new file mode 100644
> index 00000000000..a6a3684c258
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C
> @@ -0,0 +1,38 @@
> +// PR c++/96830
> +// A valid version of concepts-class5.C.
> +// { dg-do compile { target c++20 } }
> +
> +template<class T> concept C = true;
> +
> +template<C T> requires true
> +struct A {
> +  void f();
> +  template<class U> void g();
> +
> +  struct B;
> +  template<class U> struct C;
> +
> +  static int D;
> +  template<class U> static int E;
> +};
> +
> +template<C T> requires true
> +void A<T>::f() { }
> +
> +template<C T> requires true
> +template<class U>
> +void A<T>::g() { }
> +
> +template<C T> requires true
> +struct A<T>::B { };
> +
> +template<C T> requires true
> +template<class U>
> +struct A<T>::C { };
> +
> +template<C T> requires true
> +int A<T>::D = 0;
> +
> +template<C T> requires true
> +template<class U>
> +int A<T>::E = 0;


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-14 21:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 16:41 [PATCH 1/2] c++: constrained template friend class matching [PR96830] Patrick Palka
2023-03-14 16:41 ` [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830] Patrick Palka
2023-03-14 21:21   ` Jason Merrill

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).