1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=x86_64-linux-gnu 2 3 int f(); // expected-note {{declared here}} 4 5 static_assert(f(), "f"); // expected-error {{static_assert 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_assert failed "false is false"}} 8 9 void g() { 10 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}} 11 } 12 13 class C { 14 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}} 15 }; 16 17 template<int N> struct T { 18 static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert 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_assert failed due to requirement 'sizeof(char) > sizeof(char)' "Type not big enough!"}} 26 }; 27 28 S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}} 29 S<int> s2; 30 31 static_assert(false, L"\xFFFFFFFF"); // expected-error {{static_assert failed L"\xFFFFFFFF"}} 32 static_assert(false, u"\U000317FF"); // expected-error {{static_assert failed u"\U000317FF"}} 33 // FIXME: render this as u8"\u03A9" 34 static_assert(false, u8"Ω"); // expected-error {{static_assert failed u8"\316\251"}} 35 static_assert(false, L"\u1234"); // expected-error {{static_assert failed L"\x1234"}} 36 static_assert(false, L"\x1ff" "0\x123" "fx\xfffff" "goop"); // expected-error {{static_assert failed L"\x1FF""0\x123""fx\xFFFFFgoop"}} 37 38 template<typename T> struct AlwaysFails { 39 // Only give one error here. 40 static_assert(false, ""); // expected-error {{static_assert failed}} 41 }; 42 AlwaysFails<int> alwaysFails; 43 44 template<typename T> struct StaticAssertProtected { 45 static_assert(__is_literal(T), ""); // expected-error {{static_assert failed}} 46 static constexpr T t = {}; // no error here 47 }; 48 struct X { ~X(); }; 49 StaticAssertProtected<int> sap1; 50 StaticAssertProtected<X> sap2; // expected-note {{instantiation}} 51 52 static_assert(true); // expected-warning {{C++17 extension}} 53 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}} 54 55 56 // Diagnostics for static_assert with multiple conditions 57 template<typename T> struct first_trait { 58 static const bool value = false; 59 }; 60 61 template<> 62 struct first_trait<X> { 63 static const bool value = true; 64 }; 65 66 template<typename T> struct second_trait { 67 static const bool value = false; 68 }; 69 70 static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}} 71 72 namespace std { 73 74 template <class Tp, Tp v> 75 struct integral_constant { 76 static const Tp value = v; 77 typedef Tp value_type; 78 typedef integral_constant type; 79 }; 80 81 template <class Tp, Tp v> 82 const Tp integral_constant<Tp, v>::value; 83 84 typedef integral_constant<bool, true> true_type; 85 typedef integral_constant<bool, false> false_type; 86 87 template <class Tp> 88 struct is_const : public false_type {}; 89 template <class Tp> 90 struct is_const<Tp const> : public true_type {}; 91 92 // We do not define is_same in terms of integral_constant to check that both implementations are supported. 93 template <typename T, typename U> 94 struct is_same { 95 static const bool value = false; 96 }; 97 98 template <typename T> 99 struct is_same<T, T> { 100 static const bool value = true; 101 }; 102 103 } // namespace std 104 105 struct ExampleTypes { 106 using T = int; 107 using U = float; 108 }; 109 110 static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message"); 111 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}} 112 static_assert(std::is_const<ExampleTypes::T>::value, "message"); 113 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}} 114 static_assert(!std::is_const<const ExampleTypes::T>::value, "message"); 115 // expected-error@-1{{static_assert failed due to requirement '!std::is_const<const int>::value' "message"}} 116 static_assert(!(std::is_const<const ExampleTypes::T>::value), "message"); 117 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<const int>::value)' "message"}} 118 static_assert(std::is_const<const ExampleTypes::T>::value == false, "message"); 119 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<const int>::value == false' "message"}} 120 static_assert(!(std::is_const<const ExampleTypes::T>::value == true), "message"); 121 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<const int>::value == true)' "message"}} 122 123 struct BI_tag {}; 124 struct RAI_tag : BI_tag {}; 125 struct MyIterator { 126 using tag = BI_tag; 127 }; 128 struct MyContainer { 129 using iterator = MyIterator; 130 }; 131 template <class Container> 132 void foo() { 133 static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message"); 134 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}} 135 } 136 template void foo<MyContainer>(); 137 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}} 138 139 namespace ns { 140 template <typename T, int v> 141 struct NestedTemplates1 { 142 struct NestedTemplates2 { 143 template <typename U> 144 struct NestedTemplates3 : public std::is_same<T, U> {}; 145 }; 146 }; 147 } // namespace ns 148 149 template <typename T, typename U, int a> 150 void foo2() { 151 static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message"); 152 // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}} 153 } 154 template void foo2<int, float, 3>(); 155 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}} 156 157 template <class T> 158 void foo3(T t) { 159 static_assert(std::is_const<T>::value, "message"); 160 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} 161 static_assert(std::is_const<decltype(t)>::value, "message"); 162 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} 163 } 164 void callFoo3() { 165 foo3([]() {}); 166 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }} 167 } 168 169 template <class T> 170 void foo4(T t) { 171 static_assert(std::is_const<typename T::iterator>::value, "message"); 172 // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}} 173 } 174 void callFoo4() { foo4(42); } 175 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}} 176