Skip to content

Commit 59d896f

Browse files
mordantetstellar
authored andcommitted
[libc++][format] Fixes invalid usage of m type.
The m type in a range formatter may only be used when a pair or a tuple with two elements is used. This was not correctly validated as reported in llvm.org/PR60995. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D145309 (cherry picked from commit 347a65a)
1 parent c7d3410 commit 59d896f

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

libcxx/include/__format/concepts.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ concept formattable = __formattable<_Tp, _CharT>;
6666
// TODO FMT Add a test to validate we fail when using that concept after P2165
6767
// has been implemented.
6868
template <class _Tp>
69-
concept __fmt_pair_like = __is_specialization_v<_Tp, pair> ||
70-
// Use a requires since tuple_size_v may fail to instantiate,
71-
(__is_specialization_v<_Tp, tuple> && requires { tuple_size_v<_Tp> == 2; });
69+
concept __fmt_pair_like =
70+
__is_specialization_v<_Tp, pair> || (__is_specialization_v<_Tp, tuple> && tuple_size_v<_Tp> == 2);
7271

7372
# endif //_LIBCPP_STD_VER > 20
7473
#endif //_LIBCPP_STD_VER > 17

libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.functions.tests.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ void test_tuple_int(TestFunction check, ExceptionTest check_exception) {
12601260
check(SV("__(42), (99)___"), SV("{:_^15n}"), input);
12611261

12621262
// *** type ***
1263-
check(SV("__{(42), (99)}___"), SV("{:_^17m}"), input);
1263+
check_exception("The range-format-spec type m requires two elements for a pair or tuple", SV("{:m}"), input);
12641264
check_exception("The range-format-spec type s requires formatting a character type", SV("{:s}"), input);
12651265
check_exception("The range-format-spec type ?s requires formatting a character type", SV("{:?s}"), input);
12661266

@@ -1366,7 +1366,7 @@ void test_tuple_int_int_int(TestFunction check, ExceptionTest check_exception) {
13661366
check(SV("__(1, 10, 100), (42, 99, 0)___"), SV("{:_^30n}"), input);
13671367

13681368
// *** type ***
1369-
check(SV("__{(1, 10, 100), (42, 99, 0)}___"), SV("{:_^32m}"), input);
1369+
check_exception("The range-format-spec type m requires two elements for a pair or tuple", SV("{:m}"), input);
13701370
check_exception("The range-format-spec type s requires formatting a character type", SV("{:s}"), input);
13711371
check_exception("The range-format-spec type ?s requires formatting a character type", SV("{:?s}"), input);
13721372

libcxx/test/std/utilities/format/format.range/format.range.formatter/format.functions.tests.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ void test_tuple_int(TestFunction check, ExceptionTest check_exception) {
10831083
check(SV("__(42), (99)___"), SV("{:_^15n}"), input);
10841084

10851085
// *** type ***
1086-
check(SV("__{(42), (99)}___"), SV("{:_^17m}"), input);
1086+
check_exception("The range-format-spec type m requires two elements for a pair or tuple", SV("{:m}"), input);
10871087
check_exception("The range-format-spec type s requires formatting a character type", SV("{:s}"), input);
10881088
check_exception("The range-format-spec type ?s requires formatting a character type", SV("{:?s}"), input);
10891089
for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
@@ -1184,7 +1184,7 @@ void test_tuple_int_int_int(TestFunction check, ExceptionTest check_exception) {
11841184
check(SV("__(42, 99, 0), (1, 10, 100)___"), SV("{:_^30n}"), input);
11851185

11861186
// *** type ***
1187-
check(SV("__{(42, 99, 0), (1, 10, 100)}___"), SV("{:_^32m}"), input);
1187+
check_exception("The range-format-spec type m requires two elements for a pair or tuple", SV("{:m}"), input);
11881188
check_exception("The range-format-spec type s requires formatting a character type", SV("{:s}"), input);
11891189
check_exception("The range-format-spec type ?s requires formatting a character type", SV("{:?s}"), input);
11901190
for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))

0 commit comments

Comments
 (0)