public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access  [PR107049]
@ 2022-09-27 10:35 Jonathan Wakely
  2022-09-27 12:18 ` Marek Polacek
  2022-09-27 12:53 ` Jason Merrill
  0 siblings, 2 replies; 3+ messages in thread
From: Jonathan Wakely @ 2022-09-27 10:35 UTC (permalink / raw)
  To: gcc-patches, libstdc++; +Cc: Marek Polacek, Jason Merrill

Tested powerpc64le-linux. OK for trunk?

-- >8 --

The is_convertible built-ins should return false if the conversion fails
an access check, not report an error.

	PR c++/107049

gcc/cp/ChangeLog:

	* method.cc (is_convertible_helper): Use access check sentinel.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/is_convertible4.C: New test.
	* g++.dg/ext/is_nothrow_convertible4.C: New test.

libstdc++-v3/ChangeLog:

	* testsuite/20_util/is_convertible/requirements/access.cc: New
	test.
---
 gcc/cp/method.cc                              |  1 +
 gcc/testsuite/g++.dg/ext/is_convertible4.C    | 33 +++++++++++++++++++
 .../g++.dg/ext/is_nothrow_convertible4.C      | 33 +++++++++++++++++++
 .../is_convertible/requirements/access.cc     | 18 ++++++++++
 4 files changed, 85 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible4.C
 create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
 create mode 100644 libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc

diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index 9f917f13134..55af5c43c18 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -2246,6 +2246,7 @@ is_convertible_helper (tree from, tree to)
     return integer_one_node;
   cp_unevaluated u;
   tree expr = build_stub_object (from);
+  deferring_access_check_sentinel acs (dk_no_deferred);
   return perform_implicit_conversion (to, expr, tf_none);
 }
 
diff --git a/gcc/testsuite/g++.dg/ext/is_convertible4.C b/gcc/testsuite/g++.dg/ext/is_convertible4.C
new file mode 100644
index 00000000000..8a7724c5852
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_convertible4.C
@@ -0,0 +1,33 @@
+// PR c++/107049
+// { dg-do compile { target c++11 } }
+// Failed access check should be a substitution failure, not an error.
+
+template<bool B>
+struct bool_constant { static constexpr bool value = B; };
+
+template<typename From, typename To>
+struct is_convertible
+: public bool_constant<__is_convertible(From, To)>
+{ };
+
+#if __cpp_variable_templates
+template<typename From, typename To>
+constexpr bool is_convertible_v = __is_convertible(From, To);
+#endif
+
+class Private
+{
+  operator int() const
+  {
+    static_assert( not is_convertible<Private, int>::value, "" );
+#if __cpp_variable_templates
+    static_assert( not is_convertible_v<Private, int>, "" );
+#endif
+    return 0;
+  }
+};
+
+static_assert( not is_convertible<Private, int>::value, "" );
+#if __cpp_variable_templates
+static_assert( not is_convertible_v<Private, int>, "" );
+#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
new file mode 100644
index 00000000000..f81b5944ca2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
@@ -0,0 +1,33 @@
+// PR c++/107049
+// { dg-do compile { target c++11 } }
+// Failed access check should be a substitution failure, not an error.
+
+template<bool B>
+struct bool_constant { static constexpr bool value = B; };
+
+template<typename From, typename To>
+struct is_nt_convertible
+: public bool_constant<__is_nothrow_convertible(From, To)>
+{ };
+
+#if __cpp_variable_templates
+template<typename From, typename To>
+constexpr bool is_nt_convertible_v = __is_nothrow_convertible(From, To);
+#endif
+
+class Private
+{
+  operator int() const
+  {
+    static_assert( not is_nt_convertible<Private, int>::value, "" );
+#if __cpp_variable_templates
+    static_assert( not is_nt_convertible_v<Private, int>, "" );
+#endif
+    return 0;
+  }
+};
+
+static_assert( not is_nt_convertible<Private, int>::value, "" );
+#if __cpp_variable_templates
+static_assert( not is_nt_convertible_v<Private, int>, "" );
+#endif
diff --git a/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
new file mode 100644
index 00000000000..04a8c525961
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
@@ -0,0 +1,18 @@
+// { dg-do compile { target  c++11 } }
+// PR c++/107049
+
+#include <type_traits>
+
+class Private
+{
+  operator int() const
+  {
+    static_assert( not std::is_convertible<Private, int>::value, "" );
+#if __cpp_lib_type_trait_variable_templates
+    static_assert( not std::is_convertible_v<Private, int>, "" );
+#endif
+    return 0;
+  }
+};
+
+static_assert( not std::is_convertible<Private, int>::value, "" );
-- 
2.37.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049]
  2022-09-27 10:35 [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049] Jonathan Wakely
