1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -pedantic -triple=x86_64-linux-gnu -Wno-invalid-utf8 2 3 int f(); // expected-note {{declared here}} 4 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}} 8 9 void g() { 10 static_assert(false, "false is false"); // expected-error {{static assertion failed: false is false}} 11 } 12 13 class C { 14 static_assert(false, "false is false"); // expected-error {{static assertion failed: false is false}} 15 }; 16 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!}} 19 }; 20 21 T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}} 22 T<2> t2; 23 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}} 27 }; 28 29 S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}} 30 S<int> s2; 31 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}} 37 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: ሴ}} 42 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"); 49 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 57 c 58 )"); // expected-error@-3 {{static assertion failed: a\n\tb\nc\n}} 59 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>}} 62 63 //! Contains RTL/LTR marks 64 static_assert(false, "\u200Eabc\u200Fdef\u200Fgh"); // expected-error {{static assertion failed: abcdefgh}} 65 66 //! Contains ZWJ/regional indicators 67 static_assert(false, "️ "); // expected-error {{static assertion failed: ️ }} 68 69 template<typename T> struct AlwaysFails { 70 // Only give one error here. 71 static_assert(false, ""); // expected-error 2{{static assertion failed}} 72 }; 73 AlwaysFails<int> alwaysFails; // expected-note {{instantiation}} 74 AlwaysFails<double> alwaysFails2; // expected-note {{instantiation}} 75 76 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 80 }; 81 struct X { ~X(); }; 82 StaticAssertProtected<int> sap1; 83 StaticAssertProtected<X> sap2; // expected-note {{instantiation}} 84 85 static_assert(true); // expected-warning {{C++17 extension}} 86 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}} 87 88 89 // Diagnostics for static_assert with multiple conditions 90 template<typename T> struct first_trait { 91 static const bool value = false; 92 }; 93 94 template<> 95 struct first_trait<X> { 96 static const bool value = true; 97 }; 98 99 template<typename T> struct second_trait { 100 static const bool value = false; 101 }; 102 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}} 104 105 namespace std { 106 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; } 114 }; 115 116 template <class Tp, Tp v> 117 const Tp integral_constant<Tp, v>::value; 118 119 typedef integral_constant<bool, true> true_type; 120 typedef integral_constant<bool, false> false_type; 121 122 template <class Tp> 123 struct is_const : public false_type {}; 124 template <class Tp> 125 struct is_const<Tp const> : public true_type {}; 126 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; 131 }; 132 133 template <typename T> 134 struct is_same<T, T> { 135 static const bool value = true; 136 }; 137 138 } // namespace std 139 140 struct ExampleTypes { 141 explicit ExampleTypes(int); 142 using T = int; 143 using U = float; 144 }; 145 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}} 170 171 struct BI_tag {}; 172 struct RAI_tag : BI_tag {}; 173 struct MyIterator { 174 using tag = BI_tag; 175 }; 176 struct MyContainer { 177 using iterator = MyIterator; 178 }; 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}} 183 } 184 template void foo<MyContainer>(); 185 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}} 186 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> {}; 193 }; 194 }; 195 } // namespace ns 196 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::NestedTemplates3<float>::value': message}} 201 } 202 template void foo2<int, float, 3>(); 203 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}} 204 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}} 211 } 212 void callFoo3() { 213 foo3([]() {}); 214 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }} 215 } 216 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}} 221 } 222 void callFoo4() { foo4(42); } 223 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}} 224 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}} 229 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'}} 235 236 static_assert(1 , "") // expected-error {{expected ';' after 'static_assert'}} 237 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}} 243 }; 244 245 template <typename Ty> 246 struct T { 247 static_assert(false, "test"); // expected-error{{static assertion failed: test}} 248 }; 249 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}} 253 } 254 } 255 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}} 262 263 /// Simple things are ignored. 264 static_assert(1 == (-(1)), ""); //expected-error {{failed}} 265 266 /// Chars are printed as chars. 267 constexpr char getChar() { 268 return 'c'; 269 } 270 static_assert(getChar() == 'a', ""); // expected-error {{failed}} \ 271 // expected-note {{evaluates to ''c' == 'a''}} 272 273 /// Bools are printed as bools. 274 constexpr bool invert(bool b) { 275 return !b; 276 } 277 278 static_assert(invert(true) || invert(true), ""); // expected-error {{static assertion failed due to requirement 'invert(true) || invert(true)'}} 279 static_assert(invert(true) == invert(false), ""); // expected-error {{static assertion failed due to requirement 'invert(true) == invert(false)'}} \ 280 // expected-note {{evaluates to 'false == true'}} 281 static_assert(true && false, ""); // expected-error {{static assertion failed due to requirement 'true && false'}} 282 static_assert(invert(true) || invert(true) || false, ""); // expected-error {{static assertion failed due to requirement 'invert(true) || invert(true) || false'}} 283 static_assert((true && invert(true)) || false, ""); // expected-error {{static assertion failed due to requirement '(true && invert(true)) || false'}} 284 static_assert(true && invert(false) && invert(true), ""); // expected-error {{static assertion failed due to requirement 'invert(true)'}} 285 286 /// No notes here since we compare a bool expression with a bool literal. 287 static_assert(invert(true) == true, ""); // expected-error {{failed}} 288 289 #pragma clang diagnostic push 290 #pragma clang diagnostic ignored "-Wc99-extensions" 291 constexpr _Complex float com = {5,6}; 292 constexpr _Complex float com2 = {1, 9}; 293 static_assert(com == com2, ""); // expected-error {{failed}} \ 294 // expected-note {{evaluates to '(5 + 6i) == (1 + 9i)'}} 295 #pragma clang diagnostic pop 296 297 #define CHECK_4(x) ((x) == 4) 298 #define A_IS_B (a == b) 299 static_assert(CHECK_4(5), ""); // expected-error {{failed}} 300 301 constexpr int a = 4; 302 constexpr int b = 5; 303 static_assert(CHECK_4(a) && A_IS_B, ""); // expected-error {{failed}} \ 304 // expected-note {{evaluates to '4 == 5'}} 305 306 static_assert( 307 false, // expected-error {{static assertion failed}} 308 "" 309 ); 310 311 static_assert( 312 true && false, // expected-error {{static assertion failed due to requirement 'true && false'}} 313 "" 314 ); 315 316 static_assert( 317 // with a comment here 318 true && false, // expected-error {{static assertion failed due to requirement 'true && false'}} 319 "" 320 ); 321 322 static_assert( 323 // with a comment here 324 (true && // expected-error {{static assertion failed due to requirement '(true && false) || false'}} 325 false) 326 || false, 327 "" 328 ); 329 330 } 331