public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4035] c++: only declare satisfied friends
Date: Mon, 14 Nov 2022 22:52:26 +0000 (GMT)	[thread overview]
Message-ID: <20221114225226.562743858425@sourceware.org> (raw)

https://gcc.gnu.org/g:c41bbfcaf9d6ef5b57a7e89bba70b861c08a686b

commit r13-4035-gc41bbfcaf9d6ef5b57a7e89bba70b861c08a686b
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Nov 11 00:45:02 2022 -1000

    c++: only declare satisfied friends
    
    A friend declaration can only have constraints if it is defined.  If
    multiple instantiations of a class template define the same friend function
    signature, it's an error, but that shouldn't happen if it's constrained to
    only be declared in one instantiation.
    
    Currently we don't mangle requirements, so the foos all mangle the same and
    actually instantiating #1 will break, but for now we can test that they're
    considered distinct.
    
    gcc/cp/ChangeLog:
    
            * pt.cc (tsubst_friend_function): Check satisfaction.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/concepts-friend11.C: New test.

Diff:
---
 gcc/cp/pt.cc                                   |  3 +++
 gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 57917de321f..af96c5ca25f 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -11284,6 +11284,9 @@ tsubst_friend_function (tree decl, tree args)
 	  not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
 	  new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
 	}
+      else if (!constraints_satisfied_p (new_friend))
+	/* Only define a constrained hidden friend when satisfied.  */
+	return error_mark_node;
 
       /* Inside pushdecl_namespace_level, we will push into the
 	 current namespace. However, the friend function should go
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C
new file mode 100644
index 00000000000..0350ac3553e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C
@@ -0,0 +1,21 @@
+// CWG2596
+// { dg-do compile { target c++20 } }
+
+struct Base {};
+
+int foo(Base&) { return 0; } // #0
+
+template<int N>
+struct S : Base {
+  friend int foo(Base&) requires (N == 1) { return 1; }  // #1
+  // friend int foo(Base&) requires (N == 2) { return 3; }  // #2
+};
+
+S<1> s1;
+S<2> s2;          // OK, no conflict between #1 and #0
+int x = foo(s1);  // { dg-error "ambiguous" }
+int y = foo(s2);  // OK, selects #0
+
+// ??? currently the foos all mangle the same, so comment out #2
+// and only test that #1 isn't multiply defined and overloads with #0.
+// The 2596 example does not include #0 and expects both calls to work.

                 reply	other threads:[~2022-11-14 22:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221114225226.562743858425@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).