]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Make ranges::size and ranges::empty check for unbounded arrays
authorJonathan Wakely <jwakely@redhat.com>
Sun, 12 Dec 2021 21:16:25 +0000 (21:16 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 13 Dec 2021 11:15:41 +0000 (11:15 +0000)
Passing IncompleteType(&)[] to ranges::begin produces an error outside
the immediate context, which is fine for ranges::begin, but it means
that we fail to enforce the SFINAE-able constraints for ranges::size and
ranges::size. They should not be callable for any array of unknown
bound, whether the type is complete or not. Because we don't enforce
that in their constraints, we get a hard error when they try to use
ranges::begin.

This simply adds explicit checks for arrays of unknown bound to the
constraints for ranges::size and ranges::empty. We only need to check it
for the __sentinel_size and __eq_iter_empty concepts, because those are
the ones that are relevant to arrays, and which try to use
ranges::begin.

libstdc++-v3/ChangeLog:

* include/bits/ranges_base.h (ranges::size, ranges::empty): Add
explicit check for unbounded arrays before using ranges::begin.
* testsuite/std/ranges/access/empty.cc: Check handling of unbounded
arrays.
* testsuite/std/ranges/access/size.cc: Likewise.

libstdc++-v3/include/bits/ranges_base.h
libstdc++-v3/testsuite/std/ranges/access/empty.cc
libstdc++-v3/testsuite/std/ranges/access/size.cc

index 43b0b9f7bf3044ceb44351fb14649e95be097703..bf95823668b5486eab97c8efa397dfe06bc6b69e 100644 (file)
@@ -383,6 +383,8 @@ namespace ranges
     template<typename _Tp>
       concept __sentinel_size = requires(_Tp& __t)
        {
+         requires (!is_unbounded_array_v<remove_reference_t<_Tp>>);
+
          { _Begin{}(__t) } -> forward_iterator;
 
          { _End{}(__t) } -> sized_sentinel_for<decltype(_Begin{}(__t))>;
@@ -466,6 +468,8 @@ namespace ranges
     template<typename _Tp>
       concept __eq_iter_empty = requires(_Tp& __t)
        {
+         requires (!is_unbounded_array_v<remove_reference_t<_Tp>>);
+
          { _Begin{}(__t) } -> forward_iterator;
 
          bool(_Begin{}(__t) == _End{}(__t));
index 3cad47401240438d34ef11715150d02939e6cae5..83b40872bd42f91a13c0d30355a2986b52f378b1 100644 (file)
@@ -121,6 +121,16 @@ test04()
   static_assert( ! noexcept(std::ranges::empty(E3{})) );
 }
 
+template<typename T>
+  concept has_empty = requires (T& t) { std::ranges::empty(t); };
+
+// If T is an array of unknown bound, ranges::empty(E) is ill-formed.
+static_assert( ! has_empty<int[]> );
+static_assert( ! has_empty<int(&)[]> );
+static_assert( ! has_empty<int[][2]> );
+struct Incomplete;
+static_assert( ! has_empty<Incomplete[]> );
+
 int
 main()
 {
index c7e4f78f96a24960f93c7dbcb91632e7ace903e8..24857975f7b03fa1e56f61a02fdd7a51ad7d2070 100644 (file)
@@ -122,6 +122,16 @@ test06()
   static_assert( std::ranges::size(R{}) == 42 );
 }
 
+template<typename T>
+  concept has_size = requires (T& t) { std::ranges::size(t); };
+
+// If T is an array of unknown bound, ranges::size(E) is ill-formed.
+static_assert( ! has_size<int[]> );
+static_assert( ! has_size<int(&)[]> );
+static_assert( ! has_size<int[][2]> );
+struct Incomplete;
+static_assert( ! has_size<Incomplete[]> );
+
 int
 main()
 {