[libc++][format] Fixes formatting vector<bool>
authorMark de Wever <koraq@xs4all.nl>
Fri, 17 Feb 2023 17:55:48 +0000 (18:55 +0100)
committerMark de Wever <koraq@xs4all.nl>
Sat, 4 Mar 2023 12:59:49 +0000 (13:59 +0100)
Formatting a const qualified vector<bool> fails to work on libc++. This
is due to our non-conforming type for vector<bool>::const_reference. The
type is a __bit_const_reference<vector> instead of a bool. (This is
fixed in ABI v2.)

This fixes this formatter and enables the test written for it.

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D144279

libcxx/include/__bit_reference
libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.tests.h
libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/format.functions.tests.h
libcxx/test/std/utilities/format/format.formattable/concept.formattable.compile.pass.cpp

index 8b498cd..c66dc4a 100644 (file)
@@ -154,6 +154,8 @@ class __bit_const_reference
     friend typename _Cp::__self;
     friend class __bit_iterator<_Cp, true>;
 public:
+    using __container = typename _Cp::__self;
+
     _LIBCPP_INLINE_VISIBILITY
     __bit_const_reference(const __bit_const_reference&) = default;
 
index 489de3a..ecec920 100644 (file)
@@ -399,8 +399,7 @@ template <class CharT, class TestFunction, class ExceptionTest>
 void test_bool(TestFunction check, ExceptionTest check_exception) {
   std::array input{true, true, false};
   test_bool<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
-  // TODO FMT Use std::vector<bool> after it has been implemented.
-  test_bool<CharT>(check, check_exception, std::priority_queue<bool, std::deque<bool>>{input.begin(), input.end()});
+  test_bool<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end()});
   test_bool<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
 }
 
index 32b68b8..4f17a99 100644 (file)
@@ -14,9 +14,7 @@
 #include "test_macros.h"
 
 template <class CharT, class TestFunction, class ExceptionTest>
-void format_tests(TestFunction check, ExceptionTest check_exception) {
-  std::vector input{true, true, false};
-
+void format_test_vector_bool(TestFunction check, ExceptionTest check_exception, auto&& input) {
   check(SV("[true, true, false]"), SV("{}"), input);
 
   // ***** underlying has no format-spec
@@ -113,4 +111,15 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
   check_exception("Argument index out of bounds", SV("{:^^{}::>{}}"), input, 32);
 }
 
+template <class CharT, class TestFunction, class ExceptionTest>
+void format_tests(TestFunction check, ExceptionTest check_exception) {
+  format_test_vector_bool<CharT>(check, check_exception, std::vector{true, true, false});
+
+  // The const_reference shall be a bool.
+  // However libc++ uses a __bit_const_reference<vector> when
+  // _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL is defined.
+  const std::vector input{true, true, false};
+  format_test_vector_bool<CharT>(check, check_exception, input);
+}
+
 #endif // TEST_STD_CONTAINERS_SEQUENCES_VECTOR_BOOL_VECTOR_BOOL_FMT_FORMAT_FUNCTIONS_TESTS_H
index 1dd415a..0e4708e 100644 (file)
@@ -199,6 +199,12 @@ template <class CharT, class Vector>
 void test_P2286_vector_bool() {
   assert_is_formattable<Vector, CharT>();
   assert_is_formattable<typename Vector::reference, CharT>();
+
+  // The const_reference shall be a bool.
+  // However libc++ uses a __bit_const_reference<vector> when
+  // _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL is defined.
+  assert_is_formattable<const Vector&, CharT>();
+  assert_is_formattable<typename Vector::const_reference, CharT>();
 }
 
 // Tests for P2286 Formatting ranges