public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r11-10133] libstdc++: Make ranges::size and ranges::empty check for unbounded arrays
Date: Thu,  7 Jul 2022 23:33:31 +0000 (GMT)	[thread overview]
Message-ID: <20220707233331.1266B384B0F7@sourceware.org> (raw)

https://gcc.gnu.org/g:c8f091982c748a8a180e9e2b24740f9a388f63da

commit r11-10133-gc8f091982c748a8a180e9e2b24740f9a388f63da
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sun Dec 12 21:16:25 2021 +0000

    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.
    
    (cherry picked from commit 55823c5a0ba50022d7fcc95e74ec293143810ef6)

Diff:
---
 libstdc++-v3/include/bits/ranges_base.h           |  4 ++++
 libstdc++-v3/testsuite/std/ranges/access/empty.cc | 10 ++++++++++
 libstdc++-v3/testsuite/std/ranges/access/size.cc  | 10 ++++++++++
 3 files changed, 24 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
index 6d2d5bf939d..2a525e421b9 100644
--- a/libstdc++-v3/include/bits/ranges_base.h
+++ b/libstdc++-v3/include/bits/ranges_base.h
@@ -379,6 +379,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))>;
@@ -462,6 +464,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));
diff --git a/libstdc++-v3/testsuite/std/ranges/access/empty.cc b/libstdc++-v3/testsuite/std/ranges/access/empty.cc
index b2d8b105325..693355c6cf5 100644
--- a/libstdc++-v3/testsuite/std/ranges/access/empty.cc
+++ b/libstdc++-v3/testsuite/std/ranges/access/empty.cc
@@ -119,6 +119,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()
 {
diff --git a/libstdc++-v3/testsuite/std/ranges/access/size.cc b/libstdc++-v3/testsuite/std/ranges/access/size.cc
index f25a1cb9ddb..db843cddccb 100644
--- a/libstdc++-v3/testsuite/std/ranges/access/size.cc
+++ b/libstdc++-v3/testsuite/std/ranges/access/size.cc
@@ -120,6 +120,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()
 {


                 reply	other threads:[~2022-07-07 23:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220707233331.1266B384B0F7@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).