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-7470] c++: fix explicit/copy problem [PR109247]
Date: Fri, 23 Jun 2023 16:43:26 +0000 (GMT)	[thread overview]
Message-ID: <20230623164326.4F4343858C3A@sourceware.org> (raw)

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

commit r13-7470-gdc7f1bfbe5999e4639cf3f3afe70043b49352fdf
Author: Jason Merrill <jason@redhat.com>
Date:   Fri May 26 12:28:15 2023 -0400

    c++: fix explicit/copy problem [PR109247]
    
    In the testcase, the user wants the assignment to use the operator= declared
    in the class, but because [over.match.list] says that explicit constructors
    are also considered for list-initialization, as affirmed in CWG1228, we end
    up choosing the implicitly-declared copy assignment operator, using the
    explicit constructor template for the argument, which is ill-formed.  Other
    implementations haven't implemented CWG1228, so we keep getting bug reports.
    
    Discussion in CWG led to the idea for this targeted relaxation: if we use an
    explicit constructor for the conversion to the argument of a copy or move
    special member function, that makes the candidate worse than another.
    
            DR 2735
            PR c++/109247
    
    gcc/cp/ChangeLog:
    
            * call.cc (sfk_copy_or_move): New.
            (joust): Add tiebreaker for explicit conv and copy ctor.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/initlist-explicit3.C: New test.

Diff:
---
 gcc/cp/call.cc                                  | 31 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C | 15 ++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index ee290fc03c4..867d7a5bee6 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -12608,6 +12608,17 @@ cand_parms_match (z_candidate *c1, z_candidate *c2)
   return compparms (parms1, parms2);
 }
 
+/* True iff FN is a copy or move constructor or assignment operator.  */
+
+static bool
+sfk_copy_or_move (tree fn)
+{
+  if (TREE_CODE (fn) != FUNCTION_DECL)
+    return false;
+  special_function_kind sfk = special_function_p (fn);
+  return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
+}
+
 /* Compare two candidates for overloading as described in
    [over.match.best].  Return values:
 
@@ -12907,6 +12918,26 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
 	return winner;
     }
 
+  /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
+     explicit conversion (due to list-initialization) is worse.  */
+  {
+    z_candidate *sp = nullptr;
+    if (sfk_copy_or_move (cand1->fn))
+      sp = cand1;
+    if (sfk_copy_or_move (cand2->fn))
+      sp = sp ? nullptr : cand2;
+    if (sp)
+      {
+	conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
+	if (conv->user_conv_p)
+	  for (; conv; conv = next_conversion (conv))
+	    if (conv->kind == ck_user
+		&& DECL_P (conv->cand->fn)
+		&& DECL_NONCONVERTING_P (conv->cand->fn))
+	      return (sp == cand1) ? -1 : 1;
+      }
+  }
+
   /* or, if not that,
      F1 is a non-template function and F2 is a template function
      specialization.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C
new file mode 100644
index 00000000000..b0c92784566
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C
@@ -0,0 +1,15 @@
+// PR c++/109247
+// { dg-do compile { target c++11 } }
+
+template <typename _Tp> struct optional {
+  template <typename _Up> explicit optional(_Up);
+  template <typename _Up = _Tp> void operator=(_Up);
+};
+int setPattern_pattern;
+struct SourceBrush {
+  struct Brush {
+    int brush;
+  };
+  void setPattern() { m_brush = {setPattern_pattern}; }
+  optional<Brush> m_brush;
+};

                 reply	other threads:[~2023-06-23 16:43 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=20230623164326.4F4343858C3A@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).