[libcxx-commits] [libcxx] 9021f36 - [libc++] [ranges] ref_view and empty_view are borrowed ranges. Normalize borrowed_range tests.
Arthur O'Dwyer via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 27 11:22:10 PST 2022
Author: Arthur O'Dwyer
Date: 2022-01-27T14:21:51-05:00
New Revision: 9021f3682c973f04b0595c6b6b567a58887d34f5
URL: https://github.com/llvm/llvm-project/commit/9021f3682c973f04b0595c6b6b567a58887d34f5
DIFF: https://github.com/llvm/llvm-project/commit/9021f3682c973f04b0595c6b6b567a58887d34f5.diff
LOG: [libc++] [ranges] ref_view and empty_view are borrowed ranges. Normalize borrowed_range tests.
Differential Revision: https://reviews.llvm.org/D118164
Added:
libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/range.ref.view.pass.cpp
libcxx/test/std/ranges/range.adaptors/range.empty/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.factories/range.single.view/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.utility/range.subrange/borrowing.compile.pass.cpp
Modified:
libcxx/include/__ranges/empty_view.h
libcxx/include/__ranges/ref_view.h
libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.adaptors/range.common.view/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.adaptors/range.reverse/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp
libcxx/test/std/ranges/range.factories/range.iota.view/borrowing.compile.pass.cpp
libcxx/test/support/test_range.h
Removed:
libcxx/test/std/ranges/range.adaptors/range.ref.view.pass.cpp
libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp
################################################################################
diff --git a/libcxx/include/__ranges/empty_view.h b/libcxx/include/__ranges/empty_view.h
index 4a98a6f324e70..f744dcbe92f44 100644
--- a/libcxx/include/__ranges/empty_view.h
+++ b/libcxx/include/__ranges/empty_view.h
@@ -10,6 +10,7 @@
#define _LIBCPP___RANGES_EMPTY_VIEW_H
#include <__config>
+#include <__ranges/enable_borrowed_range.h>
#include <__ranges/view_interface.h>
#include <type_traits>
@@ -32,6 +33,9 @@ namespace ranges {
_LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 0; }
_LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return true; }
};
+
+ template<class _Tp>
+ inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
} // namespace ranges
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
diff --git a/libcxx/include/__ranges/ref_view.h b/libcxx/include/__ranges/ref_view.h
index 7567ac48f2550..283fa2599bff8 100644
--- a/libcxx/include/__ranges/ref_view.h
+++ b/libcxx/include/__ranges/ref_view.h
@@ -18,6 +18,7 @@
#include <__ranges/concepts.h>
#include <__ranges/data.h>
#include <__ranges/empty.h>
+#include <__ranges/enable_borrowed_range.h>
#include <__ranges/size.h>
#include <__ranges/view_interface.h>
#include <__utility/forward.h>
@@ -74,6 +75,8 @@ namespace ranges {
template<class _Range>
ref_view(_Range&) -> ref_view<_Range>;
+ template<class _Tp>
+ inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
} // namespace ranges
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
diff --git a/libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/borrowing.compile.pass.cpp
index 59cc12fd30aab..5a98b316b5e17 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/borrowing.compile.pass.cpp
@@ -14,20 +14,8 @@
// inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;
#include <ranges>
-#include <cassert>
-struct Range {
- int *begin() const;
- int *end() const;
-};
+#include "test_range.h"
-struct BorrowableRange {
- int *begin() const;
- int *end() const;
-};
-
-template<>
-inline constexpr bool std::ranges::enable_borrowed_range<BorrowableRange> = true;
-
-static_assert(!std::ranges::enable_borrowed_range<std::ranges::owning_view<Range>>);
-static_assert( std::ranges::enable_borrowed_range<std::ranges::owning_view<BorrowableRange>>);
+static_assert( std::ranges::borrowed_range<std::ranges::owning_view<BorrowedView>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::owning_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
similarity index 58%
rename from libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp
rename to libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
index 90bbfde39a188..43d7230def33b 100644
--- a/libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
@@ -10,14 +10,13 @@
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
-// class std::ranges::subrange;
+// template<class T>
+// inline constexpr bool enable_borrowed_range<ref_view<T>> = true;
#include <ranges>
-#include "test_iterators.h"
+#include "test_range.h"
-namespace ranges = std::ranges;
-
-static_assert(ranges::borrowed_range<ranges::subrange<int*>>);
-static_assert(ranges::borrowed_range<ranges::subrange<int*, int const*>>);
-static_assert(ranges::borrowed_range<ranges::subrange<int*, sentinel_wrapper<int*>, ranges::subrange_kind::unsized>>);
+static_assert(std::ranges::borrowed_range<std::ranges::ref_view<BorrowedRange>>);
+static_assert(std::ranges::borrowed_range<std::ranges::ref_view<BorrowedView>>);
+static_assert(std::ranges::borrowed_range<std::ranges::ref_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.ref.view.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/range.ref.view.pass.cpp
similarity index 100%
rename from libcxx/test/std/ranges/range.adaptors/range.ref.view.pass.cpp
rename to libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/range.ref.view.pass.cpp
diff --git a/libcxx/test/std/ranges/range.adaptors/range.common.view/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.common.view/borrowing.compile.pass.cpp
index 83283789e8bf3..1da93c80b3e08 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.common.view/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.common.view/borrowing.compile.pass.cpp
@@ -14,21 +14,13 @@
// inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;
#include <ranges>
-#include <cassert>
-#include "test_iterators.h"
-
-struct View : std::ranges::view_base {
- int *begin() const;
- sentinel_wrapper<int*> end() const;
-};
-
-struct BorrowableView : std::ranges::view_base {
- int *begin() const;
- sentinel_wrapper<int*> end() const;
+// common_view can only wrap Ts that are `view<T> && !common_range<T>`, so we need to invent one.
+struct Uncommon : std::ranges::view_base {
+ int *begin() const;
+ std::unreachable_sentinel_t end() const;
};
-template<>
-inline constexpr bool std::ranges::enable_borrowed_range<BorrowableView> = true;
-static_assert(!std::ranges::enable_borrowed_range<std::ranges::common_view<View>>);
-static_assert( std::ranges::enable_borrowed_range<std::ranges::common_view<BorrowableView>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::common_view<Uncommon>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::common_view<std::ranges::owning_view<Uncommon>>>);
+static_assert( std::ranges::borrowed_range<std::ranges::common_view<std::ranges::ref_view<Uncommon>>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.empty/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.empty/borrowing.compile.pass.cpp
new file mode 100644
index 0000000000000..0c4ba79ca90c1
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.empty/borrowing.compile.pass.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// template<class T>
+// inline constexpr bool enable_borrowed_range<empty_view<T>> = true;
+
+#include <ranges>
+
+#include "test_range.h"
+
+static_assert(std::ranges::borrowed_range<std::ranges::empty_view<int>>);
+static_assert(std::ranges::borrowed_range<std::ranges::empty_view<int*>>);
+static_assert(std::ranges::borrowed_range<std::ranges::empty_view<BorrowedView>>);
+static_assert(std::ranges::borrowed_range<std::ranges::empty_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.reverse/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.reverse/borrowing.compile.pass.cpp
index 99944aaf6a054..45d388d046101 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.reverse/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.reverse/borrowing.compile.pass.cpp
@@ -14,26 +14,8 @@
// inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;
#include <ranges>
-#include <cassert>
-#include "test_iterators.h"
+#include "test_range.h"
-struct View : std::ranges::view_base {
- friend int* begin(View&);
- friend int* begin(View const&);
- friend sentinel_wrapper<int*> end(View&);
- friend sentinel_wrapper<int*> end(View const&);
-};
-
-struct BorrowableView : std::ranges::view_base {
- friend int* begin(BorrowableView&);
- friend int* begin(BorrowableView const&);
- friend sentinel_wrapper<int*> end(BorrowableView&);
- friend sentinel_wrapper<int*> end(BorrowableView const&);
-};
-
-template<>
-inline constexpr bool std::ranges::enable_borrowed_range<BorrowableView> = true;
-
-static_assert(!std::ranges::enable_borrowed_range<std::ranges::reverse_view<View>>);
-static_assert( std::ranges::enable_borrowed_range<std::ranges::reverse_view<BorrowableView>>);
+static_assert( std::ranges::borrowed_range<std::ranges::reverse_view<BorrowedView>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::reverse_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp
index ff1766d6f41f0..b5a7358ea0d7c 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp
@@ -14,26 +14,8 @@
// inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;
#include <ranges>
-#include <cassert>
-#include "test_iterators.h"
+#include "test_range.h"
-struct View : std::ranges::view_base {
- friend int* begin(View&);
- friend int* begin(View const&);
- friend sentinel_wrapper<int*> end(View&);
- friend sentinel_wrapper<int*> end(View const&);
-};
-
-struct BorrowableView : std::ranges::view_base {
- friend int* begin(BorrowableView&);
- friend int* begin(BorrowableView const&);
- friend sentinel_wrapper<int*> end(BorrowableView&);
- friend sentinel_wrapper<int*> end(BorrowableView const&);
-};
-
-template<>
-inline constexpr bool std::ranges::enable_borrowed_range<BorrowableView> = true;
-
-static_assert(!std::ranges::enable_borrowed_range<std::ranges::take_view<View>>);
-static_assert( std::ranges::enable_borrowed_range<std::ranges::take_view<BorrowableView>>);
+static_assert( std::ranges::borrowed_range<std::ranges::take_view<BorrowedView>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::take_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.factories/range.iota.view/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.factories/range.iota.view/borrowing.compile.pass.cpp
index 7fbf987463652..eb4414d554b8b 100644
--- a/libcxx/test/std/ranges/range.factories/range.iota.view/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.factories/range.iota.view/borrowing.compile.pass.cpp
@@ -14,12 +14,8 @@
// inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;
#include <ranges>
-#include <cassert>
-#include <concepts>
-#include "test_macros.h"
-#include "types.h"
-
-static_assert(std::ranges::enable_borrowed_range<std::ranges::iota_view<int, int>>);
-static_assert(std::ranges::enable_borrowed_range<std::ranges::iota_view<int, std::unreachable_sentinel_t>>);
-static_assert(std::ranges::enable_borrowed_range<std::ranges::iota_view<int, IntComparableWith<int>>>);
+static_assert(std::ranges::borrowed_range<std::ranges::iota_view<int, int>>);
+static_assert(std::ranges::borrowed_range<std::ranges::iota_view<int, std::unreachable_sentinel_t>>);
+static_assert(std::ranges::borrowed_range<std::ranges::iota_view<int*, int*>>);
+static_assert(std::ranges::borrowed_range<std::ranges::iota_view<int*, std::unreachable_sentinel_t>>);
diff --git a/libcxx/test/std/ranges/range.factories/range.single.view/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.factories/range.single.view/borrowing.compile.pass.cpp
new file mode 100644
index 0000000000000..d925b3f9a6b6e
--- /dev/null
+++ b/libcxx/test/std/ranges/range.factories/range.single.view/borrowing.compile.pass.cpp
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// single_view does not specialize enable_borrowed_range
+
+#include <ranges>
+
+#include "test_range.h"
+
+static_assert(!std::ranges::borrowed_range<std::ranges::single_view<int>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::single_view<int*>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::single_view<BorrowedView>>);
+static_assert(!std::ranges::borrowed_range<std::ranges::single_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/borrowing.compile.pass.cpp
new file mode 100644
index 0000000000000..6c76bfb1a6974
--- /dev/null
+++ b/libcxx/test/std/ranges/range.utility/range.subrange/borrowing.compile.pass.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// template<class I, class S, subrange_kind K>
+// inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
+
+#include <ranges>
+
+static_assert(std::ranges::borrowed_range<std::ranges::subrange<int*, const int*, std::ranges::subrange_kind::sized>>);
+static_assert(std::ranges::borrowed_range<std::ranges::subrange<int*, std::unreachable_sentinel_t, std::ranges::subrange_kind::sized>>);
+static_assert(std::ranges::borrowed_range<std::ranges::subrange<int*, std::unreachable_sentinel_t, std::ranges::subrange_kind::unsized>>);
diff --git a/libcxx/test/support/test_range.h b/libcxx/test/support/test_range.h
index a9b88621e16b0..598b1d0f546cb 100644
--- a/libcxx/test/support/test_range.h
+++ b/libcxx/test/support/test_range.h
@@ -62,4 +62,21 @@ struct test_view : std::ranges::view_base {
sentinel end() const;
};
+struct BorrowedRange {
+ int *begin() const;
+ int *end() const;
+ BorrowedRange(BorrowedRange&&) = delete;
+};
+template<> inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange> = true;
+static_assert(!std::ranges::view<BorrowedRange>);
+static_assert(std::ranges::borrowed_range<BorrowedRange>);
+
+using BorrowedView = std::ranges::empty_view<int>;
+static_assert(std::ranges::view<BorrowedView>);
+static_assert(std::ranges::borrowed_range<BorrowedView>);
+
+using NonBorrowedView = std::ranges::single_view<int>;
+static_assert(std::ranges::view<NonBorrowedView>);
+static_assert(!std::ranges::borrowed_range<NonBorrowedView>);
+
#endif // LIBCXX_TEST_SUPPORT_TEST_RANGE_H
More information about the libcxx-commits
mailing list