public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: fix ICE in joust_maybe_elide_copy [PR106675]
@ 2023-02-13 17:06 Marek Polacek
  2023-02-14 21:59 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2023-02-13 17:06 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

joust_maybe_elide_copy checks that the last conversion in the ICS for
the first argument is ck_ref_bind, which is reasonable, because we've
checked that we're dealing with a copy/move constructor.  But it can
also happen that we couldn't figure out which conversion function is
better to convert the argument, as in this testcase: joust couldn't
decide if we should go with

  operator foo &()

or

  operator foo const &()

so we get a ck_ambig, which then upsets joust_maybe_elide_copy.  Since
a ck_ambig can validly occur, I think we should just return early, as
in the patch below.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?

	PR c++/106675

gcc/cp/ChangeLog:

	* call.cc (joust_maybe_elide_copy): Return false for ck_ambig.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/overload-conv-5.C: New test.
---
 gcc/cp/call.cc                               |  2 ++
 gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C | 21 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index a349d8e79db..048b2b052f8 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -12542,6 +12542,8 @@ joust_maybe_elide_copy (z_candidate *&cand)
   if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
     return false;
   conversion *conv = cand->convs[0];
+  if (conv->kind == ck_ambig)
+    return false;
   gcc_checking_assert (conv->kind == ck_ref_bind);
   conv = next_conversion (conv);
   if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
diff --git a/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C b/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C
new file mode 100644
index 00000000000..b1e7766e42b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C
@@ -0,0 +1,21 @@
+// PR c++/106675
+// { dg-do compile { target c++11 } }
+
+struct foo {
+    int n_;
+    foo(int n) : n_(n) {}
+};
+
+struct bar {
+    int n_;
+
+    operator foo() const {
+        return foo(n_);
+    }
+    operator foo &() { return *reinterpret_cast<foo *>(n_); }
+    operator foo const &() = delete;
+
+    void crashgcc() {
+        foo tmp(*this); // { dg-error "ambiguous" }
+    }
+};

base-commit: 72ae1e5635648bd3f6a5760ca46d531ad1f2c6b1
-- 
2.39.1


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

* Re: [PATCH] c++: fix ICE in joust_maybe_elide_copy [PR106675]
  2023-02-13 17:06 [PATCH] c++: fix ICE in joust_maybe_elide_copy [PR106675] Marek Polacek
@ 2023-02-14 21:59 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-02-14 21:59 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 2/13/23 09:06, Marek Polacek wrote:
> joust_maybe_elide_copy checks that the last conversion in the ICS for
> the first argument is ck_ref_bind, which is reasonable, because we've
> checked that we're dealing with a copy/move constructor.  But it can
> also happen that we couldn't figure out which conversion function is
> better to convert the argument, as in this testcase: joust couldn't
> decide if we should go with
> 
>    operator foo &()
> 
> or
> 
>    operator foo const &()
> 
> so we get a ck_ambig, which then upsets joust_maybe_elide_copy.  Since
> a ck_ambig can validly occur, I think we should just return early, as
> in the patch below.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?

OK.

> 	PR c++/106675
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (joust_maybe_elide_copy): Return false for ck_ambig.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/overload-conv-5.C: New test.
> ---
>   gcc/cp/call.cc                               |  2 ++
>   gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C | 21 ++++++++++++++++++++
>   2 files changed, 23 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index a349d8e79db..048b2b052f8 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -12542,6 +12542,8 @@ joust_maybe_elide_copy (z_candidate *&cand)
>     if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
>       return false;
>     conversion *conv = cand->convs[0];
> +  if (conv->kind == ck_ambig)
> +    return false;
>     gcc_checking_assert (conv->kind == ck_ref_bind);
>     conv = next_conversion (conv);
>     if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C b/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C
> new file mode 100644
> index 00000000000..b1e7766e42b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/overload-conv-5.C
> @@ -0,0 +1,21 @@
> +// PR c++/106675
> +// { dg-do compile { target c++11 } }
> +
> +struct foo {
> +    int n_;
> +    foo(int n) : n_(n) {}
> +};
> +
> +struct bar {
> +    int n_;
> +
> +    operator foo() const {
> +        return foo(n_);
> +    }
> +    operator foo &() { return *reinterpret_cast<foo *>(n_); }
> +    operator foo const &() = delete;
> +
> +    void crashgcc() {
> +        foo tmp(*this); // { dg-error "ambiguous" }
> +    }
> +};
> 
> base-commit: 72ae1e5635648bd3f6a5760ca46d531ad1f2c6b1


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

end of thread, other threads:[~2023-02-14 22:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-13 17:06 [PATCH] c++: fix ICE in joust_maybe_elide_copy [PR106675] Marek Polacek
2023-02-14 21:59 ` 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).