forked from OSchip/llvm-project
[libc++] [ranges] Fix bugs in ranges::empty().
It was missing the cast to `bool` in `bool(__t.empty())`. It was wrongly using `std::forward` in some places. Differential Revision: https://reviews.llvm.org/D115312
This commit is contained in:
parent
6842f52a0b
commit
a2a9a5c7d3
|
@ -13,7 +13,6 @@
|
||||||
#include <__iterator/concepts.h>
|
#include <__iterator/concepts.h>
|
||||||
#include <__ranges/access.h>
|
#include <__ranges/access.h>
|
||||||
#include <__ranges/size.h>
|
#include <__ranges/size.h>
|
||||||
#include <__utility/forward.h>
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
||||||
|
@ -29,13 +28,13 @@ namespace ranges {
|
||||||
namespace __empty {
|
namespace __empty {
|
||||||
template <class _Tp>
|
template <class _Tp>
|
||||||
concept __member_empty = requires(_Tp&& __t) {
|
concept __member_empty = requires(_Tp&& __t) {
|
||||||
bool(_VSTD::forward<_Tp>(__t).empty());
|
bool(__t.empty());
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
concept __can_invoke_size =
|
concept __can_invoke_size =
|
||||||
!__member_empty<_Tp> &&
|
!__member_empty<_Tp> &&
|
||||||
requires(_Tp&& __t) { ranges::size(_VSTD::forward<_Tp>(__t)); };
|
requires(_Tp&& __t) { ranges::size(__t); };
|
||||||
|
|
||||||
template <class _Tp>
|
template <class _Tp>
|
||||||
concept __can_compare_begin_end =
|
concept __can_compare_begin_end =
|
||||||
|
@ -50,13 +49,13 @@ namespace __empty {
|
||||||
template <__member_empty _Tp>
|
template <__member_empty _Tp>
|
||||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
|
||||||
noexcept(noexcept(bool(__t.empty()))) {
|
noexcept(noexcept(bool(__t.empty()))) {
|
||||||
return __t.empty();
|
return bool(__t.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <__can_invoke_size _Tp>
|
template <__can_invoke_size _Tp>
|
||||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
|
||||||
noexcept(noexcept(ranges::size(_VSTD::forward<_Tp>(__t)))) {
|
noexcept(noexcept(ranges::size(__t))) {
|
||||||
return ranges::size(_VSTD::forward<_Tp>(__t)) == 0;
|
return ranges::size(__t) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<__can_compare_begin_end _Tp>
|
template<__can_compare_begin_end _Tp>
|
||||||
|
|
|
@ -15,11 +15,11 @@
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <utility>
|
||||||
#include "test_macros.h"
|
#include "test_macros.h"
|
||||||
#include "test_iterators.h"
|
#include "test_iterators.h"
|
||||||
|
|
||||||
using RangeEmptyT = decltype(std::ranges::empty);
|
using RangeEmptyT = decltype(std::ranges::empty);
|
||||||
using RangeSizeT = decltype(std::ranges::size);
|
|
||||||
|
|
||||||
static_assert(!std::is_invocable_v<RangeEmptyT, int[]>);
|
static_assert(!std::is_invocable_v<RangeEmptyT, int[]>);
|
||||||
static_assert(!std::is_invocable_v<RangeEmptyT, int(&)[]>);
|
static_assert(!std::is_invocable_v<RangeEmptyT, int(&)[]>);
|
||||||
|
@ -41,12 +41,16 @@ static_assert(!std::ranges::empty(std::move(array_of_incomplete)));
|
||||||
static_assert(!std::ranges::empty(std::as_const(array_of_incomplete)));
|
static_assert(!std::ranges::empty(std::as_const(array_of_incomplete)));
|
||||||
static_assert(!std::ranges::empty(static_cast<const Incomplete(&&)[42]>(array_of_incomplete)));
|
static_assert(!std::ranges::empty(static_cast<const Incomplete(&&)[42]>(array_of_incomplete)));
|
||||||
|
|
||||||
struct NonConstSizeAndEmpty {
|
struct InputRangeWithoutSize {
|
||||||
int size();
|
cpp17_input_iterator<int*> begin() const;
|
||||||
|
cpp17_input_iterator<int*> end() const;
|
||||||
|
};
|
||||||
|
static_assert(!std::is_invocable_v<RangeEmptyT, const InputRangeWithoutSize&>);
|
||||||
|
|
||||||
|
struct NonConstEmpty {
|
||||||
bool empty();
|
bool empty();
|
||||||
};
|
};
|
||||||
static_assert(!std::is_invocable_v<RangeSizeT, const NonConstSizeAndEmpty&>);
|
static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstEmpty&>);
|
||||||
static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstSizeAndEmpty&>);
|
|
||||||
|
|
||||||
struct HasMemberAndFunction {
|
struct HasMemberAndFunction {
|
||||||
constexpr bool empty() const { return true; }
|
constexpr bool empty() const { return true; }
|
||||||
|
@ -60,7 +64,7 @@ struct BadReturnType {
|
||||||
static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType&>);
|
static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType&>);
|
||||||
|
|
||||||
struct BoolConvertible {
|
struct BoolConvertible {
|
||||||
constexpr /*TODO: explicit*/ operator bool() noexcept(false) { return true; }
|
constexpr explicit operator bool() noexcept(false) { return true; }
|
||||||
};
|
};
|
||||||
struct BoolConvertibleReturnType {
|
struct BoolConvertibleReturnType {
|
||||||
constexpr BoolConvertible empty() noexcept { return {}; }
|
constexpr BoolConvertible empty() noexcept { return {}; }
|
||||||
|
@ -159,9 +163,7 @@ constexpr bool testBeginEqualsEnd() {
|
||||||
assert(std::ranges::empty(e) == false); // e.empty()
|
assert(std::ranges::empty(e) == false); // e.empty()
|
||||||
assert(std::ranges::empty(std::as_const(e)) == true); // e.begin() == e.end()
|
assert(std::ranges::empty(std::as_const(e)) == true); // e.begin() == e.end()
|
||||||
|
|
||||||
#if 0 // TODO FIXME
|
|
||||||
assert(std::ranges::empty(EvilBeginEnd()));
|
assert(std::ranges::empty(EvilBeginEnd()));
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue