]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix <ranges> tests that fail in C++23
authorJonathan Wakely <jwakely@redhat.com>
Wed, 6 Sep 2023 13:13:18 +0000 (14:13 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 7 Sep 2023 07:09:58 +0000 (08:09 +0100)
The tests for the std::ranges access CPOs (ranges::begin etc) use
pathological types with ridiculous overload sets for begin/end/data
members, to exercise all the corner cases in the specification.

Since P2278R4 "cbegin should always return a constant iterator" was
implemented for C++23 mode, some of the range access CPOs now require
the argument to satisfy the range concept, which was not previously
required. The behaviour of the CPO also changes for corner cases where
the type is a range R for which constant_range<R> is satisfied in
addition to constant_range<const R> (meaning there's no need to wrap its
iterators in const_iterator). Adjust the expected results for those
pathological types that changed meaning in C++23, and add some new types
to verify other corner cases.

Some other range adaptor tests fail for C++20 because they assert that
ranges::end and ranges::cend return different types, which is not true
when the type satisfies constant_range.

This fixes the tests to PASS for both C++20 and C++23 (and later).

libstdc++-v3/ChangeLog:

* testsuite/std/ranges/access/cbegin.cc: Adjust for C++23
compatibility.
* testsuite/std/ranges/access/cdata.cc: Likewise.
* testsuite/std/ranges/access/cend.cc: Likewise.
* testsuite/std/ranges/access/crbegin.cc: Likewise.
* testsuite/std/ranges/access/crend.cc: Likewise.
* testsuite/std/ranges/adaptors/take.cc: Likewise.
* testsuite/std/ranges/adaptors/take_while.cc: Likewise.
* testsuite/std/ranges/adaptors/transform.cc: Likewise.

libstdc++-v3/testsuite/std/ranges/access/cbegin.cc
libstdc++-v3/testsuite/std/ranges/access/cdata.cc
libstdc++-v3/testsuite/std/ranges/access/cend.cc
libstdc++-v3/testsuite/std/ranges/access/crbegin.cc
libstdc++-v3/testsuite/std/ranges/access/crend.cc
libstdc++-v3/testsuite/std/ranges/adaptors/take.cc
libstdc++-v3/testsuite/std/ranges/adaptors/take_while.cc
libstdc++-v3/testsuite/std/ranges/adaptors/transform.cc

index 10376040c16ebd5c16e1b9a80a9c64ff57419a60..3667b0d021fb5622198df38297e372b539823cdb 100644 (file)
@@ -48,6 +48,10 @@ struct R
   friend int* begin(R&&); // this function is not defined
   friend const int* begin(const R& r) noexcept { return r.a + 2; }
   friend const int* begin(const R&&); // this function is not defined
+
+#if __cpp_lib_ranges_as_const
+  friend const int* end(const R&) noexcept; // C++23 requires this.
+#endif
 };
 
 struct RV // view on an R
@@ -56,6 +60,10 @@ struct RV // view on an R
 
   friend int* begin(RV&); // this function is not defined
   friend const int* begin(const RV& rv) noexcept { return begin(std::as_const(rv.r)); }
+
+#if __cpp_lib_ranges_as_const
+  friend const int* end(const RV&) noexcept; // C++23 requires this.
+#endif
 };
 
 // Allow ranges::begin to work with RV&&
@@ -88,6 +96,11 @@ struct RR
   friend int* begin(RR&& r) { return r.a + 1; }
   friend const int* begin(const RR& r) { return r.a + 2; }
   friend const int* begin(const RR&& r) noexcept { return r.a + 3; }
+
+#if __cpp_lib_ranges_as_const
+  short* end() noexcept { return &s + 1; }   // C++23 requires this.
+  const long* end() const { return &l + 1; } // C++23 requires this.
+#endif
 };
 
 // N.B. this is a lie, cbegin on an RR rvalue will return a dangling pointer.
index f0f45eeb6bf2e313c32a9ce487c06958ecc91cbb..d69b04c8f7440e4697832ea04f802c0f7bfbf267 100644 (file)
@@ -41,15 +41,43 @@ test01()
   static_assert( has_cdata<R&> );
   static_assert( has_cdata<const R&> );
   R r;
-  const R& c = r;
+#if ! __cpp_lib_ranges_as_const
   VERIFY( std::ranges::cdata(r) == (R*)nullptr );
   static_assert( noexcept(std::ranges::cdata(r)) );
+#else
+  // constant_range<const R> is not satisfied, so cdata(r) == data(r).
+  VERIFY( std::ranges::cdata(r) == &r.j );
+  static_assert( ! noexcept(std::ranges::cdata(r)) );
+#endif
+  const R& c = r;
   VERIFY( std::ranges::cdata(c) == (R*)nullptr );
   static_assert( noexcept(std::ranges::cdata(c)) );
 
   // not lvalues and not borrowed ranges
   static_assert( !has_cdata<R> );
   static_assert( !has_cdata<const R> );
+
+  struct R2
+  {
+    // These overloads mean that range<R2> and range<const R2> are satisfied.
+    int* begin();
+    int* end();
+    const int* begin() const;
+    const int* end() const;
+
+    int i = 0;
+    int j = 0;
+    int* data() { return &j; }
+    const R2* data() const noexcept { return nullptr; }
+  };
+  static_assert( has_cdata<R2&> );
+  static_assert( has_cdata<const R2&> );
+  R2 r2;
+  VERIFY( std::ranges::cdata(r2) == (R2*)nullptr );
+  static_assert( noexcept(std::ranges::cdata(r2)) );
+  const R2& c2 = r2;
+  VERIFY( std::ranges::cdata(c2) == (R2*)nullptr );
+  static_assert( noexcept(std::ranges::cdata(c2)) );
 }
 
 void
@@ -71,6 +99,14 @@ struct R3
   friend long* begin(R3&& r); // not defined
   friend const long* begin(const R3& r) { return &r.l; }
   friend const short* begin(const R3&&); // not defined
+
+#if __cpp_lib_ranges_as_const
+  // C++23 needs these so that range<R3> is satisfied and so that
+  // possibly-const-range<R3> is not the same type as R3.
+  friend long* begin(R3&);
+  friend long* end(R3&);
+  friend const long* end(const R3& r);
+#endif
 };
 
 template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;
index f47bd9d91358b17e521a86256da543226ea75e75..3726ebbf118502e4d3e3495aaf13870ed1d1843f 100644 (file)
@@ -42,7 +42,7 @@ struct R
   int a[4] = { 0, 1, 2, 3 };
 
   const int* begin() const { return nullptr; }
-  friend const int* begin(const R&& r) noexcept { return nullptr; }
+  friend const int* begin(const R&&) noexcept { return nullptr; }
 
   // Should be ignored because it doesn't return a sentinel for int*
   const long* end() const { return nullptr; }
@@ -53,6 +53,15 @@ struct R
   friend const int* end(const R&& r) noexcept { return r.a + 3; }
 };
 
