* [PATCH] c++: Instantiate less when evaluating __is_convertible
@ 2022-09-26 15:22 Marek Polacek
2022-09-26 15:51 ` Patrick Palka
2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
0 siblings, 2 replies; 7+ messages in thread
From: Marek Polacek @ 2022-09-26 15:22 UTC (permalink / raw)
To: GCC Patches, Jason Merrill; +Cc: Jonathan Wakely
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.
gcc/testsuite/ChangeLog:
* g++.dg/ext/is_convertible3.C: New test.
---
gcc/cp/method.cc | 1 +
gcc/testsuite/g++.dg/ext/is_convertible3.C | 66 ++++++++++++++++++++++
2 files changed, 67 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C
diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index c35a59fe56c..45f70f5d3f3 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -2246,6 +2246,7 @@ is_convertible (tree from, tree to)
{
if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
return true;
+ cp_unevaluated u;
tree expr = build_stub_object (from);
expr = perform_implicit_conversion (to, expr, tf_none);
if (expr == error_mark_node)
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..c817dc6f146
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C
@@ -0,0 +1,66 @@
+// PR c++/106784
+// { dg-do compile { target c++11 } }
+// Make sure we don't reject this at runtime by trying to instantiate
+// char_traits<CharT>::eq(CharT, CharT) while evaluating __is_convertible.
+
+template<bool B>
+struct bool_constant { static constexpr bool value = B; };
+using true_type = bool_constant<true>;
+using false_type = bool_constant<false>;
+
+template<typename T> struct is_void : false_type { };
+template<> struct is_void<void> : true_type { };
+
+template<typename T> T&& declval();
+
+template<bool> struct enable_if { };
+template<> struct enable_if<true> { using type = void; };
+template<bool B> using enable_if_t = typename enable_if<B>::type;
+
+template<typename _From, typename _To>
+ struct is_convertible
+ : public bool_constant<__is_convertible(_From, _To)>
+ { };
+
+template<class CharT>
+struct char_traits
+{
+ static unsigned long length(const char* s) { eq(*s, *s); return 0; }
+
+ static void eq(CharT l, CharT r) noexcept { l.f(r); }
+};
+
+template<class CharT>
+struct basic_string_view
+{
+ using traits_type = char_traits<CharT>;
+
+ constexpr basic_string_view() = default;
+ constexpr basic_string_view(const basic_string_view&) = default;
+
+ constexpr
+ basic_string_view(const CharT* __str) noexcept
+ : _M_len{traits_type::length(__str)}
+ { }
+
+ unsigned long _M_len = 0;
+};
+
+template<class CharT>
+struct basic_string
+{
+ template<class T>
+ enable_if_t<is_convertible<const T&, basic_string_view<CharT>>::value
+ && !is_convertible<const T&, const char*>::value>
+ replace(int, T) { }
+
+ void replace(unsigned long, const char*) { }
+
+ void replace(const char* s) { replace(1, s); }
+};
+
+int main()
+{
+ basic_string<char> s;
+ s.replace("");
+}
base-commit: 2460f7cdef7ef9c971de79271afc0db73687a272
--
2.37.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] c++: Instantiate less when evaluating __is_convertible
2022-09-26 15:22 [PATCH] c++: Instantiate less when evaluating __is_convertible 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:02 ` [PATCH] " Jonathan Wakely
1 sibling, 2 replies; 7+ messages in thread
From: Patrick Palka @ 2022-09-26 15:51 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jason Merrill, Jonathan Wakely
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).
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));
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/ext/is_convertible3.C: New test.
> ---
> gcc/cp/method.cc | 1 +
> gcc/testsuite/g++.dg/ext/is_convertible3.C | 66 ++++++++++++++++++++++
> 2 files changed, 67 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C
>
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index c35a59fe56c..45f70f5d3f3 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -2246,6 +2246,7 @@ is_convertible (tree from, tree to)
> {
> if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
> return true;
> + cp_unevaluated u;
> tree expr = build_stub_object (from);
> expr = perform_implicit_conversion (to, expr, tf_none);
> if (expr == error_mark_node)
> 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..c817dc6f146
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C
> @@ -0,0 +1,66 @@
> +// PR c++/106784
> +// { dg-do compile { target c++11 } }
> +// Make sure we don't reject this at runtime by trying to instantiate
> +// char_traits<CharT>::eq(CharT, CharT) while evaluating __is_convertible.
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +using true_type = bool_constant<true>;
> +using false_type = bool_constant<false>;
> +
> +template<typename T> struct is_void : false_type { };
> +template<> struct is_void<void> : true_type { };
> +
> +template<typename T> T&& declval();
> +
> +template<bool> struct enable_if { };
> +template<> struct enable_if<true> { using type = void; };
> +template<bool B> using enable_if_t = typename enable_if<B>::type;
> +
> +template<typename _From, typename _To>
> + struct is_convertible
> + : public bool_constant<__is_convertible(_From, _To)>
> + { };
> +
> +template<class CharT>
> +struct char_traits
> +{
> + static unsigned long length(const char* s) { eq(*s, *s); return 0; }
> +
> + static void eq(CharT l, CharT r) noexcept { l.f(r); }
> +};
> +
> +template<class CharT>
> +struct basic_string_view
> +{
> + using traits_type = char_traits<CharT>;
> +
> + constexpr basic_string_view() = default;
> + constexpr basic_string_view(const basic_string_view&) = default;
> +
> + constexpr
> + basic_string_view(const CharT* __str) noexcept
> + : _M_len{traits_type::length(__str)}
> + { }
> +
> + unsigned long _M_len = 0;
> +};
> +
> +template<class CharT>
> +struct basic_string
> +{
> + template<class T>
> + enable_if_t<is_convertible<const T&, basic_string_view<CharT>>::value
> + && !is_convertible<const T&, const char*>::value>
> + replace(int, T) { }
> +
> + void replace(unsigned long, const char*) { }
> +
> + void replace(const char* s) { replace(1, s); }
> +};
> +
> +int main()
> +{
> + basic_string<char> s;
> + s.replace("");
> +}
>
> base-commit: 2460f7cdef7ef9c971de79271afc0db73687a272
> --
> 2.37.3
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] c++: Instantiate less when evaluating __is_convertible
2022-09-26 15:22 [PATCH] c++: Instantiate less when evaluating __is_convertible Marek Polacek
2022-09-26 15:51 ` Patrick Palka
@ 2022-09-26 16:02 ` Jonathan Wakely
2022-09-26 16:26 ` Marek Polacek
1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2022-09-26 16:02 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jason Merrill
On Mon, 26 Sept 2022 at 16:23, Marek Polacek 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)
N.B. in the original code wasn't trying to do something dumb like call
a member function on a char, but it was using basic_string_view<X>
where X is a class type without an operator== and so
char_traits<X>::eq was invalid. I changed it to just use
basic_string_view<char> and changed char_traits::eq to do something
different, that was invalid for char.
I can provide a less silly test case if you like, but I don't think it
matters for the purposes of the testsuite.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] c++: Instantiate less when evaluating __is_convertible
2022-09-26 15:51 ` Patrick Palka
@ 2022-09-26 16:21 ` Jason Merrill
2022-09-26 16:25 ` [PATCH v2] " Marek Polacek
1 sibling, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2022-09-26 16:21 UTC (permalink / raw)
To: Patrick Palka, Marek Polacek; +Cc: GCC Patches, Jonathan Wakely
On 9/26/22 11:51, 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).
Agreed.
> 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));
>
>>
>> gcc/testsuite/ChangeLog:
>>
>> * g++.dg/ext/is_convertible3.C: New test.
>> ---
>> gcc/cp/method.cc | 1 +
>> gcc/testsuite/g++.dg/ext/is_convertible3.C | 66 ++++++++++++++++++++++
>> 2 files changed, 67 insertions(+)
>> create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C
>>
>> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
>> index c35a59fe56c..45f70f5d3f3 100644
>> --- a/gcc/cp/method.cc
>> +++ b/gcc/cp/method.cc
>> @@ -2246,6 +2246,7 @@ is_convertible (tree from, tree to)
>> {
>> if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
>> return true;
>> + cp_unevaluated u;
>> tree expr = build_stub_object (from);
>> expr = perform_implicit_conversion (to, expr, tf_none);
>> if (expr == error_mark_node)
>> 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..c817dc6f146
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C
>> @@ -0,0 +1,66 @@
>> +// PR c++/106784
>> +// { dg-do compile { target c++11 } }
>> +// Make sure we don't reject this at runtime by trying to instantiate
>> +// char_traits<CharT>::eq(CharT, CharT) while evaluating __is_convertible.
>> +
>> +template<bool B>
>> +struct bool_constant { static constexpr bool value = B; };
>> +using true_type = bool_constant<true>;
>> +using false_type = bool_constant<false>;
>> +
>> +template<typename T> struct is_void : false_type { };
>> +template<> struct is_void<void> : true_type { };
>> +
>> +template<typename T> T&& declval();
>> +
>> +template<bool> struct enable_if { };
>> +template<> struct enable_if<true> { using type = void; };
>> +template<bool B> using enable_if_t = typename enable_if<B>::type;
>> +
>> +template<typename _From, typename _To>
>> + struct is_convertible
>> + : public bool_constant<__is_convertible(_From, _To)>
>> + { };
>> +
>> +template<class CharT>
>> +struct char_traits
>> +{
>> + static unsigned long length(const char* s) { eq(*s, *s); return 0; }
>> +
>> + static void eq(CharT l, CharT r) noexcept { l.f(r); }
>> +};
>> +
>> +template<class CharT>
>> +struct basic_string_view
>> +{
>> + using traits_type = char_traits<CharT>;
>> +
>> + constexpr basic_string_view() = default;
>> + constexpr basic_string_view(const basic_string_view&) = default;
>> +
>> + constexpr
>> + basic_string_view(const CharT* __str) noexcept
>> + : _M_len{traits_type::length(__str)}
>> + { }
>> +
>> + unsigned long _M_len = 0;
>> +};
>> +
>> +template<class CharT>
>> +struct basic_string
>> +{
>> + template<class T>
>> + enable_if_t<is_convertible<const T&, basic_string_view<CharT>>::value
>> + && !is_convertible<const T&, const char*>::value>
>> + replace(int, T) { }
>> +
>> + void replace(unsigned long, const char*) { }
>> +
>> + void replace(const char* s) { replace(1, s); }
>> +};
>> +
>> +int main()
>> +{
>> + basic_string<char> s;
>> + s.replace("");
>> +}
>>
>> base-commit: 2460f7cdef7ef9c971de79271afc0db73687a272
>> --
>> 2.37.3
>>
>>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2] c++: Instantiate less when evaluating __is_convertible
2022-09-26 15:51 ` Patrick Palka
2022-09-26 16:21 ` Jason Merrill
@ 2022-09-26 16:25 ` Marek Polacek
2022-09-26 16:41 ` Jason Merrill
1 sibling, 1 reply; 7+ messages in thread
From: Marek Polacek @ 2022-09-26 16:25 UTC (permalink / raw)
To: Patrick Palka; +Cc: GCC Patches, Jason Merrill, Jonathan Wakely
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?
-- >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
--
2.37.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] c++: Instantiate less when evaluating __is_convertible
2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
@ 2022-09-26 16:26 ` Marek Polacek
0 siblings, 0 replies; 7+ messages in thread
From: Marek Polacek @ 2022-09-26 16:26 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: GCC Patches, Jason Merrill
On Mon, Sep 26, 2022 at 05:02:36PM +0100, Jonathan Wakely wrote:
> On Mon, 26 Sept 2022 at 16:23, Marek Polacek 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)
>
> N.B. in the original code wasn't trying to do something dumb like call
> a member function on a char, but it was using basic_string_view<X>
> where X is a class type without an operator== and so
> char_traits<X>::eq was invalid. I changed it to just use
> basic_string_view<char> and changed char_traits::eq to do something
> different, that was invalid for char.
Ack.
> I can provide a less silly test case if you like, but I don't think it
> matters for the purposes of the testsuite.
I think no need to, I'm going to use Patrick's short test.
Thanks,
Marek
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] c++: Instantiate less when evaluating __is_convertible
2022-09-26 16:25 ` [PATCH v2] " Marek Polacek
@ 2022-09-26 16:41 ` Jason Merrill
0 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2022-09-26 16:41 UTC (permalink / raw)
To: Marek Polacek, Patrick Palka; +Cc: GCC Patches, Jonathan Wakely
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
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-09-26 16:41 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26 15:22 [PATCH] c++: Instantiate less when evaluating __is_convertible 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
2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
2022-09-26 16:26 ` Marek Polacek
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).