From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id B78313858436; Mon, 18 Mar 2024 14:04:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B78313858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710770674; bh=xO1pUIdZ8urr1S4RNvVBGvEkj6KgrgrBOIIJTQUguxQ=; h=From:To:Subject:Date:From; b=Ynsw6d6RG8oZVXfJfPQU3EUA5iaPKlGNmZRhEa/qmIvqHAslnwf6VP81zbheN+3A5 JJT3+VEJJnHM5Q1jXehfhXK2HtdP1UXkSK87KN5TbDx2n1JdwYdL0HtKMFeHpg0THN qtn96lHHPqHw/i5jiYHoaVrmDbb6gJBJh82HjFfY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10235] libstdc++: Use rvalues in std::string::resize_and_overwrite (LWG 3645) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: fe1c4d6cce6a35c0800f26d87c411a34cb9472bb X-Git-Newrev: d063387b783f49f0d7868259424e7e548ad1e3c2 Message-Id: <20240318140434.B78313858436@sourceware.org> Date: Mon, 18 Mar 2024 14:04:34 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d063387b783f49f0d7868259424e7e548ad1e3c2 commit r12-10235-gd063387b783f49f0d7868259424e7e548ad1e3c2 Author: Jonathan Wakely Date: Wed Mar 22 11:54:31 2023 +0000 libstdc++: Use rvalues in std::string::resize_and_overwrite (LWG 3645) 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. (cherry picked from commit ba4f5530c475eadd2a7edb46b6c1c9d5f9267501) Diff: --- 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 24465dab102..f3911611aea 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -594,9 +594,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); + _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 ); - static_assert( std::is_same_v ); + 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 ); + static_assert( std::is_same_v ); return 0; }); }