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>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
	libstdc++ <libstdc++@gcc.gnu.org>,
	Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [PATCH] c++: Implement __is_{nothrow_,}convertible [PR106784]
Date: Fri, 23 Sep 2022 11:54:53 -0400	[thread overview]
Message-ID: <bcddca3e-b61d-730d-ac07-a33f6ba7fd38@redhat.com> (raw)
In-Reply-To: <Yy3D5NGL2kCCzmTx@redhat.com>

On 9/23/22 10:34, Marek Polacek wrote:
> On Thu, Sep 22, 2022 at 06:14:44PM -0400, Jason Merrill wrote:
>> On 9/22/22 09:39, Marek Polacek wrote:
>>> To improve compile times, the C++ library could use compiler built-ins
>>> rather than implementing std::is_convertible (and _nothrow) as class
>>> templates.  This patch adds the built-ins.  We already have
>>> __is_constructible and __is_assignable, and the nothrow forms of those.
>>>
>>> Microsoft (and clang, for compatibility) also provide an alias called
>>> __is_convertible_to.  I did not add it, but it would be trivial to do
>>> so.
>>>
>>> I noticed that our __is_assignable doesn't implement the "Access checks
>>> are performed as if from a context unrelated to either type" requirement,
>>> therefore std::is_assignable / __is_assignable give two different results
>>> here:
>>>
>>>     class S {
>>>       operator int();
>>>       friend void g(); // #1
>>>     };
>>>
>>>     void
>>>     g ()
>>>     {
>>>       // #1 doesn't matter
>>>       static_assert(std::is_assignable<int&, S>::value, "");
>>>       static_assert(__is_assignable(int&, S), "");
>>>     }
>>>
>>> This is not a problem if __is_assignable is not meant to be used by
>>> the users.
>>
>> That's fine, it's not.
>   
> Okay then.  libstdc++ needs to make sure then that it's handled right.
> 
>>> This patch doesn't make libstdc++ use the new built-ins, but I had to
>>> rename a class otherwise its name would clash with the new built-in.
>>
>> Sigh, that's going to be a hassle when comparing compiler versions on
>> preprocessed code.
> 
> Yeah, I guess :/.  Kind of like __integer_pack / __make_integer_seq.
> 
>>> --- a/gcc/cp/constraint.cc
>>> +++ b/gcc/cp/constraint.cc
>>> @@ -3697,6 +3697,12 @@ diagnose_trait_expr (tree expr, tree args)
>>>        case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
>>>          inform (loc, "  %qT does not have unique object representations", t1);
>>>          break;
>>> +    case CPTK_IS_CONVERTIBLE:
>>> +      inform (loc, "  %qT is not convertible from %qE", t2, t1);
>>> +      break;
>>> +    case CPTK_IS_NOTHROW_CONVERTIBLE:
>>> +	inform (loc, "  %qT is not %<nothrow%> convertible from %qE", t2, t1);
>>
>> It's odd that the existing diagnostics quote "nothrow", which is not a
>> keyword.  I wonder why these library traits didn't use "noexcept"?
> 
> Eh, yeah, only "throw" is.  The quotes were deliberately added in
> <https://gcc.gnu.org/pipermail/gcc-patches/2019-May/522333.html>.  Should
> I prepare a separate patch to use "%<noexcept%>" rather than "%<nothrow%>"?
> OTOH, the traits have "nothrow" in their names, so maybe just go back to
> "nothrow"?

The latter, I think.  Or possibly "no-throw".  I guess -Wformat-diag 
wants "nothrow" quoted because of the attribute of that name.

>>> --- a/gcc/cp/method.cc
>>> +++ b/gcc/cp/method.cc
>>> @@ -2236,6 +2236,37 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
>>>      return ref_conv_binds_directly (to, val, direct_init_p).is_false ();
>>>    }
>>> +/* 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
>>> +   to either type" restriction.  */
>>> +
>>> +bool
>>> +is_convertible (tree from, tree to)
>>
>> You didn't want to add conversion to is*_xible?
> 
> No, it didn't look like a good fit.  It does things we don't need, and
> also has if VOID_TYPE_P -> return error_mark_node; which would be wrong
> for __is_convertible.
> 
> I realized I'm not testing passing an incomplete type to the built-in,
> but since that is UB, I reckon we don't need to test it (we issue
> "error: invalid use of incomplete type").

But your patch does test that, in the existing call to check_trait_type 
from finish_trait_expr?

The patch is OK.

Jason


  parent reply	other threads:[~2022-09-23 15:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-22 13:39 Marek Polacek
2022-09-22 22:14 ` Jason Merrill
2022-09-23 14:34   ` Marek Polacek
2022-09-23 14:43     ` Jonathan Wakely
2022-09-23 16:34       ` Jonathan Wakely
2022-09-23 16:37         ` Marek Polacek
2022-09-23 15:54     ` Jason Merrill [this message]
2022-09-23 16:16       ` Marek Polacek
2022-09-23 14:40   ` Jonathan Wakely
2022-09-23 15:04     ` 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=bcddca3e-b61d-730d-ac07-a33f6ba7fd38@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=polacek@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).