Skip to content

Commit 7e4639d

Browse files
committed
[libc++][format] Fixes formatting vector<bool>
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
1 parent faacf8b commit 7e4639d

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

libcxx/include/__bit_reference

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class __bit_const_reference
154154
friend typename _Cp::__self;
155155
friend class __bit_iterator<_Cp, true>;
156156
public:
157+
using __container = typename _Cp::__self;
158+
157159
_LIBCPP_INLINE_VISIBILITY
158160
__bit_const_reference(const __bit_const_reference&) = default;
159161

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.tests.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ template <class CharT, class TestFunction, class ExceptionTest>
399399
void test_bool(TestFunction check, ExceptionTest check_exception) {
400400
std::array input{true, true, false};
401401
test_bool<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
402-
// TODO FMT Use std::vector<bool> after it has been implemented.
403-
test_bool<CharT>(check, check_exception, std::priority_queue<bool, std::deque<bool>>{input.begin(), input.end()});
402+
test_bool<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end()});
404403
test_bool<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
405404
}
406405

libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/format.functions.tests.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include "test_macros.h"
1515

1616
template <class CharT, class TestFunction, class ExceptionTest>
17-
void format_tests(TestFunction check, ExceptionTest check_exception) {
18-
std::vector input{true, true, false};
19-
17+
void format_test_vector_bool(TestFunction check, ExceptionTest check_exception, auto&& input) {
2018
check(SV("[true, true, false]"), SV("{}"), input);
2119

2220
// ***** underlying has no format-spec
@@ -113,4 +111,15 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
113111
check_exception("Argument index out of bounds", SV("{:^^{}::>{}}"), input, 32);
114112
}
115113

114+
template <class CharT, class TestFunction, class ExceptionTest>
115+
void format_tests(TestFunction check, ExceptionTest check_exception) {
116+
format_test_vector_bool<CharT>(check, check_exception, std::vector{true, true, false});
117+
118+
// The const_reference shall be a bool.
119+
// However libc++ uses a __bit_const_reference<vector> when
120+
// _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL is defined.
121+
const std::vector input{true, true, false};
122+
format_test_vector_bool<CharT>(check, check_exception, input);
123+
}
124+
116125
#endif // 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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ template <class CharT, class Vector>
199199
void test_P2286_vector_bool() {
200200
assert_is_formattable<Vector, CharT>();
201201
assert_is_formattable<typename Vector::reference, CharT>();
202+
203+
// The const_reference shall be a bool.
204+
// However libc++ uses a __bit_const_reference<vector> when
205+
// _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL is defined.
206+
assert_is_formattable<const Vector&, CharT>();
207+
assert_is_formattable<typename Vector::const_reference, CharT>();
202208
}
203209

204210
// Tests for P2286 Formatting ranges

0 commit comments

Comments
 (0)