public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>, Patrick Palka <ppalka@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
	Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [PATCH v2] c++: Instantiate less when evaluating __is_convertible
Date: Mon, 26 Sep 2022 12:41:04 -0400	[thread overview]
Message-ID: <c5b9e0f4-fa08-a28f-309d-b9b8e21b4f7d@redhat.com> (raw)
In-Reply-To: <YzHSgNWwCii2jawR@redhat.com>

On 9/26/22 12:25, Marek Polacek wrote:
> On Mon, Sep 26, 2022 at 11:51:30AM -0400, Patrick Palka wrote:
>> On Mon, 26 Sep 2022, Marek Polacek via Gcc-patches wrote:
>>
>>> Jon reported that evaluating __is_convertible in this test leads to
>>> instantiating char_traits<char>::eq, which is invalid (because we
>>> are trying to call a member function on a char) and so we fail to
>>> compile the test.  __is_convertible doesn't and shouldn't need to
>>> instantiate so much, so let's limit it with a cp_unevaluated guard.
>>>
>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>>>
>>> 	PR c++/106784
>>>
>>> gcc/cp/ChangeLog:
>>>
>>> 	* method.cc (is_convertible): Use cp_unevaluated.
>>
>> I think is_nothrow_convertible would need cp_unevaluated too (or maybe we
>> should define is_nothrow_convertible in terms of is_convertible).
> 
> /facepalm, that's what I get by not using a single implementation for both!
>   
>> And the testcase can probably be minimized to something like:
>>
>>    struct A;
>>    struct B { template<class T> B(const T&) noexcept { T::nonexistent; } };
>>
>>    static_assert(__is_convertible(const A&, B));
>>    static_assert(__is_nothrow_convertible(const A&, B));
> 
> Adjusted, thanks.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> -- >8 --
> Jon reported that evaluating __is_convertible in a test led to
> instantiating something ill-formed and so we failed to compile the test.
> __is_convertible doesn't and shouldn't need to instantiate so much, so
> let's limit it with a cp_unevaluated guard.  Use a helper function to
> implement both built-ins.
> 
> 	PR c++/106784
> 
> gcc/cp/ChangeLog:
> 
> 	* method.cc (is_convertible_helper): New.
> 	(is_convertible): Use it.
> 	(is_nothrow_convertible): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/is_convertible3.C: New test.
> 	* g++.dg/ext/is_nothrow_convertible3.C: New test.
> ---
>   gcc/cp/method.cc                              | 23 ++++++++++++-------
>   gcc/testsuite/g++.dg/ext/is_convertible3.C    |  9 ++++++++
>   .../g++.dg/ext/is_nothrow_convertible3.C      |  9 ++++++++
>   3 files changed, 33 insertions(+), 8 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C
>   create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C
> 
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index c35a59fe56c..9f917f13134 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -2236,6 +2236,19 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
>     return ref_conv_binds_directly (to, val, direct_init_p).is_false ();
>   }
>   
> +/* Worker for is_{,nothrow_}convertible.  Attempt to perform an implicit
> +   conversion from FROM to TO and return the result.  */
> +
> +static tree
> +is_convertible_helper (tree from, tree to)
> +{
> +  if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
> +    return integer_one_node;
> +  cp_unevaluated u;
> +  tree expr = build_stub_object (from);
> +  return perform_implicit_conversion (to, expr, tf_none);
> +}
> +
>   /* Return true if FROM can be converted to TO using implicit conversions,
>      or both FROM and TO are possibly cv-qualified void.  NB: This doesn't
>      implement the "Access checks are performed as if from a context unrelated
> @@ -2244,10 +2257,7 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
>   bool
>   is_convertible (tree from, tree to)
>   {
> -  if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
> -    return true;
> -  tree expr = build_stub_object (from);
> -  expr = perform_implicit_conversion (to, expr, tf_none);
> +  tree expr = is_convertible_helper (from, to);
>     if (expr == error_mark_node)
>       return false;
>     return !!expr;
> @@ -2258,10 +2268,7 @@ is_convertible (tree from, tree to)
>   bool
>   is_nothrow_convertible (tree from, tree to)
>   {
> -  if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
> -    return true;
> -  tree expr = build_stub_object (from);
> -  expr = perform_implicit_conversion (to, expr, tf_none);
> +  tree expr = is_convertible_helper (from, to);
>     if (expr == NULL_TREE || expr == error_mark_node)
>       return false;
>     return expr_noexcept_p (expr, tf_none);
> diff --git a/gcc/testsuite/g++.dg/ext/is_convertible3.C b/gcc/testsuite/g++.dg/ext/is_convertible3.C
> new file mode 100644
> index 00000000000..7a986d075c2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C
> @@ -0,0 +1,9 @@
> +// PR c++/106784
> +// { dg-do compile { target c++11 } }
> +// Make sure we don't reject this at runtime by trying to instantiate
> +// something that would be ill-formed.
> +
> +struct A;
> +struct B { template<class T> B(const T&) noexcept { T::nonexistent; } };
> +
> +static_assert(__is_convertible(const A&, B), "");
> diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C
> new file mode 100644
> index 00000000000..05b1e1d9ad9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C
> @@ -0,0 +1,9 @@
> +// PR c++/106784
> +// { dg-do compile { target c++11 } }
> +// Make sure we don't reject this at runtime by trying to instantiate
> +// something that would be ill-formed.
> +
> +struct A;
> +struct B { template<class T> B(const T&) noexcept { T::nonexistent; } };
> +
> +static_assert(__is_nothrow_convertible(const A&, B), "");
> 
> base-commit: 099a66498bf7a40764002793eba66c881a251b76


  reply	other threads:[~2022-09-26 16:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 15:22 [PATCH] " Marek Polacek
2022-09-26 15:51 ` Patrick Palka
2022-09-26 16:21   ` Jason Merrill
2022-09-26 16:25   ` [PATCH v2] " Marek Polacek
2022-09-26 16:41     ` Jason Merrill [this message]
2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
2022-09-26 16:26   ` Marek Polacek

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=c5b9e0f4-fa08-a28f-309d-b9b8e21b4f7d@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=polacek@redhat.com \
    --cc=ppalka@redhat.com \
    /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).