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
g()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 };
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 assertion failed: L"\xFFFFFFFF"}}
32 static_assert(false, u"\U000317FF"); // expected-error {{static assertion failed: u"\U000317FF"}}
33
34 static_assert(false, u8"Ω"); // expected-error {{static assertion failed: u8"\316\251"}}
35 static_assert(false, L"\u1234"); // expected-error {{static assertion failed: L"\x1234"}}
36 static_assert(false, L"\x1ff" "0\x123" "fx\xfffff" "goop"); // expected-error {{static assertion failed: L"\x1FF""0\x123""fx\xFFFFFgoop"}}
37
38 static_assert(false, R"(a
39 \tb
40 c
41 )"); // expected-error@-3 {{static assertion failed: a\n\tb\nc\n}}
42
43 static_assert(false, "\u0080\u0081\u0082\u0083\u0099\u009A\u009B\u009C\u009D\u009E\u009F");
44 // 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>}}
45
46 //! Contains RTL/LTR marks
47 static_assert(false, "\u200Eabc\u200Fdef\u200Fgh"); // expected-error {{static assertion failed: abcdefgh}}
48
49 //! Contains ZWJ/regional indicators
50 static_assert(false, "️ "); // expected-error {{static assertion failed: ️ }}
51
52 template<typename T> struct AlwaysFails {
53 // Only give one error here.
54 static_assert(false, ""); // expected-error {{static assertion failed}}
55 };
56 AlwaysFails<int> alwaysFails;
57
58 template<typename T> struct StaticAssertProtected {
59 static_assert(__is_literal(T), ""); // expected-error {{static assertion failed}}
60 static constexpr T t = {}; // no error here
61 };
62 struct X { ~X(); };
63 StaticAssertProtected<int> sap1;
64 StaticAssertProtected<X> sap2; // expected-note {{instantiation}}
65
66 static_assert(true); // expected-warning {{C++17 extension}}
67 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}
68
69
70 // Diagnostics for static_assert with multiple conditions
71 template<typename T> struct first_trait {
72 static const bool value = false;
73 };
74
75 template<>
76 struct first_trait<X> {
77 static const bool value = true;
78 };
79
80 template<typename T> struct second_trait {
81 static const bool value = false;
82 };
83
84 static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static assertion failed due to requirement 'second_trait<X>::value': message}}
85
86 namespace std {
87
88 template <class Tp, Tp v>
89 struct integral_constant {
90 static const Tp value = v;
91 typedef Tp value_type;
92 typedef integral_constant type;
operator value_typestd::integral_constant93 constexpr operator value_type() const noexcept { return value; }
operator ()std::integral_constant94 constexpr value_type operator()() const noexcept { return value; }
95 };
96
97 template <class Tp, Tp v>
98 const Tp integral_constant<Tp, v>::value;
99
100 typedef integral_constant<bool, true> true_type;
101 typedef integral_constant<bool, false> false_type;
102
103 template <class Tp>
104 struct is_const : public false_type {};
105 template <class Tp>
106 struct is_const<Tp const> : public true_type {};
107
108 // We do not define is_same in terms of integral_constant to check that both implementations are supported.
109 template <typename T, typename U>
110 struct is_same {
111 static const bool value = false;
112 };
113
114 template <typename T>
115 struct is_same<T, T> {
116 static const bool value = true;
117 };
118
119 } // namespace std
120
121 struct ExampleTypes {
122 explicit ExampleTypes(int);
123 using T = int;
124 using U = float;
125 };
126
127 static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message");
128 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<int, float>::value': message}}
129 static_assert(std::is_const<ExampleTypes::T>::value, "message");
130 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
131 static_assert(!std::is_const<const ExampleTypes::T>::value, "message");
132 // expected-error@-1{{static assertion failed due to requirement '!std::is_const<const int>::value': message}}
133 static_assert(!(std::is_const<const ExampleTypes::T>::value), "message");
134 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>::value)': message}}
135 static_assert(std::is_const<const ExampleTypes::T>::value == false, "message");
136 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<const int>::value == false': message}}
137 static_assert(!(std::is_const<const ExampleTypes::T>::value == true), "message");
138 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>::value == true)': message}}
139 static_assert(std::is_const<ExampleTypes::T>(), "message");
140 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>()': message}}
141 static_assert(!(std::is_const<const ExampleTypes::T>()()), "message");
142 // expected-error@-1{{static assertion failed due to requirement '!(std::is_const<const int>()())': message}}
143 static_assert(std::is_same<decltype(std::is_const<const ExampleTypes::T>()), int>::value, "message");
144 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<std::is_const<const int>, int>::value': message}}
145 static_assert(std::is_const<decltype(ExampleTypes::T(3))>::value, "message");
146 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
147 static_assert(std::is_const<decltype(ExampleTypes::T())>::value, "message");
148 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<int>::value': message}}
149 static_assert(std::is_const<decltype(ExampleTypes(3))>::value, "message");
150 // expected-error@-1{{static assertion failed due to requirement 'std::is_const<ExampleTypes>::value': message}}
151
152 struct BI_tag {};
153 struct RAI_tag : BI_tag {};
154 struct MyIterator {
155 using tag = BI_tag;
156 };
157 struct MyContainer {
158 using iterator = MyIterator;
159 };
160 template <class Container>
foo()161 void foo() {
162 static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message");
163 // expected-error@-1{{static assertion failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value': message}}
164 }
165 template void foo<MyContainer>();
166 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}}
167
168 namespace ns {
169 template <typename T, int v>
170 struct NestedTemplates1 {
171 struct NestedTemplates2 {
172 template <typename U>
173 struct NestedTemplates3 : public std::is_same<T, U> {};
174 };
175 };
176 } // namespace ns
177
178 template <typename T, typename U, int a>
foo2()179 void foo2() {
180 static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message");
181 // expected-error@-1{{static assertion failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value': message}}
182 }
183 template void foo2<int, float, 3>();
184 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}}
185
186 template <class T>
foo3(T t)187 void foo3(T t) {
188 static_assert(std::is_const<T>::value, "message");
189 // expected-error-re@-1{{static assertion failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value': message}}
190 static_assert(std::is_const<decltype(t)>::value, "message");
191 // expected-error-re@-1{{static assertion failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value': message}}
192 }
callFoo3()193 void callFoo3() {
194 foo3([]() {});
195 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }}
196 }
197
198 template <class T>
foo4(T t)199 void foo4(T t) {
200 static_assert(std::is_const<typename T::iterator>::value, "message");
201 // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}}
202 }
callFoo4()203 void callFoo4() { foo4(42); }
204 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}}
205
206 static_assert(42, "message");
207 static_assert(42.0, "message"); // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 42 to true}}
208 constexpr int *p = 0;
209 static_assert(p, "message"); // expected-error {{static assertion failed}}
210
211 struct NotBool {
212 } notBool;
213 constexpr NotBool constexprNotBool;
214 static_assert(notBool, "message"); // expected-error {{value of type 'struct NotBool' is not contextually convertible to 'bool'}}
215 static_assert(constexprNotBool, "message"); // expected-error {{value of type 'const NotBool' is not contextually convertible to 'bool'}}
216
217 static_assert(1 , "") // expected-error {{expected ';' after 'static_assert'}}
218