c++: Make __is_{,nothrow_}convertible SFINAE on access [PR107049]

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.
This commit is contained in:
Jonathan Wakely 2022-09-27 09:51:10 +01:00
parent 614e5696d7
commit 3f7eea4411
4 changed files with 85 additions and 0 deletions

View file

@ -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);
}

View file

@ -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

View file

@ -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

View file

@ -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, "" );