public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-8430] libstdc++: Fix std::basic_format_arg::handle for BasicFormatters
Date: Tue, 12 Mar 2024 14:18:02 +0000 (GMT)	[thread overview]
Message-ID: <20240312141802.1F5F4385801E@sourceware.org> (raw)

https://gcc.gnu.org/g:826f7e5ca3bddf3ff82bc52c09e84f5d35b24dbf

commit r13-8430-g826f7e5ca3bddf3ff82bc52c09e84f5d35b24dbf
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Feb 28 15:05:08 2024 +0000

    libstdc++: Fix std::basic_format_arg::handle for BasicFormatters
    
    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<TD, Context> not formattable_with<const TD, Context>,
    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.
    
    (cherry picked from commit 02ca9d3f0c5d2b0255df28f021834dd67ad79bc2)

Diff:
---
 libstdc++-v3/include/std/format                    |  6 +-----
 .../testsuite/std/format/formatter/basic.cc        | 24 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 7bcaddb3715..a938d65a7b9 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -2866,7 +2866,7 @@ namespace __format
 	// Format as const if possible, to reduce instantiations.
 	template<typename _Tp>
 	  using __maybe_const_t
-	    = __conditional_t<__formattable<_Tp>, const _Tp, _Tp>;
+	    = __conditional_t<__formattable<const _Tp>, const _Tp, _Tp>;
 
 	template<typename _Tq>
 	  static void
@@ -2884,10 +2884,6 @@ namespace __format
 	  explicit
 	  handle(_Tp& __val) noexcept
 	  {
-	    if constexpr (!__formattable<const _Tp>)
-	      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<void(*)()>(__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 <format>
+
+struct X { };
+
+template<> struct std::formatter<X, char>
+{
+  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);

                 reply	other threads:[~2024-03-12 14:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240312141802.1F5F4385801E@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).