@ 2022-09-27 12:18 ` Marek Polacek
  2022-09-27 12:53 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2022-09-27 12:18 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-patches, libstdc++, Jason Merrill

On Tue, Sep 27, 2022 at 11:35:10AM +0100, Jonathan Wakely wrote:
> Tested powerpc64le-linux. OK for trunk?
> 
> -- >8 --
> 
> The is_convertible built-ins should return false if the conversion fails
> an access check, not report an error.

Ah, so we do need that sentinel after all.

Patch looks good, thanks.
 
> 	PR c++/107049
> 
> gcc/cp/ChangeLog:
> 
> 	* method.cc (is_convertible_helper): Use access check sentinel.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/is_convertible4.C: New test.
> 	* g++.dg/ext/is_nothrow_convertible4.C: New test.
> 
> libstdc++-v3/ChangeLog:
> 
> 	* testsuite/20_util/is_convertible/requirements/access.cc: New
> 	test.
> ---
>  gcc/cp/method.cc                              |  1 +
>  gcc/testsuite/g++.dg/ext/is_convertible4.C    | 33 +++++++++++++++++++
>  .../g++.dg/ext/is_nothrow_convertible4.C      | 33 +++++++++++++++++++
>  .../is_convertible/requirements/access.cc     | 18 ++++++++++
>  4 files changed, 85 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible4.C
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
>  create mode 100644 libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> 
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index 9f917f13134..55af5c43c18 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -2246,6 +2246,7 @@ is_convertible_helper (tree from, tree to)
>      return integer_one_node;
>    cp_unevaluated u;
>    tree expr = build_stub_object (from);
> +  deferring_access_check_sentinel acs (dk_no_deferred);
>    return perform_implicit_conversion (to, expr, tf_none);
>  }
>  
> diff --git a/gcc/testsuite/g++.dg/ext/is_convertible4.C b/gcc/testsuite/g++.dg/ext/is_convertible4.C
> new file mode 100644
> index 00000000000..8a7724c5852
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_convertible4.C
> @@ -0,0 +1,33 @@
> +// PR c++/107049
> +// { dg-do compile { target c++11 } }
> +// Failed access check should be a substitution failure, not an error.
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +
> +template<typename From, typename To>
> +struct is_convertible
> +: public bool_constant<__is_convertible(From, To)>
> +{ };
> +
> +#if __cpp_variable_templates
> +template<typename From, typename To>
> +constexpr bool is_convertible_v = __is_convertible(From, To);
> +#endif
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not is_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +    static_assert( not is_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not is_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +static_assert( not is_convertible_v<Private, int>, "" );
> +#endif
> diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
> new file mode 100644
> index 00000000000..f81b5944ca2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
> @@ -0,0 +1,33 @@
> +// PR c++/107049
> +// { dg-do compile { target c++11 } }
> +// Failed access check should be a substitution failure, not an error.
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +
> +template<typename From, typename To>
> +struct is_nt_convertible
> +: public bool_constant<__is_nothrow_convertible(From, To)>
> +{ };
> +
> +#if __cpp_variable_templates
> +template<typename From, typename To>
> +constexpr bool is_nt_convertible_v = __is_nothrow_convertible(From, To);
> +#endif
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not is_nt_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +    static_assert( not is_nt_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not is_nt_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +static_assert( not is_nt_convertible_v<Private, int>, "" );
> +#endif
> diff --git a/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> new file mode 100644
> index 00000000000..04a8c525961
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> @@ -0,0 +1,18 @@
> +// { dg-do compile { target  c++11 } }
> +// PR c++/107049
> +
> +#include <type_traits>
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not std::is_convertible<Private, int>::value, "" );
> +#if __cpp_lib_type_trait_variable_templates
> +    static_assert( not std::is_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not std::is_convertible<Private, int>::value, "" );
> -- 
> 2.37.3
> 

Marek


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049]
  2022-09-27 10:35 [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049] Jonathan Wakely
  2022-09-27 12:18 ` Marek Polacek
