Skip to content

Commit da08841

Browse files
committed
[libc++][format] Improves diagnostics.
When attempting to format a type that does not have a valid formatter specialization the code is ill-formed and diagnostics are generated. The patch now properly constrains the exposition-only basic_format_arg constructor (LWG3246). This constructor is called by - std::make_format_args. This function has the precondition The type typename Context::template formatter_type<remove_const_t<Ti>> meets the BasicFormatter requirements ([formatter.requirements]) for each Ti in Args. - In libc++ `std::basic_format_string` constructor too. This is not specified by the standard, however the requirements for constructor allow this. Since `basic_format_arg` exposition-only constructor's constraint can't be observed by the user, they can use it to SFINAE their own code. Currently libc++, libstdc++, and MSVC STL consider the code ill-formed. This patch keeps that behaviour. This patch improves the output of these diagnostics: - Reduces the number of errors generated, which reduces the number of lines in the output. - The error gives a more specific diagnotic in multiple cases. For example, the formatter's format function must be const qualified. The diagnostic now mentions this specific issue. Summary of the error messages using the type no_formatter_specialization, which as the name implies has no specialization. std::format (and friends) Before - 163 lines of output with 7 errors - First two diagnostics: - error: static assertion failed due to requirement '__arg != __arg_t::__none': the supplied type is not formattable - note: in instantiation of function template specialization 'std::__format::__create_format_arg<std::format_context, no_formatter_specialization>' requested here After - 25 lines of output with 1 error - First two diagnostics: - error: static assertion failed: The required formatter specialization has not been provided. - note: in instantiation of function template specialization 'std::__format::__diagnose_invalid_formatter<std::format_context, no_formatter_specialization>' requested here std::make_format_args (used by users to call vformat and friends) Before - 70 lines of output with 3 errors - First two diagnostics: - error: static assertion failed due to requirement '__arg != __arg_t::__none': the supplied type is not formattable - note: in instantiation of function template specialization 'std::__format::__create_format_arg<std::format_context, no_formatter_specialization>' requested here After - 25 lines of output with 1 error - First two diagnostics: - error: static assertion failed: The required formatter specialization has not been provided. - note: in instantiation of function template specialization 'std::__format::__diagnose_invalid_formatter<std::format_context, no_formatter_specialization>' requested here Fixes: #100263
1 parent 0751418 commit da08841

File tree

6 files changed

+497
-16
lines changed

6 files changed

+497
-16
lines changed

