From: Jonathan Wakely Date: Sun, 12 Dec 2021 21:16:25 +0000 (+0000) Subject: libstdc++: Make ranges::size and ranges::empty check for unbounded arrays X-Git-Tag: basepoints/gcc-13~2388 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55823c5a0ba50022d7fcc95e74ec293143810ef6;p=thirdparty%2Fgcc.git libstdc++: Make ranges::size and ranges::empty check for unbounded arrays 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. --- diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 43b0b9f7bf30..bf95823668b5 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -383,6 +383,8 @@ namespace ranges template concept __sentinel_size = requires(_Tp& __t) { + requires (!is_unbounded_array_v>); + { _Begin{}(__t) } -> forward_iterator; { _End{}(__t) } -> sized_sentinel_for; @@ -466,6 +468,8 @@ namespace ranges template concept __eq_iter_empty = requires(_Tp& __t) { + requires (!is_unbounded_array_v>); + { _Begin{}(__t) } -> forward_iterator; bool(_Begin{}(__t) == _End{}(__t)); diff --git a/libstdc++-v3/testsuite/std/ranges/access/empty.cc b/libstdc++-v3/testsuite/std/ranges/access/empty.cc index 3cad47401240..83b40872bd42 100644 --- a/libstdc++-v3/testsuite/std/ranges/access/empty.cc +++ b/libstdc++-v3/testsuite/std/ranges/access/empty.cc @@ -121,6 +121,16 @@ test04() static_assert( ! noexcept(std::ranges::empty(E3{})) ); } +template + 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 ); +static_assert( ! has_empty ); +static_assert( ! has_empty ); +struct Incomplete; +static_assert( ! has_empty ); + int main() { diff --git a/libstdc++-v3/testsuite/std/ranges/access/size.cc b/libstdc++-v3/testsuite/std/ranges/access/size.cc index c7e4f78f96a2..24857975f7b0 100644 --- a/libstdc++-v3/testsuite/std/ranges/access/size.cc +++ b/libstdc++-v3/testsuite/std/ranges/access/size.cc @@ -122,6 +122,16 @@ test06() static_assert( std::ranges::size(R{}) == 42 ); } +template + 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 ); +static_assert( ! has_size ); +static_assert( ! has_size ); +struct Incomplete; +static_assert( ! has_size ); + int main() {