public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: template friend with variadic constraints [PR108066]
@ 2022-12-12 17:20 Patrick Palka
  2022-12-15 16:22 ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Patrick Palka @ 2022-12-12 17:20 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

When instantiating a constrained hidden template friend, we need to
substitute into its constraints for sake of declaration matching.  For
this substitution we use a full argument vector whose outer levels
correspond to the class's arguments and innermost level corresponds to
the template's level-lowered generic arguments.

But for A<int>::f here, for which the relevant argument vector is
{{int}, {Us...}}, this substitution triggers the assert in
use_pack_expansion_extra_args_p since one argument is a pack expansion
and the other isn't.

And for A<int, int>::f, for which the relevant argument vector is
{{int, int}, {Us...}}, the use_pack_expansion_extra_args_p assert would
also trigger but we first get a bogus "mismatched argument pack lengths"
error from tsubst_pack_expansion.

These might ultimately be bugs in tsubst_pack_expansion, but it seems
we can work around them by tweaking the constraint substitution in
maybe_substitute_reqs_for to only use the friend's outer arguments in
the case of a template friend.  This should be otherwise equivalent to
substituting using the full arguments, since the template's innermost
arguments are just its generic arguments with level=1.

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

	PR c++/108066
	PR c++/108067

gcc/cp/ChangeLog:

	* constraint.cc (maybe_substitute_reqs_for): For a template
	friend, substitute using only its outer arguments.  Remove
	dead uses_template_parms test.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-friend12.C: New test.
---
 gcc/cp/constraint.cc                          |  8 +++++++
 .../g++.dg/cpp2a/concepts-friend12.C          | 22 +++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend12.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d4cd92ec3b4..f9d1009c9fe 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -1352,6 +1352,14 @@ maybe_substitute_reqs_for (tree reqs, const_tree decl)
       tree tmpl = DECL_TI_TEMPLATE (decl);
       tree gargs = generic_targs_for (tmpl);
       processing_template_decl_sentinel s;
+      if (PRIMARY_TEMPLATE_P (tmpl))
+	{
+	  if (TEMPLATE_ARGS_DEPTH (gargs) == 1)
+	    return reqs;
+	  ++processing_template_decl;
+	  gargs = copy_node (gargs);
+	  --TREE_VEC_LENGTH (gargs);
+	}
       if (uses_template_parms (gargs))
 	++processing_template_decl;
       reqs = tsubst_constraint (reqs, gargs,
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend12.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend12.C
new file mode 100644
index 00000000000..95973842afb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend12.C
@@ -0,0 +1,22 @@
+// PR c++/108066
+// PR c++/108067
+// { dg-do compile { target c++20 } }
+
+template<class T, class U>
+concept C = __is_same(T, U);
+
+template<class... Ts>
+struct A {
+  template<class... Us>
+    requires (... && C<Ts, Us>)
+  friend void f(A, A<Us...>) { }
+};
+
+int main() {
+  A<int> x;
+  f(x, x);
+  A<int, int> y;
+  f(y, y);
+  A<char> z;
+  f(x, z); // { dg-error "no match" }
+}
-- 
2.39.0.rc2.23.g694cb1b2ab


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

end of thread, other threads:[~2022-12-22 21:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-12 17:20 [PATCH] c++: template friend with variadic constraints [PR108066] Patrick Palka
2022-12-15 16:22 ` Jason Merrill
2022-12-15 19:04   ` Patrick Palka
2022-12-15 19:31     ` Patrick Palka
2022-12-15 22:29       ` Jason Merrill
2022-12-22 17:34         ` Patrick Palka
2022-12-22 21:00           ` 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).