public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Use rvalues in std::string::resize_and_overwrite (LWG 3645)
Date: Wed, 22 Mar 2023 17:49:58 +0000	[thread overview]
Message-ID: <20230322174958.407987-1-jwakely@redhat.com> (raw)

Tested powerpc64le-linux, pushed to trunk.

-- >8--

Previously the C++23 draft required that the callback arguments were
lvalues, which was overvable by the callback. LWG 3645 removes that
overspecification, so we can pass rvalues and the user can't modify
our local variables. I've used auto(p) to produce rvalues, which is only
supported since Clang 15, but I think that's OK for a C++23 feature.

While making this change I noticed that we weren't correctly enforcing
the requirement that the callback returns an integer-like type. Add
better assertions for the type and value.

libstdc++-v3/ChangeLog:

	* include/bits/basic_string.tcc (basic_string::resize_and_overwrite):
	Pass rvalues to the callback, as now allowed by LWG 3645.
	Enforce preconditions on the return value.
	* testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc:
	Adjust.
---
 libstdc++-v3/include/bits/basic_string.tcc               | 9 ++++++---
 .../basic_string/capacity/char/resize_and_overwrite.cc   | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index cfbc78a3108..99fdbeee5ad 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -592,9 +592,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	size_type _M_r;
       };
       _Terminator __term{this};
-      const size_type __n2 [[maybe_unused]] = __n;
-      __term._M_r = std::move(__op)(__p, __n);
-      _GLIBCXX_DEBUG_ASSERT(__term._M_r >= 0 && __term._M_r <= __n2);
+      auto __r = std::move(__op)(auto(__p), auto(__n));
+      static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
+      _GLIBCXX_DEBUG_ASSERT(__r >= 0 && __r <= __n);
+      __term._M_r = size_type(__r);
+      if (__term._M_r > __n)
+	__builtin_unreachable();
     }
 #endif // C++23
 
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc
index a336b55f4a1..f716030dad7 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc
@@ -84,9 +84,11 @@ test03()
   VERIFY( s == std::string(42, 'a') );
   VERIFY( s[42] == '\0' );
 
-  s.resize_and_overwrite(0, [](auto&& p, auto&& n) {
-    static_assert( std::is_same_v<decltype(p), char*&> );
-    static_assert( std::is_same_v<decltype(n), std::string::size_type&> );
+  s.resize_and_overwrite(0, [](auto p, auto n) {
+    // N.B. these requirements were relaxed by LWG 3645:
+    // resize_and_overwrite is overspecified to call its callback with lvalues
+    static_assert( std::is_same_v<decltype(p), char*> );
+    static_assert( std::is_same_v<decltype(n), std::string::size_type> );
     return 0;
   });
 }
-- 
2.39.2


                 reply	other threads:[~2023-03-22 17:50 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=20230322174958.407987-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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).