[X86] Move getGFNICtrlMask before CTLZ/CTTZ lowering. NFC.
[llvm-project.git] / clang / test / SemaCXX / static-assert.cpp
blob0d384b6b499f78516e14e5718551b98dd86aaa75
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -pedantic -triple=x86_64-linux-gnu -Wno-invalid-utf8
3 int f(); // expected-note {{declared here}}
5 static_assert(f(), "f"); // expected-error {{static assertion expression is not an integral constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
6 static_assert(true, "true is not false");
7 static_assert(false, "false is false"); // expected-error {{static assertion failed: false is false}}
9 void g() {
10 static_assert(false, "false is false"); // expected-error {{static assertion failed: false is false}}
13 class C {
14 static_assert(false, "false is false"); // expected-error {{static assertion failed: false is false}}
17 template<int N> struct T {
18 static_assert(N == 2, "N is not 2!"); // expected-error {{static assertion failed due to requirement '1 == 2': N is not 2!}}
21 T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}}
22 T<2> t2;
24 template<typename T> struct S {
25 static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static assertion failed due to requirement 'sizeof(char) > sizeof(char)': Type not big enough!}} \
26 // expected-note {{1 > 1}}
29 S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}}
30 S<int> s2;
32 static_assert(false, L"\xFFFFFFFF"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect and is incompatible with c++2c}} \
33 // expected-error {{invalid escape sequence '\xFFFFFFFF' in an unevaluated string literal}} \
34 // expected-error {{hex escape sequence out of range}}
35 static_assert(false, u"\U000317FF"); // expected-warning {{encoding prefix 'u' on an unevaluated string literal has no effect and is incompatible with c++2c}} \
36 // expected-error {{static assertion failed}}
38 static_assert(false, u8"Ω"); // expected-warning {{encoding prefix 'u8' on an unevaluated string literal has no effect and is incompatible with c++2c}} \
39 // expected-error {{static assertion failed: Ω}}
40 static_assert(false, L"\u1234"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect and is incompatible with c++2c}} \
41 // expected-error {{static assertion failed: ሴ}}
43 static_assert(false, L"\x1ff" // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect and is incompatible with c++2c}} \
44 // expected-error {{hex escape sequence out of range}} \
45 // expected-error {{invalid escape sequence '\x1ff' in an unevaluated string literal}}
46 "0\x123" // expected-error {{invalid escape sequence '\x123' in an unevaluated string literal}}
47 "fx\xfffff" // expected-error {{invalid escape sequence '\xfffff' in an unevaluated string literal}}
48 "goop");
50 static_assert(false, "\'\"\?\\\a\b\f\n\r\t\v"); // expected-error {{'"?\<U+0007><U+0008>}}
51 static_assert(true, "\xFF"); // expected-error {{invalid escape sequence '\xFF' in an unevaluated string literal}}
52 static_assert(true, "\123"); // expected-error {{invalid escape sequence '\123' in an unevaluated string literal}}
53 static_assert(true, "\pOh no, a Pascal string!"); // expected-warning {{unknown escape sequence '\p'}} \
54 // expected-error {{invalid escape sequence '\p' in an unevaluated string literal}}
55 static_assert(false, R"(a
56 \tb
58 )"); // expected-error@-3 {{static assertion failed: a\n\tb\nc\n}}
60 static_assert(false, "\u0080\u0081\u0082\u0083\u0099\u009A\u009B\u009C\u009D\u009E\u009F");
61 // expected-error@-1 {{static assertion failed: <U+0080><U+0081><U+0082><U+0083><U+0099><U+009A><U+009B><U+009C><U+009D><U+009E><U+009F>}}
63 //! Contains RTL/LTR marks
64 static_assert(false, "\u200Eabc\u200Fdef\u200Fgh"); // expected-error {{static assertion failed: ‎abc‏def‏gh}}
66 //! Contains ZWJ/regional indicators
67 static_assert(false, "🏳️‍🌈 🏴󠁧󠁢󠁥󠁮󠁧󠁿 🇪🇺"); // expected-error {{static assertion failed: 🏳️‍🌈 🏴󠁧󠁢󠁥󠁮󠁧󠁿 🇪🇺}}
69 template<typename T> struct AlwaysFails {
70 // Only give one error here.
71 static_assert(false, ""); // expected-error 2{{static assertion failed}}
73 AlwaysFails<int> alwaysFails; // expected-note {{instantiation}}
74 AlwaysFails<double> alwaysFails2; // expected-note {{instantiation}}
77 template<typename T> struct StaticAssertProtected {
78 static_assert(__is_literal(T), ""); // expected-error {{static assertion failed}}
79 static constexpr T t = {}; // no error here
81 struct X { ~X(); };
82 StaticAssertProtected<int> sap1;
83 StaticAssertProtected<X> sap2; // expected-note {{instantiation}}
85 static_assert(true); // expected-warning {{C++17 extension}}
86 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}
89 // Diagnostics for static_assert with multiple conditions
90 template<typename T> struct first_trait {
91 static const bool value = false;
94 template<>
95 struct first_trait<X> {
96 static const bool value = true;
99 template<typename T> struct second_trait {
100 static const bool value = false;
103 static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static assertion failed due to requirement 'second_trait<X>::value': message}}
105 namespace std {
107 template <class Tp, Tp v>
108 struct integral_constant {
109 static const Tp value = v;
110 typedef Tp value_type;
111 typedef integral_constant type;
112 constexpr operator value_type() const noexcept { return value; }
113 constexpr value_type operator()() const noexcept { return value; }
116 template <class Tp, Tp v>
117 const Tp integral_constant<Tp, v>::value;
119 typedef integral_constant<bool, true> true_type;
120 typedef integral_constant<bool, false> false_type;
122 template <class Tp>
123 struct is_const : public false_type {};
124 template <class Tp>
125 struct is_const<Tp const> : public true_type {};
127 // We do not define is_same in terms of integral_constant to check that both implementations are supported.
128 template <typename T, typename U>
129 struct is_same {
130 static const bool value = false;
133 template <typename T>
134 struct is_same<T, T> {
135 static const bool value = true;
138 } // namespace std
140 struct ExampleTypes {
141 explicit ExampleTypes(int);
142 using T = int;
143 using U = float;
146 static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message");
147 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<int, float>::value': message}}
148 static_assert(std::is_const<ExampleTypes::T>::value, "message");
149 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
150 static_assert(!std::is_const<const ExampleTypes::T>::value, "message");
151 // expected-error@-1{{static assertion failed due to requirement '!std::is_const<const int>::value': message}}
152 static_assert(!(std::is_const<const ExampleTypes::T>::value), "message");
153 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>::value)': message}}
154 static_assert(std::is_const<const ExampleTypes::T>::value == false, "message");
155 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<const int>::value == false': message}}
156 static_assert(!(std::is_const<const ExampleTypes::T>::value == true), "message");
157 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>::value == true)': message}}
158 static_assert(std::is_const<ExampleTypes::T>(), "message");
159 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>()': message}}
160 static_assert(!(std::is_const<const ExampleTypes::T>()()), "message");
161 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>()())': message}}
162 static_assert(std::is_same<decltype(std::is_const<const ExampleTypes::T>()), int>::value, "message");
163 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<std::is_const<const int>, int>::value': message}}
164 static_assert(std::is_const<decltype(ExampleTypes::T(3))>::value, "message");
165 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
166 static_assert(std::is_const<decltype(ExampleTypes::T())>::value, "message");
167 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
168 static_assert(std::is_const<decltype(ExampleTypes(3))>::value, "message");
169 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<ExampleTypes>::value': message}}
171 struct BI_tag {};
172 struct RAI_tag : BI_tag {};
173 struct MyIterator {
174 using tag = BI_tag;
176 struct MyContainer {
177 using iterator = MyIterator;
179 template <class Container>
180 void foo() {
181 static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message");
182 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value': message}}
184 template void foo<MyContainer>();
185 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}}
187 namespace ns {
188 template <typename T, int v>
189 struct NestedTemplates1 {
190 struct NestedTemplates2 {
191 template <typename U>
192 struct NestedTemplates3 : public std::is_same<T, U> {};
195 } // namespace ns
197 template <typename T, typename U, int a>
198 void foo2() {
199 static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message");
200 // expected-error@-1{{static assertion failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::template NestedTemplates3<float>::value': message}}
202 template void foo2<int, float, 3>();
203 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}}
205 template <class T>
206 void foo3(T t) {
207 static_assert(std::is_const<T>::value, "message");
208 // expected-error-re@-1{{static assertion failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value': message}}
209 static_assert(std::is_const<decltype(t)>::value, "message");
210 // expected-error-re@-1{{static assertion failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value': message}}
212 void callFoo3() {
213 foo3([]() {});
214 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }}
217 template <class T>
218 void foo4(T t) {
219 static_assert(std::is_const<typename T::iterator>::value, "message");
220 // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}}
222 void callFoo4() { foo4(42); }
223 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}}
225 static_assert(42, "message");
226 static_assert(42.0, "message"); // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 42 to true}}
227 constexpr int *p = 0;
228 static_assert(p, "message"); // expected-error {{static assertion failed}}
230 struct NotBool {
231 } notBool;
232 constexpr NotBool constexprNotBool;
233 static_assert(notBool, "message"); // expected-error {{value of type 'struct NotBool' is not contextually convertible to 'bool'}}
234 static_assert(constexprNotBool, "message"); // expected-error {{value of type 'const NotBool' is not contextually convertible to 'bool'}}
236 static_assert(1 , "") // expected-error {{expected ';' after 'static_assert'}}
238 namespace DependentAlwaysFalse {
239 template <typename Ty>
240 struct S {
241 static_assert(false); // expected-error{{static assertion failed}} \
242 // expected-warning {{C++17 extension}}
245 template <typename Ty>
246 struct T {
247 static_assert(false, "test"); // expected-error{{static assertion failed: test}}
250 int f() {
251 S<double> s; //expected-note {{in instantiation of template class 'DependentAlwaysFalse::S<double>' requested here}}
252 T<double> t; //expected-note {{in instantiation of template class 'DependentAlwaysFalse::T<double>' requested here}}
256 namespace Diagnostics {
257 /// No notes for literals.
258 static_assert(false, ""); // expected-error {{failed}}
259 static_assert(1.0 > 2.0, ""); // expected-error {{failed}}
260 static_assert('c' == 'd', ""); // expected-error {{failed}}
261 static_assert(1 == 2, ""); // expected-error {{failed}}
263 /// Simple things are ignored.
264 static_assert(1 == (-(1)), ""); //expected-error {{failed}}
266 /// Chars are printed as chars.
267 constexpr char getChar() {
268 return 'c';
270 static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
271 // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
272 static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
273 // expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
274 static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
275 // expected-note {{n' (0x0A, 10) == '<U+0000>' (0x00, 0)'}}
276 // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '<U+0000>' (0x00, 0)'", but if we write it as it is,
277 // the "\n" cannot be consumed by the diagnostic consumer.
278 static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
279 // expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
280 static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
281 // expected-note {{evaluates to ''<FC>' (0xFC, -4) == 248'}}
282 static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
283 // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
284 static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
285 // expected-note {{evaluates to ''<A0>' (0xA0, -96) == ' ' (0x20, 32)'}}
286 static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
287 // expected-note {{evaluates to 'u'ゆ' (0x3086, 12422) == L'̵' (0x335, 821)'}}
288 static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
289 // expected-note {{evaluates to 'L'/' (0xFF0F, 65295) == u'�' (0xFFFD, 65533)'}}
290 static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
291 // expected-note {{evaluates to 'L'⚾' (0x26BE, 9918) == U'🌍' (0x1F30D, 127757)'}}
292 static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
293 // expected-note {{evaluates to 'U'\a' (0x07, 7) == L'\t' (0x09, 9)'}}
294 static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
295 // expected-note {{evaluates to 'L'§' (0xA7, 167) == U'Ö' (0xD6, 214)'}}
297 /// Bools are printed as bools.
298 constexpr bool invert(bool b) {
299 return !b;
302 static_assert(invert(true) || invert(true), ""); // expected-error {{static assertion failed due to requirement 'invert(true) || invert(true)'}}
303 static_assert(invert(true) == invert(false), ""); // expected-error {{static assertion failed due to requirement 'invert(true) == invert(false)'}} \
304 // expected-note {{evaluates to 'false == true'}}
305 static_assert(true && false, ""); // expected-error {{static assertion failed due to requirement 'true && false'}}
306 static_assert(invert(true) || invert(true) || false, ""); // expected-error {{static assertion failed due to requirement 'invert(true) || invert(true) || false'}}
307 static_assert((true && invert(true)) || false, ""); // expected-error {{static assertion failed due to requirement '(true && invert(true)) || false'}}
308 static_assert(true && invert(false) && invert(true), ""); // expected-error {{static assertion failed due to requirement 'invert(true)'}}
310 /// No notes here since we compare a bool expression with a bool literal.
311 static_assert(invert(true) == true, ""); // expected-error {{failed}}
313 #pragma clang diagnostic push
314 #pragma clang diagnostic ignored "-Wc99-extensions"
315 constexpr _Complex float com = {5,6};
316 constexpr _Complex float com2 = {1, 9};
317 static_assert(com == com2, ""); // expected-error {{failed}} \
318 // expected-note {{evaluates to '(5 + 6i) == (1 + 9i)'}}
319 #pragma clang diagnostic pop
321 #define CHECK_4(x) ((x) == 4)
322 #define A_IS_B (a == b)
323 static_assert(CHECK_4(5), ""); // expected-error {{failed}}
325 constexpr int a = 4;
326 constexpr int b = 5;
327 static_assert(CHECK_4(a) && A_IS_B, ""); // expected-error {{failed}} \
328 // expected-note {{evaluates to '4 == 5'}}
330 static_assert(
331 false, // expected-error {{static assertion failed}}
335 static_assert(
336 true && false, // expected-error {{static assertion failed due to requirement 'true && false'}}
340 static_assert(
341 // with a comment here
342 true && false, // expected-error {{static assertion failed due to requirement 'true && false'}}
346 static_assert(
347 // with a comment here
348 (true && // expected-error {{static assertion failed due to requirement '(true && false) || false'}}
349 false)
350 || false,
354 static_assert(1 + 1 != 2, ""); // expected-error {{failed}} \
355 // expected-note {{evaluates to '2 != 2'}}
356 static_assert(1 - 1 == 2, ""); // expected-error {{failed}} \
357 // expected-note {{evaluates to '0 == 2'}}
358 static_assert(1 * 1 == 2, ""); // expected-error {{failed}} \
359 // expected-note {{evaluates to '1 == 2'}}
360 static_assert(1 / 1 == 2, ""); // expected-error {{failed}} \
361 // expected-note {{evaluates to '1 == 2'}}
362 static_assert(1 << 3 != 8, ""); // expected-error {{failed}} \
363 // expected-note {{evaluates to '8 != 8'}}