From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: wrong std::is_convertible with cv-qual fn [PR109680]
Date: Wed, 3 May 2023 15:37:03 -0400 [thread overview]
Message-ID: <5dad74d3-5d80-fb3d-5774-675f36cad249@redhat.com> (raw)
In-Reply-To: <20230502231015.56181-1-polacek@redhat.com>
On 5/2/23 19:10, Marek Polacek wrote:
> This PR points out that std::is_convertible has given the wrong answer
> in
>
> static_assert (!std::is_convertible_v <int () const, int (*) ()>, "");
>
> since r13-2822 implemented __is_{,nothrow_}convertible.
>
> std::is_convertible uses the imaginary
>
> To test() { return std::declval<From>(); }
>
> to do its job. Here, From is 'int () const'. std::declval is defined as:
>
> template<class T>
> typename std::add_rvalue_reference<T>::type declval() noexcept;
>
> std::add_rvalue_reference is defined as "If T is a function type that
> has no cv- or ref- qualifier or an object type, provides a member typedef
> type which is T&&, otherwise type is T."
>
> In our case, T is cv-qualified, so the result is T, so we end up with
>
> int () const declval() noexcept;
>
> which is invalid. In other words, this is pretty much like:
>
> using T = int () const;
> T fn1(); // bad, fn returning a fn
> T& fn2(); // bad, cannot declare reference to qualified function type
> T* fn3(); // bad, cannot declare pointer to qualified function type
>
> using U = int ();
> U fn4(); // bad, fn returning a fn
> U& fn5(); // OK
> U* fn6(); // OK
>
> I think is_convertible_helper needs to simulate std::declval better.
> I wouldn't be surprised if other type traits needed a similar fix.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/13?
>
> I've tested the new test with G++12 and clang++ as well (with
> std::is_convertible).
>
> PR c++/109680
>
> gcc/cp/ChangeLog:
>
> * method.cc (is_convertible_helper): Correct simulating std::declval.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/ext/is_convertible6.C: New test.
> ---
> gcc/cp/method.cc | 20 ++++++++++++++++++++
> gcc/testsuite/g++.dg/ext/is_convertible6.C | 16 ++++++++++++++++
> 2 files changed, 36 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible6.C
>
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index 00eae56eb5b..38eb7520312 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -2245,6 +2245,26 @@ is_convertible_helper (tree from, tree to)
> {
> if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
> return integer_one_node;
> + /* std::is_{,nothrow_}convertible test whether the imaginary function
> + definition
> +
> + To test() { return std::declval<From>(); }
> +
> + is well-formed. A function can't return a function... */
> + if (FUNC_OR_METHOD_TYPE_P (to)
> + /* ...neither can From be a function with cv-/ref-qualifiers:
> + std::declval is defined as
> +
> + template<class T>
> + typename std::add_rvalue_reference<T>::type declval() noexcept;
> +
> + and std::add_rvalue_reference yields T when T is a function with
> + cv- or ref-qualifiers, making the definition ill-formed.
> + ??? Should we check this in other uses of build_stub_object too? */
Probably we want a build_trait_object that wraps build_stub_object with
these extra checks, or does something that exercises more of the normal
code, maybe by tsubsting into T (U&&) with { to, from }?
> + || (FUNC_OR_METHOD_TYPE_P (from)
> + && (type_memfn_quals (from) != TYPE_UNQUALIFIED
> + || type_memfn_rqual (from) != REF_QUAL_NONE)))
> + return error_mark_node;
> cp_unevaluated u;
> tree expr = build_stub_object (from);
> deferring_access_check_sentinel acs (dk_no_deferred);
> diff --git a/gcc/testsuite/g++.dg/ext/is_convertible6.C b/gcc/testsuite/g++.dg/ext/is_convertible6.C
> new file mode 100644
> index 00000000000..180582663e8
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_convertible6.C
> @@ -0,0 +1,16 @@
> +// PR c++/109680
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(X) static_assert((X),#X)
> +
> +SA(!__is_convertible(int () const, int (*)()));
> +SA(!__is_convertible(int (*)(), int () const));
> +
> +SA( __is_convertible(int (), int (*)()));
> +SA(!__is_convertible(int (*)(), int ()));
> +
> +SA( __is_convertible(int (int), int (*) (int)));
> +SA(!__is_convertible(int (*) (int), int (int)));
> +
> +SA(!__is_convertible(int (int) const, int (*) (int)));
> +SA(!__is_convertible(int (*) (int), int (int) const));
>
> base-commit: 33020780a9699f1146eeed61783cec89fde337a0
next prev parent reply other threads:[~2023-05-03 19:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-02 23:10 Marek Polacek
2023-05-03 19:37 ` Jason Merrill [this message]
2023-05-10 21:28 ` [PATCH v2] " Marek Polacek
2023-05-10 22:08 ` Jason Merrill
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=5dad74d3-5d80-fb3d-5774-675f36cad249@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@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).