From patchwork Tue Nov 15 14:31:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 60652 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 99EB438AA269 for ; Tue, 15 Nov 2022 14:32:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99EB438AA269 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668522732; bh=74ipC/sN/bWfG1moQZGTqK3uTOhIaXt8oAMstw3J51w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=w7JIBIi1QQ4vM3Y7HAdJUk9SDTMIRYrjE4M1G4eJCha5cHBd8ipiyOULt/fDbUjWc Xs4iXu/E/I7tBzORgNtEG5HjYNCERY+VQ49vAgcsWJp4bUJeNTVpqN/2xJN3vNs/sr ysTBtI5TltkTTB7ystH+MPF/9o0wt0suiHwBQ0ZA= 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 D8CCE3898396 for ; Tue, 15 Nov 2022 14:31:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D8CCE3898396 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-262-NNOpak2aPfmIqF8kX0jeWA-1; Tue, 15 Nov 2022 09:31:37 -0500 X-MC-Unique: NNOpak2aPfmIqF8kX0jeWA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03CFB185A794; Tue, 15 Nov 2022 14:31:37 +0000 (UTC) Received: from localhost (unknown [10.33.36.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD90F40C6EC3; Tue, 15 Nov 2022 14:31:36 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstc++: std::formattable concept should not be defined for C++20 Date: Tue, 15 Nov 2022 14:31:34 +0000 Message-Id: <20221115143134.1155246-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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 x86_64-linux and x86_64-w64-mingw32. Pushed to trunk. -- >8 -- This concept was added by a C++23 proposal, so don't define it for C++20. Split the format/formatter/formatter.cc test into two parts, one that tests the C++20 requirements and one that tests the C++23 concept. libstdc++-v3/ChangeLog: * include/std/format (formattable): Only define for C++23/ * testsuite/std/format/formatter.cc: Moved to... * testsuite/std/format/formatter/requirements.cc: ...here. * testsuite/std/format/formatter/concept.cc: New test. * testsuite/std/format/functions/format.cc: Replace use of std::formattable in C++20. --- libstdc++-v3/include/std/format | 11 +++- .../testsuite/std/format/formatter/concept.cc | 46 ++++++++++++++++ .../requirements.cc} | 54 +++++-------------- .../testsuite/std/format/functions/format.cc | 12 ++++- 4 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/format/formatter/concept.cc rename libstdc++-v3/testsuite/std/format/{formatter.cc => formatter/requirements.cc} (50%) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index c79c8f2ce31..204a1710aca 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -2181,11 +2181,14 @@ namespace __format } // namespace __format /// @endcond +#if __cplusplus > 202002L // [format.formattable], concept formattable template concept formattable = __format::__formattable_impl, _CharT>; +#endif +#if __cpp_lib_format_ranges /// @cond undocumented namespace __format { @@ -2199,6 +2202,7 @@ namespace __format = conditional_t<__const_formattable_range<_Rg, _CharT>, const _Rg, _Rg>; } // namespace __format /// @endcond +#endif // format_ranges /// An iterator after the last character written, and the number of /// characters that would have been written. @@ -3485,16 +3489,19 @@ namespace __format std::visit_format_arg([this](auto& __arg) { using _Type = remove_reference_t; + using _Formatter = typename _Context::template formatter_type<_Type>; if constexpr (is_same_v<_Type, monostate>) __format::__invalid_arg_id_in_format_string(); else if constexpr (is_same_v<_Type, handle>) __arg.format(this->_M_pc, this->_M_fc); - else + else if constexpr (is_default_constructible_v<_Formatter>) { - typename _Context::template formatter_type<_Type> __f; + _Formatter __f; this->_M_pc.advance_to(__f.parse(this->_M_pc)); this->_M_fc.advance_to(__f.format(__arg, this->_M_fc)); } + else + static_assert(__format::__formattable_with<_Type, _Context>); }, _M_fc.arg(__id)); } }; diff --git a/libstdc++-v3/testsuite/std/format/formatter/concept.cc b/libstdc++-v3/testsuite/std/format/formatter/concept.cc new file mode 100644 index 00000000000..fe56dc44a68 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/formatter/concept.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +struct S { }; + +template<> struct std::formatter : std::formatter { + template + auto format(S, std::basic_format_context& ctx) const { + return formatter::format("ess", ctx); + } +}; + +struct T { }; + +template<> struct std::formatter : std::formatter { + // This function only accepts std::format_context, not other contexts. + auto format(T, std::format_context& ctx) const { + return formatter::format("tee", ctx); + } +}; + +struct U { }; + +void +test_concept() // [format.formattable] +{ + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( ! std::formattable ); + static_assert( ! std::formattable ); + static_assert( ! std::formattable ); + static_assert( std::formattable ); + static_assert( std::formattable ); + static_assert( ! std::formattable ); // only formats as char + static_assert( ! std::formattable ); // formatter not generic + static_assert( ! std::formattable ); // no formatter +} diff --git a/libstdc++-v3/testsuite/std/format/formatter.cc b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc similarity index 50% rename from libstdc++-v3/testsuite/std/format/formatter.cc rename to libstdc++-v3/testsuite/std/format/formatter/requirements.cc index 64ff2dbfbfd..3bff8bdbd5d 100644 --- a/libstdc++-v3/testsuite/std/format/formatter.cc +++ b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc @@ -4,48 +4,6 @@ #include #include -struct S { }; - -template<> struct std::formatter : std::formatter { - template - auto format(S, std::basic_format_context& ctx) const { - return formatter::format("ess", ctx); - } -}; - -struct T { }; - -template<> struct std::formatter : std::formatter { - // This function only accepts std::format_context, not other contexts. - auto format(T, std::format_context& ctx) const { - return formatter::format("tee", ctx); - } -}; - -struct U { }; - -void -test_concept() // [format.formattable] -{ - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( ! std::formattable ); - static_assert( ! std::formattable ); - static_assert( ! std::formattable ); - static_assert( std::formattable ); - static_assert( std::formattable ); - static_assert( ! std::formattable ); // only formats as char - static_assert( ! std::formattable ); // formatter not generic - static_assert( ! std::formattable ); // no formatter -} - enum color { red, green, blue }; const char* color_names[] = { "red", "green", "blue" }; @@ -62,6 +20,12 @@ test_specializations() // [format.formatter.spec] { std::string s0 = std::format("{}", 42); // OK, library-provided formatter VERIFY( s0 == "42" ); + using Fi = 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 ); // std::string s1 = std::format("{}", L"foo"); // error: disabled formatter using Fw = std::format_context::formatter_type; @@ -73,6 +37,12 @@ test_specializations() // [format.formatter.spec] std::string s2 = std::format("{}", red); // OK, user-provided formatter VERIFY( s2 == "red" ); + using Fc = 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 ); // std::string s3 = std::format("{}", err{}); // error: disabled formatter using Ferr = std::format_context::formatter_type; diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 165ef41b4b3..c01405eac90 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -297,17 +297,25 @@ bool format_float() return s == "-0. != +0.500 "; } +#if __cplusplus > 202002L +template +concept formattable = std::formattable; +#else +template +concept formattable = requires (T t, char* p) { std::to_chars(p, p, t); }; +#endif + void test_float128() { #ifdef __SIZEOF_FLOAT128__ - if constexpr (std::formattable<__float128, char>) + if constexpr (formattable<__float128>) VERIFY( format_float<__float128>() ); else std::puts("Cannot format __float128 on this target"); #endif #if __FLT128_DIG__ - if constexpr (std::formattable<_Float128, char>) + if constexpr (formattable<_Float128>) VERIFY( format_float<_Float128>() ); else std::puts("Cannot format _Float128 on this target");