Skip to content

[libc++] Re-introduce special support for narrowing conversions to bool in variant #73121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libcxx/docs/ReleaseNotes/18.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Deprecations and Removals
- The non-conforming constructor ``std::future_error(std::error_code)`` has been removed. Please use the
``std::future_error(std::future_errc)`` constructor provided in C++17 instead.

- `P1957 <https://wg21.link/P1957>` has been implemented in Clang and libc++ removed a code path that led to
narrowing conversions in ``std::variant`` behaving in a non-standard way. This may change how some uses of
``std::variant``'s constructor behave in user code. The ``_LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT``
macro is provided to restore the previous behavior, and it will be supported in the LLVM 18 release only.
In LLVM 19 and beyond, ``_LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT`` will not be honored anymore.

Upcoming Deprecations and Removals
----------------------------------

Expand Down
19 changes: 19 additions & 0 deletions libcxx/include/variant
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,25 @@ struct __overload {
auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
};

// TODO(LLVM-19): Remove all occurrences of this macro.
#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
template <class _Tp, size_t>
struct __overload_bool {
template <class _Up, class _Ap = __remove_cvref_t<_Up>>
auto operator()(bool, _Up&&) const
-> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
};

template <size_t _Idx>
struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
template <size_t _Idx>
struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
template <size_t _Idx>
struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
template <size_t _Idx>
struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
#endif

template <class ..._Bases>
struct __all_overloads : _Bases... {
void operator()() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ void test_T_assignment_sfinae() {
};
static_assert(!std::is_assignable<V, X>::value,
"no boolean conversion in operator=");
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
static_assert(std::is_assignable<V, std::false_type>::value,
"converted to bool in operator=");
#endif
}
{
struct X {};
Expand Down Expand Up @@ -297,11 +299,13 @@ void test_T_assignment_performs_assignment() {
}

void test_T_assignment_vector_bool() {
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
std::vector<bool> vec = {true};
std::variant<bool, int> v;
v = vec[0];
assert(v.index() == 0);
assert(std::get<0>(v) == true);
#endif
}

int main(int, char**) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ int main(int, char**)
static_assert(!std::is_assignable<std::variant<int, bool>, decltype("meow")>::value, "");
static_assert(!std::is_assignable<std::variant<int, const bool>, decltype("meow")>::value, "");

#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
static_assert(std::is_assignable<std::variant<bool>, std::true_type>::value, "");
#endif
static_assert(!std::is_assignable<std::variant<bool>, std::unique_ptr<char> >::value, "");
static_assert(!std::is_assignable<std::variant<bool>, decltype(nullptr)>::value, "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ void test_T_ctor_sfinae() {
};
static_assert(!std::is_constructible<V, X>::value,
"no boolean conversion in constructor");
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
static_assert(std::is_constructible<V, std::false_type>::value,
"converted to bool in constructor");
#endif
}
{
struct X {};
Expand Down Expand Up @@ -200,10 +202,12 @@ void test_construction_with_repeated_types() {
}

void test_vector_bool() {
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
std::vector<bool> vec = {true};
std::variant<bool, int> v = vec[0];
assert(v.index() == 0);
assert(std::get<0>(v) == true);
#endif
}

int main(int, char**) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ int main(int, char**)
static_assert(!std::is_constructible<std::variant<int, bool>, decltype("meow")>::value, "");
static_assert(!std::is_constructible<std::variant<int, const bool>, decltype("meow")>::value, "");

#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
static_assert(std::is_constructible<std::variant<bool>, std::true_type>::value, "");
#endif
static_assert(!std::is_constructible<std::variant<bool>, std::unique_ptr<char> >::value, "");
static_assert(!std::is_constructible<std::variant<bool>, decltype(nullptr)>::value, "");

Expand Down
3 changes: 2 additions & 1 deletion libcxx/test/support/variant_test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

// FIXME: Currently the variant<T&> tests are disabled using this macro.
#define TEST_VARIANT_HAS_NO_REFERENCES

// TODO(LLVM-19): Remove TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
# define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
#endif

#ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
constexpr bool VariantAllowsNarrowingConversions = true;
#else
Expand Down