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: 208455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp572643dyb; Thu, 29 Feb 2024 09:53:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWFKWQYRFw4IJ2CeekG+nYfHZeJMeirxBmiSLZBYeFQhIOT19i4sgejKhAAisk/9Nd+OgrDy1a1Wal97z2ir2VOQBK14Q== X-Google-Smtp-Source: AGHT+IGZJYAjP5ttzgtrdTzmefOe2IcBhPsZ9sBsl4yV/25QOyvuZeDB7DqfT4NihS/O+HCaERrV X-Received: by 2002:a05:6808:f13:b0:3c1:b373:d7b5 with SMTP id m19-20020a0568080f1300b003c1b373d7b5mr3273101oiw.3.1709229225591; Thu, 29 Feb 2024 09:53:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709229225; cv=pass; d=google.com; s=arc-20160816; b=NGgsxJYjqz/hiVRQf/OHanp5t4eoc4WhYy36xc7tnn4ioANmcOdCd3O3wgNOy+6jUK xK+r8ONzzmB6dY6hl5s75FcQkf0cyamNgeWU870mlQ+jWi+r4mqfAOqYI3WgUzgXUQMT 1msvUJzic9yaQJn9fHSwQAqJd4RbFrWdm0qsMZ4WON6ByTcDUuSbnTrGOnavbZ9Be7Hn shGXir2bwdc5xrvOt02JT8gEW/Ue9M7c+5GJDli06kD+pMgDWAVPYPjk8VlkEJwiQCPD hV6s9+nmyQ6RtWSnXT3Uhqzqbpva5rjb+4P4wef/7TZSEtM2vVsY1Be0vLb21K5HZNCy ZOsA== 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:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=ceFaEhbW3Z/9ASgW4XbiefZE/99PyOn92eGH3pn4fI0=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=UP7LuC/e+XMIHTQRHRmlwSMYSjSoZyIggsO0BYSOZctDjQFRyw4rwFZjNNt7A+8vWE jHzagq5MXMDEZFJ/UA5yjkwM5awTysu0dsZeHZJWTbAJ6ySrsuqgJValpW2I7A3HjuqU vj9gd01uzO67YZ1U6Sq94djJVW6uHeOpxxVW72x5b/8S56vxERzyLJE+Odh0U9B90u8a SAzx0j0dUkeX96+hqYQPg+Izsh/MRQJ8V1zfK4GsSB3c5ydHmS8FlyS2LxxQpk9qGtdH 9qwMJexIDXluVlwqkQFkrKmVIjJqOKHa0kl43wgyTrtmbPCNSwtpB4mfySBFklyWeNSY aHWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q9amqTXM; 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 r9-20020a05622a034900b0042ea1877d5fsi1906085qtw.112.2024.02.29.09.53.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 09:53:45 -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=Q9amqTXM; 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 4260F3858415 for ; Thu, 29 Feb 2024 17:53:45 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792256744401651009 X-GMAIL-MSGID: 1792256744401651009 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);