From: Jonathan Wakely Date: Wed, 24 Jul 2024 17:08:03 +0000 (+0100) Subject: libstdc++: Implement LWG 3836 for std::expected bool conversions X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ab1a0e683d8a10bcd59aab38e44bbce57c35bb49;p=thirdparty%2Fgcc.git libstdc++: Implement LWG 3836 for std::expected bool conversions libstdc++-v3/ChangeLog: * include/std/expected (expected): Constrain constructors to prevent problematic bool conversions, as per LWG 3836. * testsuite/20_util/expected/lwg3836.cc: New test. --- diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 86026c3947a1..2594cfe131c0 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -314,6 +314,17 @@ namespace __expected __guard.release(); } } + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3836. std::expected conversion constructor + // expected(const expected&) should take precedence over + // expected(U&&) with operator bool + + // If T is cv bool, remove_cvref_t is not a specialization of expected. + template + concept __not_constructing_bool_from_expected + = ! is_same_v, bool> + || ! __is_expected>; } /// @endcond @@ -327,26 +338,41 @@ namespace __expected static_assert( ! __expected::__is_unexpected> ); static_assert( __expected::__can_be_unexpected<_Er> ); - template> + // If T is not cv bool, converts-from-any-cvref> and + // is_constructible, cv expected ref-qual> are false. + template, + typename = remove_cv_t<_Tp>> static constexpr bool __cons_from_expected - = __or_v&>, - is_constructible<_Tp, expected<_Up, _Err>>, - is_constructible<_Tp, const expected<_Up, _Err>&>, - is_constructible<_Tp, const expected<_Up, _Err>>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_constructible<_Unex, expected<_Up, _Err>&>, - is_constructible<_Unex, expected<_Up, _Err>>, - is_constructible<_Unex, const expected<_Up, _Err>&>, - is_constructible<_Unex, const expected<_Up, _Err>> + = __or_v&>, + is_constructible<_Tp, expected<_Up, _Gr>>, + is_constructible<_Tp, const expected<_Up, _Gr>&>, + is_constructible<_Tp, const expected<_Up, _Gr>>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_constructible<_Unex, expected<_Up, _Gr>&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> >; - template + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // If t is cv bool, we know it can be constructed from expected, + // but we don't want to cause the expected(U&&) constructor to be used, + // so we only check the is_constructible, ...> cases. + template + static constexpr bool __cons_from_expected<_Up, _Gr, _Unex, bool> + = __or_v&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> + >; + + template constexpr static bool __explicit_conv = __or_v<__not_>, - __not_> + __not_> >; template @@ -445,8 +471,9 @@ namespace __expected template requires (!is_same_v, expected>) && (!is_same_v, in_place_t>) - && (!__expected::__is_unexpected>) && is_constructible_v<_Tp, _Up> + && (!__expected::__is_unexpected>) + && __expected::__not_constructing_bool_from_expected<_Tp, _Up> constexpr explicit(!is_convertible_v<_Up, _Tp>) expected(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp, _Up>) diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc new file mode 100644 index 000000000000..cd029c449632 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc @@ -0,0 +1,34 @@ +// { dg-do run { target c++23 } } + +#include +#include + +constexpr void +test_convert_contained_value_to_bool() +{ + struct BaseError { }; + struct DerivedError : BaseError { }; + + std::expected e = false; + + // Should use expected(const expected&) ctor, not expected(U&&): + std::expected e2 = e; + + // Contained value should be e.value() not static_cast(e): + VERIFY( e2.value() == false ); + + std::expected e3(std::unexpect); + std::expected e4 = e3; + // Should have error, not static_cast(e3): + VERIFY( ! e4.has_value() ); +} + +int main() +{ + test_convert_contained_value_to_bool(); + + static_assert([] { + test_convert_contained_value_to_bool(); + return true; + }()); +}