[gcc(refs/vendors/redhat/heads/gcc-8-branch)] PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.

Jakub Jelinek jakub@gcc.gnu.org
Thu Sep 17 16:47:35 GMT 2020


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

commit e0939f9565b715bcb8d20073984073e3478b6ff5
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Feb 26 00:33:52 2020 -0500

    PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
    
    After r269667 which introduced joust_maybe_elide_copy, in C++17 we can elide
    a constructor if it uses a conversion function that returns a prvalue, and
    use the conversion function in its stead.
    
    This eliding means that if we have a candidate that previously didn't have
    ->second_conv, it can have it after the elision.  This confused the
    -Wconversion warning because it was assuming that if cand1->second_conv is
    non-null, so is cand2->second_conv.  Here cand1->second_conv was non-null
    but cand2->second_conv remained null, so it crashed in compare_ics.
    
    I checked with clang that both compilers call A::operator B() in C++17 and
    B::B(A const &) otherwise.
    
    gcc/cp/ChangeLog
    2020-02-26  Marek Polacek  <polacek@redhat.com>
    
            PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
            * call.c (joust): Don't attempt to warn if ->second_conv is null.
    
    gcc/testsuite/ChangeLog
    2020-02-26  Marek Polacek  <polacek@redhat.com>
    
            PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
            * g++.dg/cpp0x/overload-conv-4.C: New test.

Diff:
---
 gcc/cp/ChangeLog                             |  5 +++++
 gcc/cp/call.c                                |  4 +++-
 gcc/testsuite/ChangeLog                      |  5 +++++
 gcc/testsuite/g++.dg/cpp0x/overload-conv-4.C | 23 +++++++++++++++++++++++
 4 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 260f38f0b58..1b5e2bb2cde 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+	PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
+	* call.c (joust): Don't attempt to warn if ->second_conv is null.
+
 2020-02-26  Jason Merrill  <jason@redhat.com>
 
 	PR c++/86521 - C++17 copy elision in initialization by constructor.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 3b0b216d2ea..22de5c70425 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -10240,7 +10240,9 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
      either between a constructor and a conversion op, or between two
      conversion ops.  */
   if ((complain & tf_warning)
-      && winner && warn_conversion && cand1->second_conv
+      /* In C++17, the constructor might have been elided, which means that
+	 an originally null ->second_conv could become non-null.  */
+      && winner && warn_conversion && cand1->second_conv && cand2->second_conv
       && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
       && winner != compare_ics (cand1->second_conv, cand2->second_conv))
     {
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index d94c604ca12..be5addaffd0 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+	PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
+	* g++.dg/cpp0x/overload-conv-4.C: New test.
+
 2020-02-25  Alexandre Oliva <aoliva@redhat.com>
 
 	PR c++/86747
diff --git a/gcc/testsuite/g++.dg/cpp0x/overload-conv-4.C b/gcc/testsuite/g++.dg/cpp0x/overload-conv-4.C
new file mode 100644
index 00000000000..6fcdbbaa6a4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/overload-conv-4.C
@@ -0,0 +1,23 @@
+// PR c++/90998 - ICE with copy elision in init by ctor and -Wconversion.
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wconversion" }
+
+struct B;
+
+struct A {
+    operator B();
+};
+
+struct B {
+    B(A const &rs);
+    B(B const &rs);
+};
+
+B
+f (A x)
+{
+  // C++14: we call B::B(A const &)
+  // C++17: we call A::operator B()
+  return B(x); // { dg-warning "choosing .A::operator B\\(\\). over .B::B\\(const A&\\)" "" { target c++17 } }
+  // { dg-warning "for conversion from .A. to .B." "" { target c++17 } .-1 }
+}


More information about the Gcc-cvs mailing list