libcxx/include/__format/format_arg.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class __basic_format_arg_value {
219219
__format_([](basic_format_parse_context<_CharT>& __parse_ctx, _Context& __ctx, const void* __ptr) {
220220
using _Dp = remove_const_t<_Tp>;
221221
using _Qp = conditional_t<__formattable_with<const _Dp, _Context>, const _Dp, _Dp>;
222-
static_assert(__formattable_with<_Qp, _Context>, "Mandated by [format.arg]/10");
222+
static_assert(__formattable_with<_Qp, _Context>, "Mandated by [format.arg]/12");
223223

224224
typename _Context::template formatter_type<_Dp> __f;
225225
__parse_ctx.advance_to(__f.parse(__parse_ctx));
@@ -334,21 +334,16 @@ class _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS basic_format_arg {
334334
private:
335335
using char_type = typename _Context::char_type;
336336

337-
// TODO FMT Implement constrain [format.arg]/4
338-
// Constraints: The template specialization
339-
// typename Context::template formatter_type<T>
340-
// meets the Formatter requirements ([formatter.requirements]). The extent
341-
// to which an implementation determines that the specialization meets the
342-
// Formatter requirements is unspecified, except that as a minimum the
343-
// expression
344-
// typename Context::template formatter_type<T>()
345-
// .format(declval<const T&>(), declval<Context&>())
346-
// shall be well-formed when treated as an unevaluated operand.
347-
348337
public:
349338
__basic_format_arg_value<_Context> __value_;
350339
__format::__arg_t __type_;
351340

341+
// This constructor is used for the exposition only constructor.
342+
// Per [format.arg]/4
343+
// template<class T> explicit basic_format_arg(T& v) noexcept;
344+
// Constraints: T satisfies formattable-with<Context>.
345+
//
346+
// This constraint is implemented in __create_format_arg
352347
_LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(__format::__arg_t __type,
353348
__basic_format_arg_value<_Context> __value) noexcept
354349
: __value_(__value), __type_(__type) {}

libcxx/include/__format/format_arg_store.h

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ consteval __arg_t __determine_arg_t() {
161161
//
162162
// Modeled after template<class T> explicit basic_format_arg(T& v) noexcept;
163163
// [format.arg]/4-6
164-
template <class _Context, class _Tp>
164+
template <class _Context, __formattable_with<_Context> _Tp>
165165
_LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> __create_format_arg(_Tp& __value) noexcept {
166166
using _Dp = remove_const_t<_Tp>;
167167
constexpr __arg_t __arg = __format::__determine_arg_t<_Context, _Dp>();
168168
static_assert(__arg != __arg_t::__none, "the supplied type is not formattable");
169-
static_assert(__formattable_with<_Tp, _Context>);
170169

171170
// Not all types can be used to directly initialize the
172171
// __basic_format_arg_value. First handle all types needing adjustment, the
@@ -205,6 +204,85 @@ _LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> __create_format_arg(_Tp& __valu
205204
return basic_format_arg<_Context>{__arg, __value};
206205
}
207206

207+
// Helper function that issues a diagnostic when __formattable_with<_Tp, _Context> is false.
208+
//
209+
// Since it's quite easy to make a mistake writing a formatter specialization
210+
// this function tries to give a better explanation. This should improve the
211+
// diagnostics when trying to format type that has no properly specialized
212+
// formatter.
213+
template <class _Context, class _Tp>
214+
[[noreturn]] _LIBCPP_HIDE_FROM_ABI constexpr void __diagnose_invalid_formatter() {
215+
using _Formatter = typename _Context::template formatter_type<remove_const_t<_Tp>>;
216+
constexpr bool __is_disabled =
217+
!is_default_constructible_v<_Formatter> && !is_copy_constructible_v<_Formatter> &&
218+
!is_move_constructible_v<_Formatter> && !is_copy_assignable_v<_Formatter> && !is_move_assignable_v<_Formatter>;
219+
constexpr bool __is_semiregular = semiregular<_Formatter>;
220+
221+
constexpr bool __has_parse_function =
222+
requires(_Formatter& __f, basic_format_parse_context<typename _Context::char_type> __pc) {
223+
{ __f.parse(__pc) };
224+
};
225+
constexpr bool __correct_parse_function_return_type =
226+
requires(_Formatter& __f, basic_format_parse_context<typename _Context::char_type> __pc) {
227+
{ __f.parse(__pc) } -> same_as<typename decltype(__pc)::iterator>;
228+
};
229+
230+
constexpr bool __has_format_function = requires(_Formatter& __f, _Tp&& __t, _Context __fc) {
231+
{ __f.format(__t, __fc) };
232+
};
233+
constexpr bool __is_format_function_const_qualified = requires(const _Formatter& __cf, _Tp&& __t, _Context __fc) {
234+
{ __cf.format(__t, __fc) };
235+
};
236+
constexpr bool __correct_format_function_return_type = requires(_Formatter& __f, _Tp&& __t, _Context __fc) {
237+
{ __f.format(__t, __fc)->same_as<typename _Context::iterator> };
238+
};
239+
240+
// The reason these static_asserts are placed in an if-constexpr-chain is to
241+
// only show one error. For example, when the formatter is not specialized it
242+
// would show all static_assert messages. With this chain the compiler only
243+
// evaluates one static_assert.
244+
245+
if constexpr (__is_disabled)
246+
static_assert(false, "The required formatter specialization has not been provided.");
247+
else if constexpr (!__is_semiregular)
248+
static_assert(false, "The required formatter specialization is not semiregular.");
249+
250+
else if constexpr (!__has_parse_function)
251+
static_assert(
252+
false, "The required formatter specialization does not have a parse function taking the proper arguments.");
253+
else if constexpr (!__correct_parse_function_return_type)
254+
static_assert(false, "The required formatter specialization's parse function does not return the required type.");
255+
256+
else if constexpr (!__has_format_function)
257+
static_assert(
258+
false, "The required formatter specialization does not have a format function taking the proper arguments.");
259+
else if constexpr (!__is_format_function_const_qualified)
260+
static_assert(false, "The required formatter specialization's format function is not const qualified.");
261+
else if constexpr (!__correct_format_function_return_type)
262+
static_assert(false, "The required formatter specialization's format function does not return the required type.");
263+
264+
else
265+
// This should not happen; it makes sure the code is ill-formed.
266+
static_assert(false, "The required formatter specialization is not formattable with its context.");
267+
}
268+
269+
// The Psuedo constructor is constrained per [format.arg]/4.
270+
// The only way for a user to call __create_format_arg is from std::make_format_args.
271+
// Per [format.arg.store]/3
272+
// template<class Context = format_context, class... Args>
273+
// format-arg-store<Context, Args...> make_format_args(Args&... fmt_args);
274+
//
275+
// Preconditions: The type typename Context::template formatter_type<remove_const_t<Ti>>
276+
// meets the BasicFormatter requirements ([formatter.requirements]) for each Ti in Args.
277+
//
278+
// This function is only called when the precondition is violated.
279+
// Without this function the compilation would fail since the overload is
280+
// SFINAE'ed away. This function is used to provide better diagnostics.
281+
template <class _Context, class _Tp>
282+
_LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> __create_format_arg(_Tp&) noexcept {
283+
__format::__diagnose_invalid_formatter<_Context, _Tp>();
284+
}
285+
208286
template <class _Context, class... _Args>
209287
_LIBCPP_HIDE_FROM_ABI void
210288
__create_packed_storage(uint64_t& __types, __basic_format_arg_value<_Context>* __values, _Args&... __args) noexcept {

libcxx/include/__format/format_context.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ __format_context_create(_OutIt __out_it, basic_format_args<basic_format_context<
6565
}
6666
# endif
6767

68-
using format_context = basic_format_context<back_insert_iterator<__format::__output_buffer<char>>, char>;
68+
template <class _CharT>
69+
using __format_context _LIBCPP_NODEBUG =
70+
basic_format_context<back_insert_iterator<__format::__output_buffer<_CharT>>, _CharT>;
71+
72+
using format_context = __format_context<char>;
6973
# if _LIBCPP_HAS_WIDE_CHARACTERS
70-
using wformat_context = basic_format_context< back_insert_iterator<__format::__output_buffer<wchar_t>>, wchar_t>;
74+
using wformat_context = __format_context<wchar_t>;
7175
# endif
7276

7377
template <class _OutIt, class _CharT>

libcxx/include/__format/format_functions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,22 @@ class _LIBCPP_TEMPLATE_VIS __compile_time_handle {
9191
}
9292

9393
template <class _Tp>
94+
requires __formattable_with<_Tp, __format_context<_CharT>>
9495
_LIBCPP_HIDE_FROM_ABI constexpr void __enable() {
9596
__parse_ = [](basic_format_parse_context<_CharT>& __ctx) {
9697
formatter<_Tp, _CharT> __f;
9798
__ctx.advance_to(__f.parse(__ctx));
9899
};
99100
}
100101

102+
template <class _Tp>
103+
_LIBCPP_HIDE_FROM_ABI constexpr void __enable() {
104+
// Avoids the throw set in the constructor.
105+
// Otherwise two diagnostics will be issued.
106+
__parse_ = [](basic_format_parse_context<_CharT>&) {};
107+
__format::__diagnose_invalid_formatter<__format_context<_CharT>, _Tp>();
108+
}
109+
101110
// Before calling __parse the proper handler needs to be set with __enable.
102111
// The default handler isn't a core constant expression.
103112
_LIBCPP_HIDE_FROM_ABI constexpr __compile_time_handle()
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// <format>
12+
13+
// template<class Context = format_context, class... Args>
14+
// format-arg-store<Context, Args...> make_format_args(Args&... args);
15+
//
16+
// Preconditions: The type typename Context::template formatter_type<remove_const_t<Ti>>
17+
// meets the BasicFormatter requirements ([formatter.requirements]) for each Ti in Args.
18+
//
19+
// When the precondition is violated libc++ diagnoses the issue with the
20+
// formatter specialization.
21+
22+
#include <format>
23+
24+
#include "test_macros.h"
25+
26+
struct no_formatter_specialization {};
27+
void test_no_formatter_specialization() {
28+
no_formatter_specialization t;
29+
// expected-error@*:* {{static assertion failed: The required formatter specialization has not been provided.}}
30+
(void)std::make_format_args(t);
31+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
32+
// expected-error@*:* {{static assertion failed: The required formatter specialization has not been provided.}}
33+
(void)std::make_wformat_args(t);
34+
#endif
35+
}
36+
37+
struct correct_formatter_specialization {};
38+
template <class CharT>
39+
struct std::formatter<correct_formatter_specialization, CharT> {
40+
template <class ParseContext>
41+
constexpr typename ParseContext::iterator parse(ParseContext&);
42+
43+
template <class FormatContext>
44+
typename FormatContext::iterator format(correct_formatter_specialization&, FormatContext&) const;
45+
};
46+
void test_correct_formatter_specialization() {
47+
correct_formatter_specialization t;
48+
(void)std::make_format_args(t);
49+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
50+
(void)std::make_wformat_args(t);
51+
#endif
52+
}
53+
54+
struct formatter_not_semiregular {};
55+
template <class CharT>
56+
struct std::formatter<formatter_not_semiregular, CharT> {
57+
formatter(int);
58+
59+
template <class ParseContext>
60+
constexpr typename ParseContext::iterator parse(ParseContext&);
61+
62+
template <class FormatContext>
63+
typename FormatContext::iterator format(formatter_not_semiregular&, FormatContext&) const;
64+
};
65+
void test_formatter_not_semiregular() {
66+
formatter_not_semiregular t;
67+
// expected-error@*:* {{static assertion failed: The required formatter specialization is not semiregular.}}
68+
(void)std::make_format_args(t);
69+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
70+
// expected-error@*:* {{static assertion failed: The required formatter specialization is not semiregular.}}
71+
(void)std::make_wformat_args(t);
72+
#endif
73+
}
74+
75+
struct formatter_no_parse_function {};
76+
template <class CharT>
77+
struct std::formatter<formatter_no_parse_function, CharT> {
78+
template <class FormatContext>
79+
typename FormatContext::iterator format(formatter_no_parse_function&, FormatContext&) const;
80+
};
81+
void test_formatter_no_parse_function() {
82+
formatter_no_parse_function t;
83+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}}
84+
(void)std::make_format_args(t);
85+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
86+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}}
87+
(void)std::make_wformat_args(t);
88+
#endif
89+
}
90+
91+
struct parse_function_invalid_arguments {};
92+
template <class CharT>
93+
struct std::formatter<parse_function_invalid_arguments, CharT> {
94+
template <class ParseContext>
95+
constexpr typename ParseContext::iterator parse(ParseContext&, int);
96+
97+
template <class FormatContext>
98+
typename FormatContext::iterator format(parse_function_invalid_arguments&, FormatContext&) const;
99+
};
100+
void test_parse_function_invalid_arguments() {
101+
parse_function_invalid_arguments t;
102+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}}
103+
(void)std::make_format_args(t);
104+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
105+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}}
106+
(void)std::make_wformat_args(t);
107+
#endif
108+
}
109+
110+
struct parse_function_invalid_return_type {};
111+
template <class CharT>
112+
struct std::formatter<parse_function_invalid_return_type, CharT> {
113+
template <class ParseContext>
114+
constexpr int parse(ParseContext&);
115+
116+
template <class FormatContext>
117+
typename FormatContext::iterator format(parse_function_invalid_return_type&, FormatContext&) const;
118+
};
119+
void test_parse_function_invalid_return_type() {
120+
parse_function_invalid_return_type t;
121+
// expected-error@*:* {{static assertion failed: The required formatter specialization's parse function does not return the required type.}}
122+
(void)std::make_format_args(t);
123+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
124+
// expected-error@*:* {{static assertion failed: The required formatter specialization's parse function does not return the required type.}}
125+
(void)std::make_wformat_args(t);
126+
#endif
127+
}
128+
129+
struct no_format_function {};
130+
template <class CharT>
131+
struct std::formatter<no_format_function, CharT> {
132+
template <class ParseContext>
133+
constexpr typename ParseContext::iterator parse(ParseContext&);
134+
};
135+
void test_no_format_function() {
136+
no_format_function t;
137+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}}
138+
(void)std::make_format_args(t);
139+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
140+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}}
141+
(void)std::make_wformat_args(t);
142+
#endif
143+
}
144+
145+
struct format_function_invalid_arguments {};
146+
template <class CharT>
147+
struct std::formatter<format_function_invalid_arguments, CharT> {
148+
template <class ParseContext>
149+
constexpr typename ParseContext::iterator parse(ParseContext&);
150+
151+
template <class FormatContext>
152+
typename FormatContext::iterator format(format_function_invalid_arguments&) const;
153+
};
154+
void test_format_function_invalid_arguments() {
155+
format_function_invalid_arguments t;
156+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}}
157+
(void)std::make_format_args(t);
158+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
159+
// expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}}
160+
(void)std::make_wformat_args(t);
161+
#endif
162+
}
163+
164+
struct format_function_invalid_return_type {};
165+
template <class CharT>
166+
struct std::formatter<format_function_invalid_return_type, CharT> {
167+
template <class ParseContext>
168+
constexpr typename ParseContext::iterator parse(ParseContext&);
169+
170+
template <class FormatContext>
171+
int format(format_function_invalid_return_type&, FormatContext&) const;
172+
};
173+
void test_format_function_invalid_return_type() {
174+
format_function_invalid_return_type t;
175+
// expected-error@*:* {{static assertion failed: The required formatter specialization's format function does not return the required type.}}
176+
(void)std::make_format_args(t);
177+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
178+
// expected-error@*:* {{static assertion failed: The required formatter specialization's format function does not return the required type.}}
179+
(void)std::make_wformat_args(t);
180+
#endif
181+
}
182+
183+
struct format_function_not_const_qualified {};
184+
template <class CharT>
185+
struct std::formatter<format_function_not_const_qualified, CharT> {
186+
template <class ParseContext>
187+
constexpr typename ParseContext::iterator parse(ParseContext&);
188+
189+
template <class FormatContext>
190+
typename FormatContext::iterator format(format_function_not_const_qualified&, FormatContext&);
191+
};
192+
void test_format_function_not_const_qualified() {
193+
format_function_not_const_qualified t;
194+
// expected-error@*:* {{static assertion failed: The required formatter specialization's format function is not const qualified.}}
195+
(void)std::make_format_args(t);
196+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
197+
// expected-error@*:* {{static assertion failed: The required formatter specialization's format function is not const qualified.}}
198+
(void)std::make_wformat_args(t);
199+
#endif
200+
}

0 commit comments

Comments
 (0)