From patchwork Thu Feb 29 17:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 86604 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 9FE3A3858C3A for ; Thu, 29 Feb 2024 17:53:28 +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 ESMTPS id CDBF73858C52 for ; Thu, 29 Feb 2024 17:51:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDBF73858C52 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 CDBF73858C52 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=1709229112; cv=none; b=fmgSFEB27e538MV1Twjzk7W0XoKaNaT1bd4aGN7QTkufWxpkvtEOTWAumgsyYdaKq8vW7NmQzlPg5zd078TWPhbSDZxRa8qjwifVwNT2wHOftzYKzPMlPRgIY0MvIwSyMoxHB9IZ6ZnWH0X71WcoK3Fyk55KFQ7akCQE1EbX5Rw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709229112; c=relaxed/simple; bh=rOLIpedvjOJ8+Ih7YPsiKtf9PnwKiUP3CO5CiUVaco4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Fj9cWgCqJjmXii6KOqgldN7wbgsIqdt35f+MX5i4UzT1pJbhUA4j9tkrYbd/EPGMm+2aLV94OKjqs/Kesav6rh2zeQ6tv8lvOFW/TjMdXvErH565fcYr2VyzkgeX//mRIFjmsnmXj8w9V2vEItCiv2z0vHzR7ZHejdjSIdTkpow= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709229109; 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=ceFaEhbW3Z/9ASgW4XbiefZE/99PyOn92eGH3pn4fI0=; b=Q9amqTXMw3AjHN5IF6jOQlMxkOKn2W/JqLCtQdgnJb5eHM0U9cC/ra+1sKHq2q1xSTbkLV BtdDpEnaMi/m5f5LeD9rMd7PbV76FBDrDvkOKYGtmy87A8MjPDOpHbl6N2uJ7btP+BD1su nCTEVXwj2ny2oCd6ocXyPqoeGY+PIm0= 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-186-nIZ9JhdvPoOgexl-q3ckgw-1; Thu, 29 Feb 2024 12:51:47 -0500 X-MC-Unique: nIZ9JhdvPoOgexl-q3ckgw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 865233814E93; Thu, 29 Feb 2024 17:51:47 +0000 (UTC) Received: from localhost (unknown [10.42.28.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5409F492BE2; Thu, 29 Feb 2024 17:51:47 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::basic_format_arg::handle for BasicFormatters Date: Thu, 29 Feb 2024 17:51:34 +0000 Message-ID: <20240229175146.26977-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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+patchwork=sourceware.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- std::basic_format_arg::handle is supposed to format its value as const if that is valid, to reduce the number of instantiations of the formatter's format function. I made a silly typo so that it checks formattable_with not formattable_with, which breaks support for BasicFormatters i.e. ones that can only format non-const types. There's a static_assert in the handle constructor which is supposed to improve diagnostics for trying to format a const argument with a formatter that doesn't support it. That condition can't fail, because the std::basic_format_arg constructor is already constrained to check that the argument type is formattable. The static_assert can be removed. libstdc++-v3/ChangeLog: * include/std/format (basic_format_arg::handle::__maybe_const_t): Fix condition to check if const type is formattable. (basic_format_arg::handle::handle(T&)): Remove redundant static_assert. * testsuite/std/format/formatter/basic.cc: New test. --- libstdc++-v3/include/std/format | 6 +---- .../testsuite/std/format/formatter/basic.cc | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/format/formatter/basic.cc diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 961441e355b..ee189f9086c 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3210,7 +3210,7 @@ namespace __format // Format as const if possible, to reduce instantiations. template using __maybe_const_t - = __conditional_t<__formattable<_Tp>, const _Tp, _Tp>; + = __conditional_t<__formattable, const _Tp, _Tp>; template static void @@ -3228,10 +3228,6 @@ namespace __format explicit handle(_Tp& __val) noexcept { - if constexpr (!__formattable) - static_assert(!is_const_v<_Tp>, "std::format argument must be " - "non-const for this type"); - this->_M_ptr = __builtin_addressof(__val); auto __func = _S_format<__maybe_const_t<_Tp>>; this->_M_func = reinterpret_cast(__func); diff --git a/libstdc++-v3/testsuite/std/format/formatter/basic.cc b/libstdc++-v3/testsuite/std/format/formatter/basic.cc new file mode 100644 index 00000000000..56c18864135 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/formatter/basic.cc @@ -0,0 +1,24 @@ +// { dg-do compile { target c++20 } } + +// BasicFormatter requirements do not require a const parameter. + +#include + +struct X { }; + +template<> struct std::formatter +{ + constexpr auto parse(format_parse_context& ctx) + { return ctx.begin(); } + + // Takes non-const X& + format_context::iterator format(X&, format_context& ctx) const + { + auto out = ctx.out(); + *out++ = 'x'; + return out; + } +}; + +X x; +auto s = std::format("{}", x);