[libcxx-commits] [libcxx] [libc++][NFC] Check for take_view returning different types depending on simple_view-ness (PR #79085)

Will Hawkins via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 23 12:55:58 PST 2024


https://github.com/hawkinsw updated https://github.com/llvm/llvm-project/pull/79085

>From 60a03ea866f96719d45714cc22bcca4df6399fc1 Mon Sep 17 00:00:00 2001
From: Will Hawkins <hawkinsw at obs.cr>
Date: Mon, 22 Jan 2024 22:14:20 -0500
Subject: [PATCH 1/3] [libc++][NFC] Check for take_view returning different
 types depending on simple_view-ness

Add a check to make sure that an instance of a
take_view::_iterator<false> is returned when the base type is not a simple
view nor a sized view.
---
 .../range.adaptors/range.take/end.pass.cpp    | 21 ++++++++++++
 .../ranges/range.adaptors/range.take/types.h  | 33 +++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
index 6cab05daa9e059a..a54b81c83e6aadb 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
@@ -69,6 +69,27 @@ constexpr bool test() {
     assert(tv.end() == std::ranges::next(tv.begin(), 8));
   }
 
+  // Check that (non-)simple, non-sized views have different types for their end() member function.
+  {
+    // These assertions must be true in order to trigger the different paths through the end member function
+    // that will return values with different types:
+    static_assert(!simple_view<NonSimpleViewNonSized>);
+    static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
+    static_assert(simple_view<SimpleViewNonSized>);
+    static_assert(std::ranges::range<const SimpleViewNonSized>);
+    static_assert(!std::ranges::sized_range<const SimpleViewNonSized>);
+
+    std::ranges::take_view<NonSimpleViewNonSized> tvns(NonSimpleViewNonSized{buffer, buffer + 8}, 0);
+    std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0);
+
+    // __iterator<false> has base with type std::ranges::sentinel_t<NonSimpleViewNonSized>; adding a const qualifier
+    // would change the equality.
+    static_assert(!std::is_same_v<decltype(tvns.end().base()), std::ranges::sentinel_t<const NonSimpleViewNonSized>>);
+    // __iterator<true> has base with type std::ranges::sentinel_t<const NonSimpleViewNonSized>; adding a const qualifier
+    // would not change the equality.
+    static_assert(std::is_same_v<decltype(tvs.end().base()), std::ranges::sentinel_t<const SimpleViewNonSized>>);
+  }
+
   return true;
 }
 
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/types.h b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
index db80e68bb21afef..083e02ac7cac7ca 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
@@ -65,4 +65,37 @@ struct View : std::ranges::view_base {
   int* end_;
 };
 
+template <bool Simple>
+struct InputView : std::ranges::view_base {
+  constexpr explicit InputView(int* b, int* e) : begin_(b), end_(e) {}
+
+  constexpr common_input_iterator<int*> begin() const { return common_input_iterator<int*>(begin_); }
+  constexpr common_input_iterator<int*> end() const { return common_input_iterator<int*>(end_); }
+
+  constexpr common_input_iterator<const int*> begin()
+    requires(!Simple)
+  {
+    return common_input_iterator<const int*>(begin_);
+  }
+  constexpr common_input_iterator<const int*> end()
+    requires(!Simple)
+  {
+    return common_input_iterator<const int*>(end_);
+  }
+
+private:
+  int* begin_;
+  int* end_;
+};
+
+using NonSimpleViewNonSized = InputView<false>;
+static_assert(std::ranges::view<NonSimpleViewNonSized>);
+static_assert(!simple_view<NonSimpleViewNonSized>);
+static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
+
+using SimpleViewNonSized = InputView<true>;
+static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
+static_assert(std::ranges::view<SimpleViewNonSized>);
+static_assert(simple_view<SimpleViewNonSized>);
+
 #endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H

>From bac7229289a9a6a6af3b074fd508488163f61d42 Mon Sep 17 00:00:00 2001
From: Will Hawkins <hawkinsw at obs.cr>
Date: Tue, 23 Jan 2024 15:54:07 -0500
Subject: [PATCH 2/3] fixup! [libc++][NFC] Check for take_view returning
 different types depending on simple_view-ness

