[libcxx-commits] [libcxx] a2a9a5c - [libc++] [ranges] Fix bugs in ranges::empty().
Arthur O'Dwyer via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 23 11:56:36 PST 2021
Author: Arthur O'Dwyer
Date: 2021-12-23T14:56:23-05:00
New Revision: a2a9a5c7d3d9e6860444d8b4fd9f1fd665801586
URL: https://github.com/llvm/llvm-project/commit/a2a9a5c7d3d9e6860444d8b4fd9f1fd665801586
DIFF: https://github.com/llvm/llvm-project/commit/a2a9a5c7d3d9e6860444d8b4fd9f1fd665801586.diff
LOG: [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
Added:
Modified:
libcxx/include/__ranges/empty.h
libcxx/test/std/ranges/range.access/empty.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__ranges/empty.h b/libcxx/include/__ranges/empty.h
index bf6772c5673b7..4c6a3a0d24ff0 100644
--- a/libcxx/include/__ranges/empty.h
+++ b/libcxx/include/__ranges/empty.h
@@ -13,7 +13,6 @@
#include <__iterator/concepts.h>
#include <__ranges/access.h>
#include <__ranges/size.h>
-#include <__utility/forward.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -29,13 +28,13 @@ namespace ranges {
namespace __empty {
template <class _Tp>
concept __member_empty = requires(_Tp&& __t) {
- bool(_VSTD::forward<_Tp>(__t).empty());
+ bool(__t.empty());
};
template<class _Tp>
concept __can_invoke_size =
!__member_empty<_Tp> &&
- requires(_Tp&& __t) { ranges::size(_VSTD::forward<_Tp>(__t)); };
+ requires(_Tp&& __t) { ranges::size(__t); };
template <class _Tp>
concept __can_compare_begin_end =
@@ -50,13 +49,13 @@ namespace __empty {
template <__member_empty _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
noexcept(noexcept(bool(__t.empty()))) {
- return __t.empty();
+ return bool(__t.empty());
}
template <__can_invoke_size _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
- noexcept(noexcept(ranges::size(_VSTD::forward<_Tp>(__t)))) {
- return ranges::size(_VSTD::forward<_Tp>(__t)) == 0;
+ noexcept(noexcept(ranges::size(__t))) {
+ return ranges::size(__t) == 0;
}
template<__can_compare_begin_end _Tp>
diff --git a/libcxx/test/std/ranges/range.access/empty.pass.cpp b/libcxx/test/std/ranges/range.access/empty.pass.cpp
index 9be523eb42b7b..5fb9965df7553 100644
--- a/libcxx/test/std/ranges/range.access/empty.pass.cpp
+++ b/libcxx/test/std/ranges/range.access/empty.pass.cpp
@@ -15,11 +15,11 @@
#include <ranges>
#include <cassert>
+#include <utility>
#include "test_macros.h"
#include "test_iterators.h"
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(&)[]>);
@@ -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(static_cast<const Incomplete(&&)[42]>(array_of_incomplete)));
-struct NonConstSizeAndEmpty {
- int size();
+struct InputRangeWithoutSize {
+ 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();
};
-static_assert(!std::is_invocable_v<RangeSizeT, const NonConstSizeAndEmpty&>);
-static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstSizeAndEmpty&>);
+static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstEmpty&>);
struct HasMemberAndFunction {
constexpr bool empty() const { return true; }
@@ -60,7 +64,7 @@ struct BadReturnType {
static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType&>);
struct BoolConvertible {
- constexpr /*TODO: explicit*/ operator bool() noexcept(false) { return true; }
+ constexpr explicit operator bool() noexcept(false) { return true; }
};
struct BoolConvertibleReturnType {
constexpr BoolConvertible empty() noexcept { return {}; }
@@ -159,9 +163,7 @@ constexpr bool testBeginEqualsEnd() {
assert(std::ranges::empty(e) == false); // e.empty()
assert(std::ranges::empty(std::as_const(e)) == true); // e.begin() == e.end()
-#if 0 // TODO FIXME
assert(std::ranges::empty(EvilBeginEnd()));
-#endif
return true;
}
More information about the libcxx-commits
mailing list