From patchwork Sun Sep 22 16:57:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1988349 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hnaBJpW8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XBXPT5pfkz1xsL for ; Mon, 23 Sep 2024 02:58:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 887B4385840D for ; Sun, 22 Sep 2024 16:58:03 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 66FCE3858D37 for ; Sun, 22 Sep 2024 16:57:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66FCE3858D37 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 66FCE3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727024257; cv=none; b=EJCyIB762dO4ZZDRFjeHNTU+QWbqobngrZQAonMZJJv3oa5xAXVhwoVT3FVJqf5V424QfrnOJRrQFc0gFSvKzyehZGU65lWpXKietPOnkMxs49NOrKRPeS7wiwYQHhMkNYVMvwA3ia7a+RW8A2j13/OZd5xbQdjTuU1lSahLHSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727024257; c=relaxed/simple; bh=ALhqgU//bUhJBMWNEtxYhBdeOiLu8kd8DG6AnHO0Qso=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=C+g9gYGfO7dwnDJEef5rVh0U6989IF1SJ6/s0CwzxM4KPptbotY06u7XfeKauDjbEwb3FLvZldY33OaQGnP154xQzEZkWIUJA+5vhZBahChFDIfmjS2fb/tsW5Yc7rvDFbkV9rQ9/J/iVr02fPXAUjQzkq40z2lgucUK6h0cCuw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727024255; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DjnVEfV2j4fZn1+1BMFz0z/b/XopOqepdL4VyQY+NGo=; b=hnaBJpW8rj+iyjZ5/mnwKKvAx7grTjRNDLL+WzJS6B0o0mvTpUfnDa/4E/xoTKyRzwNoOh 1gIHBDyctO133M7ErnvgwCoDsD2ZzW8GFYMul6TLiP9SgcTwZo79QXD2E7gcb9XLS90eTJ QM38UCyQm6h+UKRQkZBPJhO1CoS2Izw= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-192-l-bupimmMcedoMeZQt96Lg-1; Sun, 22 Sep 2024 12:57:32 -0400 X-MC-Unique: l-bupimmMcedoMeZQt96Lg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.17]) (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 mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CF7CC190ECD6; Sun, 22 Sep 2024 16:57:31 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3041719560A3; Sun, 22 Sep 2024 16:57:30 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Disable std::formatter specialization Date: Sun, 22 Sep 2024 17:57:17 +0100 Message-ID: <20240922165729.1437944-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- I noticed that char8_t was missing from the list of types that were prevented from using the std::formatter partial specialization for integer types. That partial specialization was also matching cv-qualified integer types, because std::integral is true. This change simplifies the constraints by introducing a new variable template which is only true for cv-unqualified integer types, with explicit specializations to exclude the character types. This should be slightly more efficient than the previous constraints that checked std::integral and (!__is_one_of). It also avoids the need for a separate std::formatter specialization for 128-bit integers, as they can be handled by the new variable template too. libstdc++-v3/ChangeLog: * include/std/format (__format::__is_formattable_integer): New variable template and specializations. (template struct formatter): Replace constraints on first arg with __is_formattable_integer. * testsuite/std/format/formatter/requirements.cc: Check that std::formatter specializations for char8_t and const int are disabled. --- libstdc++-v3/include/std/format | 95 ++++++++++--------- .../std/format/formatter/requirements.cc | 17 ++++ 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 4c5377aabec..100a53dfd76 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1468,8 +1468,9 @@ namespace __format // We can format a floating-point type iff it is usable with to_chars. template - concept __formattable_float = requires (_Tp __t, char* __p) - { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); }; + concept __formattable_float + = is_same_v, _Tp> && requires (_Tp __t, char* __p) + { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); }; template<__char _CharT> struct __formatter_fp @@ -2184,53 +2185,53 @@ namespace __format #endif // USE_WCHAR_T /// @} - /// Format an integer. - template - requires (!__is_one_of<_Tp, char, wchar_t, char16_t, char32_t>::value) - struct formatter<_Tp, _CharT> - { - formatter() = default; +/// @cond undocumented +namespace __format +{ + // each cv-unqualified arithmetic type ArithmeticT other than + // char, wchar_t, char8_t, char16_t, or char32_t + template + constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value; - [[__gnu__::__always_inline__]] - constexpr typename basic_format_parse_context<_CharT>::iterator - parse(basic_format_parse_context<_CharT>& __pc) - { - return _M_f.template _M_parse<_Tp>(__pc); - } - - template - typename basic_format_context<_Out, _CharT>::iterator - format(_Tp __u, basic_format_context<_Out, _CharT>& __fc) const - { return _M_f.format(__u, __fc); } - - private: - __format::__formatter_int<_CharT> _M_f; - }; - -#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__ - template - requires (__is_one_of<_Tp, __int128, unsigned __int128>::value) - struct formatter<_Tp, _CharT> - { - formatter() = default; - - [[__gnu__::__always_inline__]] - constexpr typename basic_format_parse_context<_CharT>::iterator - parse(basic_format_parse_context<_CharT>& __pc) - { - return _M_f.template _M_parse<_Tp>(__pc); - } - - template - typename basic_format_context<_Out, _CharT>::iterator - format(_Tp __u, basic_format_context<_Out, _CharT>& __fc) const - { return _M_f.format(__u, __fc); } - - private: - __format::__formatter_int<_CharT> _M_f; - }; +#if defined __SIZEOF_INT128__ + template<> inline constexpr bool __is_formattable_integer<__int128> = true; + template<> inline constexpr bool __is_formattable_integer + = true; #endif + template<> inline constexpr bool __is_formattable_integer = false; + template<> inline constexpr bool __is_formattable_integer = false; +#ifdef _GLIBCXX_USE_CHAR8_T + template<> inline constexpr bool __is_formattable_integer = false; +#endif + template<> inline constexpr bool __is_formattable_integer = false; + template<> inline constexpr bool __is_formattable_integer = false; +} +/// ~endcond + + /// Format an integer. + template + requires __format::__is_formattable_integer<_Tp> + struct formatter<_Tp, _CharT> + { + formatter() = default; + + [[__gnu__::__always_inline__]] + constexpr typename basic_format_parse_context<_CharT>::iterator + parse(basic_format_parse_context<_CharT>& __pc) + { + return _M_f.template _M_parse<_Tp>(__pc); + } + + template + typename basic_format_context<_Out, _CharT>::iterator + format(_Tp __u, basic_format_context<_Out, _CharT>& __fc) const + { return _M_f.format(__u, __fc); } + + private: + __format::__formatter_int<_CharT> _M_f; + }; + #if defined __glibcxx_to_chars /// Format a floating-point value. template<__format::__formattable_float _Tp, __format::__char _CharT> @@ -2614,6 +2615,8 @@ namespace __format } // namespace __format /// @endcond +// Concept std::formattable was introduced by P2286R8 "Formatting Ranges", +// but we can't guard it with __cpp_lib_format_ranges until we define that! #if __cplusplus > 202002L // [format.formattable], concept formattable template diff --git a/libstdc++-v3/testsuite/std/format/formatter/requirements.cc b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc index bde67e586ef..416b9a8ede5 100644 --- a/libstdc++-v3/testsuite/std/format/formatter/requirements.cc +++ b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc @@ -26,6 +26,16 @@ test_specializations() // [format.formatter.spec] static_assert( std::is_copy_assignable_v ); static_assert( std::is_move_assignable_v ); +#ifdef _GLIBCXX_USE_CHAR8_T + // std::string s0 = std::format("{}", u8'a'); // error: disabled formatter + using Fc8 = std::format_context::formatter_type; + static_assert( ! std::is_default_constructible_v ); + static_assert( ! std::is_copy_constructible_v ); + static_assert( ! std::is_move_constructible_v ); + static_assert( ! std::is_copy_assignable_v ); + static_assert( ! std::is_move_assignable_v ); +#endif + // std::string s1 = std::format("{}", L"foo"); // error: disabled formatter using Fw = std::format_context::formatter_type; static_assert( ! std::is_default_constructible_v ); @@ -34,6 +44,13 @@ test_specializations() // [format.formatter.spec] static_assert( ! std::is_copy_assignable_v ); static_assert( ! std::is_move_assignable_v ); + using Fic = std::format_context::formatter_type; // disabled + static_assert( ! std::is_default_constructible_v ); + static_assert( ! std::is_copy_constructible_v ); + static_assert( ! std::is_move_constructible_v ); + static_assert( ! std::is_copy_assignable_v ); + static_assert( ! std::is_move_assignable_v ); + std::string s2 = std::format("{}", red); // OK, user-provided formatter VERIFY( s2 == "red" ); using Fc = std::format_context::formatter_type;