Rename test class and refactor.
---
 .../range.adaptors/range.take/end.pass.cpp      | 17 +++++------------
 .../ranges/range.adaptors/range.take/types.h    |  8 ++++----
 2 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
index a54b81c83e6aadb..a5c1dfdca13adf1 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
@@ -69,24 +69,17 @@ constexpr bool test() {
     assert(tv.end() == std::ranges::next(tv.begin(), 8));
   }
 
-  // Check that (non-)simple, non-sized views have different types for their end() member function.
   {
-    // These assertions must be true in order to trigger the different paths through the end member function
-    // that will return values with different types:
-    static_assert(!simple_view<NonSimpleViewNonSized>);
-    static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
-    static_assert(simple_view<SimpleViewNonSized>);
-    static_assert(std::ranges::range<const SimpleViewNonSized>);
-    static_assert(!std::ranges::sized_range<const SimpleViewNonSized>);
-
-    std::ranges::take_view<NonSimpleViewNonSized> tvns(NonSimpleViewNonSized{buffer, buffer + 8}, 0);
-    std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0);
-
     // __iterator<false> has base with type std::ranges::sentinel_t<NonSimpleViewNonSized>; adding a const qualifier
     // would change the equality.
+    std::ranges::take_view<NonSimpleViewNonSized> tvns(NonSimpleViewNonSized{buffer, buffer + 8}, 0);
     static_assert(!std::is_same_v<decltype(tvns.end().base()), std::ranges::sentinel_t<const NonSimpleViewNonSized>>);
+  }
+
+  {
     // __iterator<true> has base with type std::ranges::sentinel_t<const NonSimpleViewNonSized>; adding a const qualifier
     // would not change the equality.
+    std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0);
     static_assert(std::is_same_v<decltype(tvs.end().base()), std::ranges::sentinel_t<const SimpleViewNonSized>>);
   }
 
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/types.h b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
index 083e02ac7cac7ca..207505f0c72c123 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
@@ -66,8 +66,8 @@ struct View : std::ranges::view_base {
 };
 
 template <bool Simple>
-struct InputView : std::ranges::view_base {
-  constexpr explicit InputView(int* b, int* e) : begin_(b), end_(e) {}
+struct CommonInputView : std::ranges::view_base {
+  constexpr explicit CommonInputView(int* b, int* e) : begin_(b), end_(e) {}
 
   constexpr common_input_iterator<int*> begin() const { return common_input_iterator<int*>(begin_); }
   constexpr common_input_iterator<int*> end() const { return common_input_iterator<int*>(end_); }
@@ -88,12 +88,12 @@ struct InputView : std::ranges::view_base {
   int* end_;
 };
 
-using NonSimpleViewNonSized = InputView<false>;
+using NonSimpleViewNonSized = CommonInputView<false>;
 static_assert(std::ranges::view<NonSimpleViewNonSized>);
 static_assert(!simple_view<NonSimpleViewNonSized>);
 static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
 
-using SimpleViewNonSized = InputView<true>;
+using SimpleViewNonSized = CommonInputView<true>;
 static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
 static_assert(std::ranges::view<SimpleViewNonSized>);
 static_assert(simple_view<SimpleViewNonSized>);

>From 71d9a06148c753afe7892bf92bc10ec5bc7492a1 Mon Sep 17 00:00:00 2001
From: Will Hawkins <hawkinsw at obs.cr>
Date: Tue, 23 Jan 2024 15:55:48 -0500
Subject: [PATCH 3/3] fixup! [libc++][NFC] Check for take_view returning
 different types depending on simple_view-ness

Rearrange static_asserts to match.
---
 libcxx/test/std/ranges/range.adaptors/range.take/types.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/types.h b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
index 207505f0c72c123..68a6f1ab875c515 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
@@ -94,8 +94,8 @@ static_assert(!simple_view<NonSimpleViewNonSized>);
 static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
 
 using SimpleViewNonSized = CommonInputView<true>;
-static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
 static_assert(std::ranges::view<SimpleViewNonSized>);
 static_assert(simple_view<SimpleViewNonSized>);
+static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
 
 #endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H



More information about the libcxx-commits mailing list