]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Check string value_type in std::make_format_args [PR112607]
authorJonathan Wakely <jwakely@redhat.com>
Sat, 18 Nov 2023 20:56:35 +0000 (20:56 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 18 Nov 2023 21:42:33 +0000 (21:42 +0000)
libstdc++-v3/ChangeLog:

PR libstdc++/112607
* include/std/format (basic_format_arg::_S_to_arg_type): Check
value_type for basic_string_view and basic_string
specializations.
* testsuite/std/format/arguments/112607.cc: New test.

libstdc++-v3/include/std/format
libstdc++-v3/testsuite/std/format/arguments/112607.cc [new file with mode: 0644]

index 7c52cce5dbb6ae7d14c6dba4344126203a4dc948..58cd310db4df3946674a26b77ed29d362e075fe8 100644 (file)
@@ -3220,10 +3220,14 @@ namespace __format
            return type_identity<__format::__float128_t>();
 # endif
 #endif
-         else if constexpr (__is_specialization_of<_Td, basic_string_view>)
-           return type_identity<basic_string_view<_CharT>>();
-         else if constexpr (__is_specialization_of<_Td, basic_string>)
-           return type_identity<basic_string_view<_CharT>>();
+         else if constexpr (__is_specialization_of<_Td, basic_string_view>
+                           || __is_specialization_of<_Td, basic_string>)
+           {
+             if constexpr (is_same_v<typename _Td::value_type, _CharT>)
+               return type_identity<basic_string_view<_CharT>>();
+             else
+               return type_identity<handle>();
+           }
          else if constexpr (is_same_v<decay_t<_Td>, const _CharT*>)
            return type_identity<const _CharT*>();
          else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
diff --git a/libstdc++-v3/testsuite/std/format/arguments/112607.cc b/libstdc++-v3/testsuite/std/format/arguments/112607.cc
new file mode 100644 (file)
index 0000000..19eec76
--- /dev/null
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++20 } }
+
+// PR libstdc++/112607
+// _Normalize does not consider char_type for the basic_string_view case
+
+#include <format>
+
+template<typename T>
+struct Alloc
+{
+  using value_type = T;
+  Alloc() = default;
+  template<typename U>
+    Alloc(const Alloc<U>&) { }
+  T* allocate(std::size_t);
+  void deallocate(T*, std::size_t);
+  bool operator==(const Alloc&) const;
+};
+
+template<typename C>
+using String = std::basic_string<C, std::char_traits<C>, Alloc<C>>;
+
+template<>
+struct std::formatter<String<wchar_t>> : std::formatter<std::string> {
+  auto format(const String<wchar_t>&, auto& ctx) const {
+    return std::formatter<std::string>::format(" ", ctx);
+  }
+};
+
+std::string str = std::format("{}", String<wchar_t>{});