From patchwork Wed Mar 22 17:49:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 66763 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 46BC13848434 for ; Wed, 22 Mar 2023 17:51:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46BC13848434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679507461; bh=7vPDhs2ZtYHfJiBhaYsWoEuu7Ib6OYDXgRjddxoby4w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=swleeDcwFA0T5Ooxaw4h0UN7NHtVXsY7HabSX/xsigGChkPtE4YBWhsTiFEE1Tbx1 UGvtd3I1+EqQ70Jn0Awiuh1iVg4IDsKJx6ccBYZfce3IodRHiuU8I91oRSpfWArAI3 Qh1spS5fFVccp1JVRFT4A7rck0d962In1gAKAw44= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9A156384F017 for ; Wed, 22 Mar 2023 17:50:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A156384F017 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-22-AT__tL5LNDKfqXahm8VDPg-1; Wed, 22 Mar 2023 13:49:59 -0400 X-MC-Unique: AT__tL5LNDKfqXahm8VDPg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3806E81B544; Wed, 22 Mar 2023 17:49:59 +0000 (UTC) Received: from localhost (unknown [10.33.36.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id F1A5F2166B2A; Wed, 22 Mar 2023 17:49:58 +0000 (UTC) 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 Message-Id: <20230322174958.407987-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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); + _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; }); }