+#if __cpp_lib_ranges_as_const
+struct R2 : R
+{
+  // This overload means constant_range<const R2> will be satisfied:
+  friend const int* begin(const R2&) noexcept;
+  friend const int* end(const R2& r2) noexcept { return r2.a + 2; }
+};
+#endif
+
 struct RV // view on an R
 {
   R& r;
@@ -71,12 +80,28 @@ test03()
 {
   R r;
   const R& c = r;
+#if ! __cpp_lib_ranges_as_const
   VERIFY( std::ranges::cend(r) == std::ranges::end(c) );
+#else
+  // constant_range<const R> is not satisfied, so cend(r) == end(r) instead.
+  VERIFY( std::ranges::cend(r) == std::ranges::end(r) );
+  R2 r2;
+  const R& c2 = r2;
+  // But constant_range<const R2> is satisfied, so cend(r2) == end(c2).
+  VERIFY( std::ranges::cend(r2) == std::ranges::end(c2) );
+  VERIFY( std::ranges::cend(r2) == std::ranges::end((const R&)c2) );
+#endif
   VERIFY( std::ranges::cend(c) == std::ranges::end(c) );
 
   RV v{r};
-  const RV cv{r};
+#if ! __cpp_lib_ranges_as_const
   VERIFY( std::ranges::cend(std::move(v)) == std::ranges::end(c) );
+#else
+  // constant_range<RV> is already satisfied, so cend(v) == end(r) instead.
+  VERIFY( std::ranges::cend(std::move(v)) == std::ranges::end(r) );
+#endif
+
+  const RV cv{r};
   VERIFY( std::ranges::cend(std::move(cv)) == std::ranges::end(c) );
 }
 
index db58d9577d901a9358aec293ddd3d4d3d85323a4..95b4607fdf1be84a979f10aabffdb5ffb0e27aa2 100644 (file)
@@ -37,13 +37,33 @@ struct R1V // view on an R1
 {
   R1& r;
 
-  friend const long* rbegin(R1V&); // this is not defined
+  friend const long* rbegin(R1V&) { return nullptr; }
   friend const int* rbegin(const R1V& rv) noexcept { return rv.r.rbegin(); }
 };
 
 // Allow ranges::end to work with R1V&&
 template<> constexpr bool std::ranges::enable_borrowed_range<R1V> = true;
 
+#if __cpp_lib_ranges_as_const
+struct R1VC // view on an R1
+{
+  R1& r;
+
+  friend const long* rbegin(R1VC&); // this is not defined
+  friend const int* rbegin(const R1VC& rv) noexcept { return rv.r.rbegin(); }
+
+  // The following ensure that the following are satisfied:
+  // constant_range<const R1VC> && ! constant_range<R1VC>
+  friend int* begin(R1VC&);
+  friend int* end(R1VC&);
+  friend const int* begin(const R1VC&);
+  friend const int* end(const R1VC&);
+};
+
+// Allow ranges::end to work with R1VC&&
+template<> constexpr bool std::ranges::enable_borrowed_range<R1VC> = true;
+#endif
+
 void
 test01()
 {
@@ -53,8 +73,24 @@ test01()
   VERIFY( std::ranges::crbegin(c) == std::ranges::rbegin(c) );
 
   R1V v{r};
-  const R1V cv{r};
+#if ! __cpp_lib_ranges_as_const
+  VERIFY( std::ranges::crbegin(v) == std::ranges::rbegin(c) );
   VERIFY( std::ranges::crbegin(std::move(v)) == std::ranges::rbegin(c) );
+#else
+  // constant_range<const R1V> is not satisfied, so crbegin(v) == rbegin(v).
+  VERIFY( std::ranges::crbegin(v) == (long*)nullptr );
+  VERIFY( std::ranges::crbegin(std::move(v)) == (long*)nullptr );
+  R1VC v2{r};
+  // But constant_range<const R1VC> is satisfied:
+  VERIFY( std::ranges::crbegin(v2) == std::ranges::rbegin(c) );
+  VERIFY( std::ranges::crbegin(std::move(v2)) == std::ranges::rbegin(c) );
+  const R1VC cv2{r};
+  VERIFY( std::ranges::crbegin(cv2) == std::ranges::rbegin(c) );
+  VERIFY( std::ranges::crbegin(std::move(cv2)) == std::ranges::rbegin(c) );
+#endif
+
+  const R1V cv{r};
+  VERIFY( std::ranges::crbegin(cv) == std::ranges::rbegin(c) );
   VERIFY( std::ranges::crbegin(std::move(cv)) == std::ranges::rbegin(c) );
 }
 
index a7e033fc317159c93f7b8a557912af4284a1a434..2e4c0f3197a0346353ac97fb31ebb9e6e8837c64 100644 (file)
@@ -86,18 +86,47 @@ struct R3
   friend const int* rend(const R3& r) { return &r.i; }
 };
 
