xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/format.verify.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
1aed5ddf8SMark de Wever //===----------------------------------------------------------------------===//
2*6a54dfbfSLouis Dionne //
3aed5ddf8SMark de Wever // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4aed5ddf8SMark de Wever // See https://llvm.org/LICENSE.txt for license information.
5aed5ddf8SMark de Wever // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6aed5ddf8SMark de Wever //
7aed5ddf8SMark de Wever //===----------------------------------------------------------------------===//
8aed5ddf8SMark de Wever 
9aed5ddf8SMark de Wever // UNSUPPORTED: c++03, c++11, c++14, c++17
10aed5ddf8SMark de Wever 
113d334df5SLouis Dionne // TODO FMT This test should not require std::to_chars(floating-point)
12f0fc8c48SLouis Dionne // XFAIL: availability-fp_to_chars-missing
133d334df5SLouis Dionne 
14aed5ddf8SMark de Wever // Basic test to validate ill-formed code is properly detected.
15aed5ddf8SMark de Wever 
16aed5ddf8SMark de Wever // <format>
17aed5ddf8SMark de Wever 
18aed5ddf8SMark de Wever // template<class... Args>
19aed5ddf8SMark de Wever //   string format(format-string<Args...> fmt, const Args&... args);
20aed5ddf8SMark de Wever // template<class... Args>
21aed5ddf8SMark de Wever //   wstring format(wformat-string<Args...> fmt, const Args&... args);
22aed5ddf8SMark de Wever 
23aed5ddf8SMark de Wever #include <format>
24aed5ddf8SMark de Wever 
25aed5ddf8SMark de Wever #include "test_macros.h"
26aed5ddf8SMark de Wever 
27aed5ddf8SMark de Wever // clang-format off
28aed5ddf8SMark de Wever 
29aed5ddf8SMark de Wever void f() {
309c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
31aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
32aed5ddf8SMark de Wever 
339c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
34aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
35aed5ddf8SMark de Wever 
369c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
37aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
38aed5ddf8SMark de Wever 
399c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
40aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
41aed5ddf8SMark de Wever 
429c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{:-}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
43aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
44aed5ddf8SMark de Wever 
459c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{:#}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
46aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
47aed5ddf8SMark de Wever 
489c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{:L}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
49aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
50aed5ddf8SMark de Wever 
519c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{0:{0}}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
52aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
53aed5ddf8SMark de Wever 
549c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{:.42d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
55aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
56aed5ddf8SMark de Wever 
579c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{:d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
58aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
59aed5ddf8SMark de Wever 
60aed5ddf8SMark de Wever #ifndef TEST_HAS_NO_WIDE_CHARACTERS
619c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
62aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
63aed5ddf8SMark de Wever 
649c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
65aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
66aed5ddf8SMark de Wever 
679c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
68aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
69aed5ddf8SMark de Wever 
709c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
71aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
72aed5ddf8SMark de Wever 
739c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{:-}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
74aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
75aed5ddf8SMark de Wever 
769c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{:#}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
77aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
78aed5ddf8SMark de Wever 
799c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{:L}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
80aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
81aed5ddf8SMark de Wever 
829c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{0:{0}}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
83aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
84aed5ddf8SMark de Wever 
859c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{:.42d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
86aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
87aed5ddf8SMark de Wever 
889c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{:d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
89aed5ddf8SMark de Wever   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
90aed5ddf8SMark de Wever #endif
91aed5ddf8SMark de Wever }
92606e2808SMark de Wever 
93606e2808SMark de Wever struct tiny {
94606e2808SMark de Wever   int bit : 1;
95606e2808SMark de Wever };
96606e2808SMark de Wever 
97606e2808SMark de Wever void P2418()
98606e2808SMark de Wever {
99606e2808SMark de Wever   auto t = tiny{};
1009c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format("{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}}
101606e2808SMark de Wever 
102606e2808SMark de Wever #ifndef TEST_HAS_NO_WIDE_CHARACTERS
1039c053e69SMark de Wever   TEST_IGNORE_NODISCARD std::format(L"{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}}
104606e2808SMark de Wever #endif
105606e2808SMark de Wever }
106