@ 2022-09-27 12:53 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2022-09-27 12:53 UTC (permalink / raw)
  To: Jonathan Wakely, gcc-patches, libstdc++; +Cc: Marek Polacek

On 9/27/22 06:35, Jonathan Wakely wrote:
> Tested powerpc64le-linux. OK for trunk?

OK, thanks.

> -- >8 --
> 
> The is_convertible built-ins should return false if the conversion fails
> an access check, not report an error.
> 
> 	PR c++/107049
> 
> gcc/cp/ChangeLog:
> 
> 	* method.cc (is_convertible_helper): Use access check sentinel.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/is_convertible4.C: New test.
> 	* g++.dg/ext/is_nothrow_convertible4.C: New test.
> 
> libstdc++-v3/ChangeLog:
> 
> 	* testsuite/20_util/is_convertible/requirements/access.cc: New
> 	test.
> ---
>   gcc/cp/method.cc                              |  1 +
>   gcc/testsuite/g++.dg/ext/is_convertible4.C    | 33 +++++++++++++++++++
>   .../g++.dg/ext/is_nothrow_convertible4.C      | 33 +++++++++++++++++++
>   .../is_convertible/requirements/access.cc     | 18 ++++++++++
>   4 files changed, 85 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible4.C
>   create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
>   create mode 100644 libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> 
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index 9f917f13134..55af5c43c18 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -2246,6 +2246,7 @@ is_convertible_helper (tree from, tree to)
>       return integer_one_node;
>     cp_unevaluated u;
>     tree expr = build_stub_object (from);
> +  deferring_access_check_sentinel acs (dk_no_deferred);
>     return perform_implicit_conversion (to, expr, tf_none);
>   }
>   
> diff --git a/gcc/testsuite/g++.dg/ext/is_convertible4.C b/gcc/testsuite/g++.dg/ext/is_convertible4.C
> new file mode 100644
> index 00000000000..8a7724c5852
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_convertible4.C
> @@ -0,0 +1,33 @@
> +// PR c++/107049
> +// { dg-do compile { target c++11 } }
> +// Failed access check should be a substitution failure, not an error.
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +
> +template<typename From, typename To>
> +struct is_convertible
> +: public bool_constant<__is_convertible(From, To)>
> +{ };
> +
> +#if __cpp_variable_templates
> +template<typename From, typename To>
> +constexpr bool is_convertible_v = __is_convertible(From, To);
> +#endif
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not is_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +    static_assert( not is_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not is_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +static_assert( not is_convertible_v<Private, int>, "" );
> +#endif
> diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
> new file mode 100644
> index 00000000000..f81b5944ca2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C
> @@ -0,0 +1,33 @@
> +// PR c++/107049
> +// { dg-do compile { target c++11 } }
> +// Failed access check should be a substitution failure, not an error.
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +
> +template<typename From, typename To>
> +struct is_nt_convertible
> +: public bool_constant<__is_nothrow_convertible(From, To)>
> +{ };
> +
> +#if __cpp_variable_templates
> +template<typename From, typename To>
> +constexpr bool is_nt_convertible_v = __is_nothrow_convertible(From, To);
> +#endif
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not is_nt_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +    static_assert( not is_nt_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not is_nt_convertible<Private, int>::value, "" );
> +#if __cpp_variable_templates
> +static_assert( not is_nt_convertible_v<Private, int>, "" );
> +#endif
> diff --git a/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> new file mode 100644
> index 00000000000..04a8c525961
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc
> @@ -0,0 +1,18 @@
> +// { dg-do compile { target  c++11 } }
> +// PR c++/107049
> +
> +#include <type_traits>
> +
> +class Private
> +{
> +  operator int() const
> +  {
> +    static_assert( not std::is_convertible<Private, int>::value, "" );
> +#if __cpp_lib_type_trait_variable_templates
> +    static_assert( not std::is_convertible_v<Private, int>, "" );
> +#endif
> +    return 0;
> +  }
> +};
> +
> +static_assert( not std::is_convertible<Private, int>::value, "" );


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-27 12:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-27 10:35 [PATCH] c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049] Jonathan Wakely
2022-09-27 12:18 ` Marek Polacek
2022-09-27 12:53 ` Jason Merrill

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).