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