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 r14-9644] libstdc++: Disable std::formatter specializations (LWG 3944)
Date: Sat, 23 Mar 2024 11:09:20 +0000 (GMT)	[thread overview]
Message-ID: <20240323110920.A5C2A3858D37@sourceware.org> (raw)

https://gcc.gnu.org/g:543585046d17add37c0108b06d2884d0e03cedde

commit r14-9644-g543585046d17add37c0108b06d2884d0e03cedde
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Mar 21 11:15:06 2024 +0000

    libstdc++: Disable std::formatter specializations (LWG 3944)
    
    This was just approved in Tokyo as a DR for C++23. It doesn't affect us
    yet, because we don't implement the __cpp_lib_format_ranges features. We
    can add the disabled specializations and add a testcase now though.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/format (formatter): Disable specializations that
            would allow sequences of narrow characters to be formatted as
            wchar_t without conversion, as per LWG 3944.
            * testsuite/std/format/formatter/lwg3944.cc: New test.

Diff:
---
 libstdc++-v3/include/std/format                    | 23 ++++++++++++++++
 .../testsuite/std/format/formatter/lwg3944.cc      | 31 ++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 613016d1a10..22dcb5f24bd 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -2478,6 +2478,29 @@ namespace __format
     };
   /// @}
 
+#if defined _GLIBCXX_USE_WCHAR_T && __cpp_lib_format_ranges
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 3944. Formatters converting sequences of char to sequences of wchar_t
+
+  namespace __format { struct __disabled; }
+
+  // std::formatter<__disabled, C> uses the primary template, which is disabled.
+  template<>
+    struct formatter<char*, wchar_t>
+    : private formatter<__format::__disabled, wchar_t> { };
+  template<>
+    struct formatter<const char*, wchar_t>
+    : private formatter<__format::__disabled, wchar_t> { };
+  template<size_t _Nm>
+    struct formatter<char[_Nm], wchar_t>
+    : private formatter<__format::__disabled, wchar_t> { };
+  template<class _Traits, class _Allocator>
+    struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
+    : private formatter<__format::__disabled, wchar_t> { };
+  template<class _Traits>
+    struct formatter<basic_string_view<char, _Traits>, wchar_t>
+    : private formatter<__format::__disabled, wchar_t> { };
+#endif
 
 /// @cond undocumented
 namespace __format
diff --git a/libstdc++-v3/testsuite/std/format/formatter/lwg3944.cc b/libstdc++-v3/testsuite/std/format/formatter/lwg3944.cc
new file mode 100644
index 00000000000..ff5f075bcc8
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/format/formatter/lwg3944.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++20 } }
+// { dg-options "-Wno-unused-result" }
+
+// LWG 3944. Formatters converting sequences of char to sequences of wchar_t
+
+#include <format>
+
+void test_lwg3944()
+{
+  // Ill-formed in C++20 and C++23
+  const char* cstr = "hello";
+  char* str = const_cast<char*>(cstr);
+  std::format(L"{}", str); // { dg-error "here" }
+  std::format(L"{}",cstr); // { dg-error "here" }
+
+  // Ill-formed in C++20
+  // In C++23 they give L"['h', 'e', 'l', 'l', 'o']"
+  std::format(L"{}", "hello"); // { dg-error "here" }
+  std::format(L"{}", std::string_view("hello")); // { dg-error "here" }
+  std::format(L"{}", std::string("hello")); // { dg-error "here" }
+#ifdef __cpp_lib_format_ranges
+  // LWG 3944 does not change this, it's still valid.
+  std::format(L"{}", std::vector{'h', 'e', 'l', 'l', 'o'});
+#endif
+}
+
+// { dg-error "std::formatter must be specialized" "" { target *-*-* } 0 }
+// { dg-prune-output "use of deleted function" }
+// { dg-prune-output "no matching function" }
+// { dg-prune-output "has no member named 'parse'" }
+// { dg-prune-output "not a constant expression" }

                 reply	other threads:[~2024-03-23 11:09 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=20240323110920.A5C2A3858D37@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).