From patchwork Thu Jan 11 20:23:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 187496 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1733103dyi; Thu, 11 Jan 2024 13:27:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEbn3vYohuOMXca39/erDhjOnrlGnhSGq/AzVQ4i3rt5j2jtA/ZE58CrgXcB4RgEd9NLvLT X-Received: by 2002:a05:6122:4f0d:b0:4b6:f254:b220 with SMTP id gh13-20020a0561224f0d00b004b6f254b220mr416532vkb.19.1705008469822; Thu, 11 Jan 2024 13:27:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705008469; cv=pass; d=google.com; s=arc-20160816; b=pm++hzE4Qa5M2N7FNzs+yWRO8ZwIhwHANTT7MSV+qJ7wkQXfOVOGKrhT65DQL9Ol2/ AHkAKun5uxWfCigXlmc394K1Rpq6NLZKKoL1C9ht5cpwWN1+Zqn/vQclgvtHmaoxZ/KI Sv1OHu5UNSGe7x+u2Za/XHkXfwKq5LFojVVcx5uX0ZijZaXYDlhprW7HwEq+EsaO08vz JDRkIFdXOPR238tk3lF9lyaKdza32WlkWa3AA99r2pJ7OyrpzEy0rNSXeXuH3J4IUnxc b3Wyf2DE6PAJsq+poTHXe5SSkfb2aYMMUZ/QRD7TmwiG3XG3HeMG6NOgNO2jSYK2jLJm BJ6Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=I/VywpqGLFbaKwESz+admsnu678qxX1Q5kOwPmEYFmM=; fh=VEJ4CKB+UWG+7ydSRCgMNa2LCx0JHa3hjhBsZd9ubS8=; b=WpSpBjpnA4sA1yN59y2BHlNrv/KVHvASJBQ2+y3WClQBU1BJQHGYqb3yP6AhgmmwXa o1rGvQC9QZ+89FuT+ytjYUvrLj0R5OmkYC1N74iyKgp0s+6+o1oH0W23808y75R7ylyY BSw1pOwApnPW9C95oMo9WxWR8w32kPNDI3DEGoEqkoUnwHoduzb9B6HoDi6DmEvr2eLr K3FTgy4gmFF/pp/FMPV49gL7KiZlLFyIL1lO6py3R3ECU1h1Eo5gzaSuWdJ/OTlxwZeN 6PeZ5JT9GhIMVtuWkqU5c7u77UEokuo7cfibHYIJXOt7BVXNoOyz7Lzr5UJY99r7AP8T 0dhA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HP62KP3n; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id x12-20020a05620a098c00b007830ce6519fsi1653739qkx.649.2024.01.11.13.27.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 13:27:49 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HP62KP3n; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE62F386EC0D for ; Thu, 11 Jan 2024 20:24:41 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 88FC93857C50 for ; Thu, 11 Jan 2024 20:23:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88FC93857C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 88FC93857C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705004637; cv=none; b=ctMHoJxbCC8aSyzvOADxNp3siAg82tydygjnhbB9gYiGVtGrfKG+KNrAD5owjf+N6WMVIT5/Kcm0YF+YK5QZWp9K4RGQn3YDNGj4+wEWA8WE90IKkGgEFLIDIzlc9BDcdkbsCxkg2zkzYrRgsNG+1AfZ29CbBRHSLa4bf7CreEY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705004637; c=relaxed/simple; bh=NsEK6EXTSGaKhDnCiXUHLMYfYLHEA/pMZ/m01GF9MVw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AtdCRntL1TeD6BTtGBFaPUWyiU6CMQiucFHiuxIfujrtXL2LiAWTxCDpEtFK+pXAI5cgW3yUo2EGuuk96sY9SVrLzFjkmrJsNbq4a+z6XOgLvNQGRJZf9ABD0Osb15i1QpGrdbpcFwdCgAUAygkxlQjsk+U9bMIu8RI/AuA5Gfk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705004635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I/VywpqGLFbaKwESz+admsnu678qxX1Q5kOwPmEYFmM=; b=HP62KP3nnh/4vSuUSoIoll7vZw03wqIuHjjuF2QNFVldvWKVyDr4O8pyCgttLrJhqawzFI LSw/lsOo1qrJcZKjmmMbH/yqQv3V6141h3LWYj46yEbZBulqOoPDwIQKWGUh2fqI1lxkDN 2GWpDEHjJtVkucQx09BCYPDB4ZqgGMo= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-219-Yw0BCmRbPZad_cPj819kHQ-1; Thu, 11 Jan 2024 15:23:53 -0500 X-MC-Unique: Yw0BCmRbPZad_cPj819kHQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 015012932492; Thu, 11 Jan 2024 20:23:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF90E40C6EB9; Thu, 11 Jan 2024 20:23:52 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: =?utf-8?q?Daniel_Kr=C3=BCgler?= Subject: [PATCH] libstdc++: Fix std::runtime_format deviations from the spec [PR113320] Date: Thu, 11 Jan 2024 20:23:07 +0000 Message-ID: <20240111202352.513513-1-jwakely@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787830961075395901 X-GMAIL-MSGID: 1787830961075395901 Tested x86_64-linux. Does this look better now? It also fixes PR113320 that got reported. -- >8 -- I seem to have implemented this feature based on the P2918R0 revision, not the final P2918R2 one that was approved for C++26. This commit fixes it. The runtime-format-string type should not have a publicly accessible data member, so add a constructor and make it a friend of basic_format_string. It should also be non-copyable, so that it can only be constructed from a prvalue via temporary materialization. Change the basic_format_string constructor parameter to pass by value. Also add noexcept to the constructors and runtime_format generator functions. libstdc++-v3/ChangeLog: PR libstdc++/113320 * include/std/format (__format::_Runtime_format_string): Add constructor and disable copy operations. (basic_format_string(_Runtime_format_string)): Add noexcept and take parameter by value not rvalue reference. (runtime_format): Add noexcept. * testsuite/std/format/runtime_format.cc: Check noexcept. Check that construction is only possible from prvalues, not xvalues. --- libstdc++-v3/include/std/format | 37 ++++++++++++------- .../testsuite/std/format/runtime_format.cc | 11 ++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index b3b5a0bbdbc..540f8b805f8 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -70,6 +70,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // [format.context], class template basic_format_context template class basic_format_context; + // [format.fmt.string], class template basic_format_string + template struct basic_format_string; + /// @cond undocumented namespace __format { @@ -83,7 +86,20 @@ namespace __format using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>; template - struct _Runtime_format_string { basic_string_view<_CharT> _M_str; }; + struct _Runtime_format_string + { + [[__gnu__::__always_inline__]] + _Runtime_format_string(basic_string_view<_CharT> __s) noexcept + : _M_str(__s) { } + + _Runtime_format_string(const _Runtime_format_string&) = delete; + void operator=(const _Runtime_format_string&) = delete; + + private: + basic_string_view<_CharT> _M_str; + + template friend struct std::basic_format_string; + }; } // namespace __format /// @endcond @@ -104,8 +120,6 @@ namespace __format template class basic_format_arg; - // [format.fmt.string], class template basic_format_string - /** A compile-time checked format string for the specified argument types. * * @since C++23 but available as an extension in C++20. @@ -119,7 +133,7 @@ namespace __format basic_format_string(const _Tp& __s); [[__gnu__::__always_inline__]] - basic_format_string(__format::_Runtime_format_string<_CharT>&& __s) + basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept : _M_str(__s._M_str) { } @@ -144,14 +158,14 @@ namespace __format #if __cplusplus > 202302L [[__gnu__::__always_inline__]] inline __format::_Runtime_format_string - runtime_format(string_view __fmt) - { return {__fmt}; } + runtime_format(string_view __fmt) noexcept + { return __fmt; } #ifdef _GLIBCXX_USE_WCHAR_T [[__gnu__::__always_inline__]] inline __format::_Runtime_format_string - runtime_format(wstring_view __fmt) - { return {__fmt}; } + runtime_format(wstring_view __fmt) noexcept + { return __fmt; } #endif #endif // C++26 @@ -3652,12 +3666,9 @@ namespace __format friend std::basic_format_args<_Context>; template - friend auto + friend auto std:: #if _GLIBCXX_INLINE_VERSION - // Needed for PR c++/59526 - std::__8:: -#else - std:: + __8:: // Needed for PR c++/59256 #endif make_format_args(_Argz&...) noexcept; diff --git a/libstdc++-v3/testsuite/std/format/runtime_format.cc b/libstdc++-v3/testsuite/std/format/runtime_format.cc index 174334c7676..f2bfa5b434d 100644 --- a/libstdc++-v3/testsuite/std/format/runtime_format.cc +++ b/libstdc++-v3/testsuite/std/format/runtime_format.cc @@ -29,6 +29,17 @@ test_internal_api() VERIFY( s == "0x315" ); } +static_assert( noexcept(std::format_string<>(std::runtime_format(""))) ); +static_assert( noexcept(std::wformat_string<>(std::runtime_format(L""))) ); +static_assert( noexcept(std::format_string(std::runtime_format(""))) ); +static_assert( noexcept(std::wformat_string(std::runtime_format(L""))) ); +// A format string can be constructed from the result of std::runtime_format +// using copy elision, but cannot be constructed from an xvalue. +static_assert( !std::is_constructible_v, + decltype(std::runtime_format(""))&&> ); +static_assert( !std::is_constructible_v, + decltype(std::runtime_format(L""))&&> ); + int main() { test_char();