-// N.B. this is a lie, rend on an R3 rvalue will return a dangling pointer.
-template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;
+struct R4
+{
+  int i = 0;
+
+  // These members mean that range<R4> and range<const R4> are satisfied.
+  const short* begin() const { return 0; }
+  const short* end() const { return 0; }
+
+  const int* rbegin() const noexcept { return &i + 1; }
+  const long* rend() const noexcept { return nullptr; } // not a sentinel for rbegin()
+
+  friend const long* rbegin(const R4&) noexcept { return nullptr; }
+  friend const int* rend(const R4& r) { return &r.i; }
+};
 
 void
 test03()
 {
   R3 r;
   const R3& c = r;
+#if ! __cpp_lib_ranges_as_const
   VERIFY( std::ranges::crend(r) == std::ranges::rend(c) );
   static_assert( !noexcept(std::ranges::crend(r)) );
+#else
+  // constant_range<const R3> is not satisfied, so crend(r) is equivalent
+  // to const_sentinel{rend(r)}, which is ill-formed because range<R3>
+  // is not satisfied.
+  static_assert( not std::ranges::range<R3> );
+  static_assert( not std::ranges::range<const R3> );
+#endif
   VERIFY( std::ranges::crend(c) == std::ranges::rend(c) );
   static_assert( !noexcept(std::ranges::crend(c)) );
+
+  R4 r4;
+  const R4& c4 = r4;
+  auto b = std::ranges::rbegin(r4);
+  auto s0 = std::ranges::rend(r4);
+  static_assert( std::ranges::__access::__adl_rend<R4&> );
+  auto s = std::ranges::crend(r4);
+  auto s2 = std::ranges::rend(c4);
+  // VERIFY( std::ranges::crend(r4) == std::ranges::rend(c4) );
 }
 
 void
index 290b5c2bba59e321c907e9d2b43f69e0d9cc87a9..a1f6068bc09bf508b19371534e69fac1fb7975ff 100644 (file)
@@ -94,8 +94,10 @@ test05()
 
   // Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>.
   static_assert(!ranges::common_range<decltype(v)>);
+#if ! __cpp_lib_ranges_as_const
   static_assert(!std::same_as<decltype(ranges::end(v)),
                              decltype(ranges::cend(v))>);
+#endif
   auto b = ranges::cend(v);
   b = ranges::end(v);
 }
index 46060771483d2867a9badb50ae4731816391f319..098091edc8b6332dd2808bc95c42b033e2979d5b 100644 (file)
@@ -64,8 +64,10 @@ test03()
 
   // Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>.
   static_assert(!ranges::common_range<decltype(v)>);
+#if ! __cpp_lib_ranges_as_const
   static_assert(!std::same_as<decltype(ranges::end(v)),
                              decltype(ranges::cend(v))>);
+#endif
   auto b = ranges::cend(v);
   b = ranges::end(v);
 }
index 9d52cb01dadb0ada374fdc7bfb89d3cbc8d2785f..d9801d5abb3949170f380d0d7fd8bb62d743d610 100644 (file)
@@ -108,16 +108,20 @@ test05()
   auto r = ranges::subrange{i, std::default_sentinel};
   auto v = r | views::transform(std::negate{});
 
+#if ! __cpp_lib_ranges_as_const
   // Verify that _Iterator<false> is implicitly convertible to _Iterator<true>.
   static_assert(!std::same_as<decltype(ranges::begin(v)),
                              decltype(ranges::cbegin(v))>);
+#endif
   auto a = ranges::cbegin(v);
   a = ranges::begin(v);
 
+#if ! __cpp_lib_ranges_as_const
   // Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>.
   static_assert(!ranges::common_range<decltype(v)>);
   static_assert(!std::same_as<decltype(ranges::end(v)),
                              decltype(ranges::cend(v))>);
+#endif
   auto b = ranges::cend(v);
   b = ranges::end(v);
 }