From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 2912D3857BB0 for ; Thu, 11 Jan 2024 20:23:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2912D3857BB0 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 2912D3857BB0 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=1705004638; cv=none; b=s7Gkjhe1SgV/bJUXc3ArL+QicxEjjafq8sW8iU7YGKLVKV9HpNrj8O5PRrZEjMq+HSOe1FqgnEgiKg/aegbcR0P40MlsruyTbxbkEW1Eqq5on4z+2CXduw50a4ug1nzW6uq7WFhSweQ+S3/0KjTonB+cX+Q8BiftAqiNiT55Erk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705004638; c=relaxed/simple; bh=NsEK6EXTSGaKhDnCiXUHLMYfYLHEA/pMZ/m01GF9MVw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XfgDbqwwMXPIZ0p1QsGf3wWDL4oYEfm1VAkS8qG6SzGWkk/s5pcb2wEX4HiCcZqPJL2fnSKZirxtaqyPhRN6glZBm9Dc6GX273ROVNYXM8k1FuNcfrIufYN2VygdKnFqPyaWxCGez5zdSljSnvcBVLvUV7f370gkUymvtJKuxpY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705004636; 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=QT+MWuMuHaYVCRFW6GfDuR6wkZicq8brnlgAAHA4fLLiCvg3TZIzppST76anfLBaHHCKHW xXCOGOn5FpqSF1tVve7w7XMM8urSlYMprlkYvLt+gHPz/JxJDNUM6hWybQOYEkErFazI3o qByMraJb4HZQF3buY6wOvaiDVb4yZXs= 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=20Kr=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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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(); -- 2.43.0