1 | //===----------------------------------------------------------------------===// |
2 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
3 | // See https://llvm.org/LICENSE.txt for license information. |
4 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
5 | // |
6 | //===----------------------------------------------------------------------===// |
7 | |
8 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
9 | // UNSUPPORTED: no-localization |
10 | |
11 | // TODO FMT This test should not require std::to_chars(floating-point) |
12 | // XFAIL: availability-fp_to_chars-missing |
13 | |
14 | // Basic test to validate ill-formed code is properly detected. |
15 | |
16 | // <format> |
17 | |
18 | // template<class Out, class... Args> |
19 | // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, |
20 | // const locale& loc, format-string<Args...> fmt, |
21 | // const Args&... args); |
22 | // template<class Out, class... Args> |
23 | // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, |
24 | // const locale& loc, wformat-string<Args...> fmt, |
25 | // const Args&... args); |
26 | |
27 | #include <format> |
28 | #include <locale> |
29 | |
30 | #include "test_macros.h" |
31 | |
32 | extern char* out; |
33 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
34 | extern wchar_t* wout; |
35 | #endif |
36 | |
37 | // clang-format off |
38 | |
39 | void f() { |
40 | std::format_to_n(out, 42, std::locale(), "{" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
41 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
42 | |
43 | std::format_to_n(out, 42, std::locale(), "}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
44 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
45 | |
46 | std::format_to_n(out, 42, std::locale(), "{}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
47 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
48 | |
49 | std::format_to_n(out, 42, std::locale(), "{0}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
50 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
51 | |
52 | std::format_to_n(out, 42, std::locale(), "{:-}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
53 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
54 | |
55 | std::format_to_n(out, 42, std::locale(), "{:#}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
56 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
57 | |
58 | std::format_to_n(out, 42, std::locale(), "{:L}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
59 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
60 | |
61 | std::format_to_n(out, 42, std::locale(), "{0:{0}}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
62 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
63 | |
64 | std::format_to_n(out, 42, std::locale(), "{:.42d}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
65 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
66 | |
67 | std::format_to_n(out, 42, std::locale(), "{:d}" , "Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
68 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
69 | |
70 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
71 | std::format_to_n(wout, 42, std::locale(), L"{" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
72 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
73 | |
74 | std::format_to_n(wout, 42, std::locale(), L"}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
75 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
76 | |
77 | std::format_to_n(wout, 42, std::locale(), L"{}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
78 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
79 | |
80 | std::format_to_n(wout, 42, std::locale(), L"{0}" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
81 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
82 | |
83 | std::format_to_n(wout, 42, std::locale(), L"{:-}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
84 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
85 | |
86 | std::format_to_n(wout, 42, std::locale(), L"{:#}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
87 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
88 | |
89 | std::format_to_n(wout, 42, std::locale(), L"{:L}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
90 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
91 | |
92 | std::format_to_n(wout, 42, std::locale(), L"{0:{0}}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
93 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
94 | |
95 | std::format_to_n(wout, 42, std::locale(), L"{:.42d}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
96 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
97 | |
98 | std::format_to_n(wout, 42, std::locale(), L"{:d}" , L"Forty-two" ); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} |
99 | // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} |
100 | #endif |
101 | } |
102 | |