1 // RUN: %clang_cc1 -std=c++2b -fsyntax-only -verify=expected,cxx20_2b,cxx2b %s -fcxx-exceptions -triple=x86_64-linux-gnu
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20_2b,cxx20 %s -fcxx-exceptions -triple=x86_64-linux-gnu
3 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify=expected,cxx14_20,cxx14 %s -fcxx-exceptions -triple=x86_64-linux-gnu
4
5 struct S {
6 // dummy ctor to make this a literal type
7 constexpr S(int);
8
9 S();
10
11 int arr[10];
12
getS13 constexpr int &get(int n) { return arr[n]; }
getS14 constexpr const int &get(int n) const { return arr[n]; }
15 };
16
17 S s = S();
18 const S &sr = s;
19 static_assert(&s.get(4) - &sr.get(2) == 2, "");
20
21 // Compound-statements can be used in constexpr functions.
e()22 constexpr int e() {{{{}} return 5; }}
23 static_assert(e() == 5, "");
24
25 // Types can be defined in constexpr functions.
f()26 constexpr int f() {
27 enum E { e1, e2, e3 };
28
29 struct S {
30 constexpr S(E e) : e(e) {}
31 constexpr int get() { return e; }
32 E e;
33 };
34
35 return S(e2).get();
36 }
37 static_assert(f() == 1, "");
38
39 // Variables can be declared in constexpr functions.
g(int k)40 constexpr int g(int k) {
41 const int n = 9;
42 int k2 = k * k;
43 int k3 = k2 * k;
44 return 3 * k3 + 5 * k2 + n * k - 20;
45 }
46 static_assert(g(2) == 42, "");
h(int n)47 constexpr int h(int n) { // expected-error {{constexpr function never produces a constant expression}}
48 static const int m = n; // expected-note {{control flows through the definition of a static variable}} \
49 // cxx14_20-warning {{definition of a static variable in a constexpr function is a C++2b extension}}
50 return m;
51 }
i(int n)52 constexpr int i(int n) { // expected-error {{constexpr function never produces a constant expression}}
53 thread_local const int m = n; // expected-note {{control flows through the definition of a thread_local variable}} \
54 // cxx14_20-warning {{definition of a thread_local variable in a constexpr function is a C++2b extension}}
55 return m;
56 }
57
58 // if-statements can be used in constexpr functions.
j(int k)59 constexpr int j(int k) {
60 if (k == 5)
61 return 1;
62 if (k == 1)
63 return 5;
64 else {
65 if (int n = 2 * k - 4) {
66 return n + 1;
67 return 2;
68 }
69 }
70 } // expected-note 2{{control reached end of constexpr function}}
71 static_assert(j(0) == -3, "");
72 static_assert(j(1) == 5, "");
73 static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}}
74 static_assert(j(3) == 3, "");
75 static_assert(j(4) == 5, "");
76 static_assert(j(5) == 1, "");
77
78 // There can be 0 return-statements.
k()79 constexpr void k() {
80 }
81
82 // If the return type is not 'void', no return statements => never a constant
83 // expression, so still diagnose that case.
fn()84 [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}}
85 fn();
86 }
87
88 // We evaluate the body of a constexpr constructor, to check for side-effects.
89 struct U {
UU90 constexpr U(int n) {
91 if (j(n)) {} // expected-note {{in call to 'j(2)'}}
92 }
93 };
94 constexpr U u1{1};
95 constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}}
96
97 // We allow expression-statements.
l(bool b)98 constexpr int l(bool b) {
99 if (b)
100 throw "invalid value for b!"; // expected-note {{subexpression not valid}}
101 return 5;
102 }
103 static_assert(l(false) == 5, "");
104 static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}}
105
106 // Potential constant expression checking is still applied where possible.
htonl(int x)107 constexpr int htonl(int x) { // expected-error {{never produces a constant expression}}
108 typedef unsigned char uchar;
109 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) };
110 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
111 }
112
maybe_htonl(bool isBigEndian,int x)113 constexpr int maybe_htonl(bool isBigEndian, int x) {
114 if (isBigEndian)
115 return x;
116
117 typedef unsigned char uchar;
118 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) };
119 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
120 }
121
122 constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}}
123
124 namespace NS {
125 constexpr int n = 0;
126 }
namespace_alias()127 constexpr int namespace_alias() {
128 namespace N = NS;
129 return N::n;
130 }
131
132 namespace assign {
133 constexpr int a = 0;
134 const int b = 0;
135 int c = 0; // expected-note {{here}}
136
set(const int & a,int b)137 constexpr void set(const int &a, int b) {
138 const_cast<int&>(a) = b; // expected-note 3{{constant expression cannot modify an object that is visible outside that expression}}
139 }
wrap(int a,int b)140 constexpr int wrap(int a, int b) {
141 set(a, b);
142 return a;
143 }
144
145 static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}}
146 static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}}
147 static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(c, 1)'}}
148
149 static_assert(wrap(a, 1) == 1, "");
150 static_assert(wrap(b, 1) == 1, "");
151 static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
152 }
153
154 namespace string_assign {
155 template<typename T>
swap(T & a,T & b)156 constexpr void swap(T &a, T &b) {
157 T tmp = a;
158 a = b;
159 b = tmp;
160 }
161 template<typename Iterator>
reverse(Iterator begin,Iterator end)162 constexpr void reverse(Iterator begin, Iterator end) {
163 while (begin != end && begin != --end)
164 swap(*begin++, *end);
165 }
166 template<typename Iterator1, typename Iterator2>
equal(Iterator1 a,Iterator1 ae,Iterator2 b,Iterator2 be)167 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) {
168 while (a != ae && b != be)
169 if (*a++ != *b++)
170 return false;
171 return a == ae && b == be;
172 }
test1(int n)173 constexpr bool test1(int n) {
174 char stuff[100] = "foobarfoo";
175 const char stuff2[100] = "oofraboof";
176 reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}}
177 return equal(stuff, stuff + n, stuff2, stuff2 + n);
178 }
179 static_assert(!test1(1), "");
180 static_assert(test1(3), "");
181 static_assert(!test1(6), "");
182 static_assert(test1(9), "");
183 static_assert(!test1(100), "");
184 static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}}
185
f()186 constexpr void f() { // expected-error{{constexpr function never produces a constant expression}} expected-note@+2{{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}}
187 char foo[10] = { "z" }; // expected-note {{here}}
188 foo[10] = 'x'; // expected-warning {{past the end}}
189 }
190 }
191
192 namespace array_resize {
do_stuff(int k1,int k2)193 constexpr int do_stuff(int k1, int k2) {
194 int arr[1234] = { 1, 2, 3, 4 };
195 arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}}
196 return arr[k2];
197 }
198 static_assert(do_stuff(1, 2) == 3, "");
199 static_assert(do_stuff(0, 0) == 5, "");
200 static_assert(do_stuff(1233, 1233) == 5, "");
201 static_assert(do_stuff(1233, 0) == 1, "");
202 static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
203 static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
204 static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
205 }
206
207 namespace potential_const_expr {
set(int & n)208 constexpr void set(int &n) { n = 1; }
div_zero_1()209 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error
div_zero_2()210 constexpr int div_zero_2() { // expected-error {{never produces a constant expression}}
211 int z = 0;
212 return 100 / (set(z), 0); // expected-note {{division by zero}}
213 }
214 int n; // expected-note {{declared here}}
ref()215 constexpr int ref() { // expected-error {{never produces a constant expression}}
216 int &r = n;
217 return r; // expected-note {{read of non-const variable 'n'}}
218 }
219 }
220
221 namespace subobject {
A()222 union A { constexpr A() : y(5) {} int x, y; };
223 struct B { A a; };
224 struct C : B {};
D(int n)225 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; };
f(D & d)226 constexpr void f(D &d) {
227 d.c.a.y = 3;
228 // expected-note@-1 {{cannot modify an object that is visible outside}}
229 // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}}
230 }
check(D & d)231 constexpr bool check(D &d) { return d.c.a.y == 3; }
232 // cxx20_2b-note@-1 {{read of member 'y' of union with active member 'x'}}
233
g()234 constexpr bool g() { D d; f(d); return d.c.a.y == 3; }
235 static_assert(g(), "");
236
237 D d;
h()238 constexpr bool h() { f(d); return check(d); } // expected-note {{in call}}
239 static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}}
240
i()241 constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}}
242 static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}}
243
j()244 constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // cxx14-note {{assignment to member 'x' of union with active member 'y'}}
245 // cxx20_2b-note@-1 {{in call to 'check(d)'}}
246 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}}
247 }
248
249 namespace lifetime {
id(int && n)250 constexpr int &&id(int &&n) { return static_cast<int&&>(n); }
dead()251 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}}
bad()252 constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}}
253 static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}}
254 }
255
256 namespace const_modify {
modify(int & n)257 constexpr int modify(int &n) { return n = 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}}
test1()258 constexpr int test1() { int k = 0; return modify(k); }
test2()259 constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note 2 {{in call}}
260 static_assert(test1() == 1, "");
261 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
262 constexpr int i = test2(); // expected-error {{constant expression}} expected-note {{in call}}
263 }
264
265 namespace null {
test(int * p)266 constexpr int test(int *p) {
267 return *p = 123; // expected-note {{assignment to dereferenced null pointer}}
268 }
269 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}}
270 }
271
272 namespace incdec {
ref(T && r)273 template<typename T> constexpr T &ref(T &&r) { return r; }
274 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
postinc(T && r)275 template<typename T> constexpr T postinc(T &&r) { return (r++, r); }
postdec(T && r)276 template<typename T> constexpr T postdec(T &&r) { return (r--, r); }
277
278 template int &ref<int>(int &&);
279 // cxx2b-note@-1 {{in instantiation of function template specialization}}
280
281 static_assert(postinc(0) == 1, "");
282 static_assert(postdec(0) == -1, "");
283 #if __cplusplus <= 202002L
284 static_assert(++ref(0) == 1, "");
285 static_assert(ref(0)++ == 0, "");
286 static_assert(--ref(0) == -1, "");
287 static_assert(ref(0)-- == 0, "");
288 #endif
289
290 #if __cplusplus <= 202002L
291 constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}}
292 constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++;
293 constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}}
294 constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe);
295
296 // inc/dec on short can't overflow because we promote to int first
297 static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, "");
298 static_assert(--ref<short>(0x8000) == 0x7fff, "");
299
300 // inc on bool sets to true
301 static_assert(++ref(false), "");
302 // cxx14-warning@-1 {{incrementing expression of type bool}}
303 // cxx20-error@-2 {{incrementing expression of type bool}}
304 static_assert(++ref(true), "");
305 // cxx14-warning@-1 {{incrementing expression of type bool}}
306 // cxx20-error@-2 {{incrementing expression of type bool}}
307 #endif
308
309 int arr[10];
310 static_assert(postinc(&arr[0]) == &arr[1], "");
311 static_assert(postdec(&arr[1]) == &arr[0], "");
312 #if __cplusplus <= 202002L
313 static_assert(++ref(&arr[0]) == &arr[1], "");
314 static_assert(++ref(&arr[9]) == &arr[10], "");
315 static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
316 static_assert(ref(&arr[0])++ == &arr[0], "");
317 static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
318 static_assert(--ref(&arr[10]) == &arr[9], "");
319 static_assert(--ref(&arr[1]) == &arr[0], "");
320 static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
321 static_assert(ref(&arr[1])-- == &arr[1], "");
322 static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
323 #endif
324
325 static_assert(postinc(0.0) == 1.0, "");
326 static_assert(postdec(0.0) == -1.0, "");
327 #if __cplusplus <= 202002L
328 int x;
329 static_assert(++ref(&x) == &x + 1, "");
330
331 static_assert(++ref(0.0) == 1.0, "");
332 static_assert(ref(0.0)++ == 0.0, "");
333 static_assert(--ref(0.0) == -1.0, "");
334 static_assert(ref(0.0)-- == 0.0, "");
335
336 static_assert(++ref(1e100) == 1e100, "");
337 static_assert(--ref(1e100) == 1e100, "");
338 #endif
339
340 union U {
341 int a, b;
342 };
f(U u)343 constexpr int f(U u) {
344 return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}}
345 }
346 constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}}
347 constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}}
348 // cxx20_2b-warning@-1 {{decrement of object of volatile-qualified type 'volatile int' is deprecated}}
349
incr(int k)350 constexpr int incr(int k) {
351 int x = k;
352 if (x++ == 100)
353 return x;
354 return incr(x);
355 }
356 static_assert(incr(0) == 101, "");
357 }
358
359 namespace compound_assign {
test_int()360 constexpr bool test_int() {
361 int a = 3;
362 a += 6;
363 if (a != 9) return false;
364 a -= 2;
365 if (a != 7) return false;
366 a *= 3;
367 if (a != 21) return false;
368 if (&(a /= 10) != &a) return false;
369 if (a != 2) return false;
370 a <<= 3;
371 if (a != 16) return false;
372 a %= 6;
373 if (a != 4) return false;
374 a >>= 1;
375 if (a != 2) return false;
376 a ^= 10;
377 if (a != 8) return false;
378 a |= 5;
379 if (a != 13) return false;
380 a &= 14;
381 if (a != 12) return false;
382 a += -1.2;
383 if (a != 10) return false;
384 a -= 3.1;
385 if (a != 6) return false;
386 a *= 2.2;
387 if (a != 13) return false;
388 if (&(a /= 1.5) != &a) return false;
389 if (a != 8) return false;
390 return true;
391 }
392 static_assert(test_int(), "");
393
test_float()394 constexpr bool test_float() {
395 float f = 123.;
396 f *= 2;
397 if (f != 246.) return false;
398 if ((f -= 0.5) != 245.5) return false;
399 if (f != 245.5) return false;
400 f /= 0.5;
401 if (f != 491.) return false;
402 f += -40;
403 if (f != 451.) return false;
404 return true;
405 }
406 static_assert(test_float(), "");
407
test_bool()408 constexpr bool test_bool() {
409 bool b = false;
410 b |= 2;
411 if (b != true) return false;
412 b <<= 1;
413 if (b != true) return false;
414 b *= 2;
415 if (b != true) return false;
416 b -= 1;
417 if (b != false) return false;
418 b -= 1;
419 if (b != true) return false;
420 b += -1;
421 if (b != false) return false;
422 b += 1;
423 if (b != true) return false;
424 b += 1;
425 if (b != true) return false;
426 b ^= b;
427 if (b != false) return false;
428 return true;
429 }
430 static_assert(test_bool(), "");
431
test_ptr()432 constexpr bool test_ptr() {
433 int arr[123] = {};
434 int *p = arr;
435 if ((p += 4) != &arr[4]) return false;
436 if (p != &arr[4]) return false;
437 p += -1;
438 if (p != &arr[3]) return false;
439 if ((p -= -10) != &arr[13]) return false;
440 if (p != &arr[13]) return false;
441 p -= 11;
442 if (p != &arr[2]) return false;
443 return true;
444 }
445 static_assert(test_ptr(), "");
446
447 template<typename T>
test_overflow()448 constexpr bool test_overflow() {
449 T a = 1;
450 while (a != a / 2)
451 a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }}
452 return true;
453 }
454
455 static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}}
456 static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined
457 static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication
458 static_assert(test_overflow<unsigned short>(), ""); // ok
459 static_assert(test_overflow<unsigned long long>(), ""); // ok
460 static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}}
461 static_assert(test_overflow<float>(), ""); // ok
462 static_assert(test_overflow<double>(), ""); // ok
463
test_promotion(short k)464 constexpr short test_promotion(short k) {
465 short s = k;
466 s *= s;
467 return s;
468 }
469 static_assert(test_promotion(100) == 10000, "");
470 static_assert(test_promotion(200) == -25536, "");
471 static_assert(test_promotion(256) == 0, "");
472
test_bounds(const char * p,int o)473 constexpr const char *test_bounds(const char *p, int o) {
474 return p += o; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}}
475 }
476 static_assert(test_bounds("foo", 0)[0] == 'f', "");
477 static_assert(test_bounds("foo", 3)[0] == 0, "");
478 static_assert(test_bounds("foo", 4)[-3] == 'o', "");
479 static_assert(test_bounds(&"foo"[4], -4)[0] == 'f', "");
480 static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}}
481 static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}}
482 static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}}
483 }
484
485 namespace loops {
fib_loop(int a)486 constexpr int fib_loop(int a) {
487 int f_k = 0, f_k_plus_one = 1;
488 for (int k = 1; k != a; ++k) {
489 int f_k_plus_two = f_k + f_k_plus_one;
490 f_k = f_k_plus_one;
491 f_k_plus_one = f_k_plus_two;
492 }
493 return f_k_plus_one;
494 }
495 static_assert(fib_loop(46) == 1836311903, "");
496
breaks_work()497 constexpr bool breaks_work() {
498 int a = 0;
499 for (int n = 0; n != 100; ++n) {
500 ++a;
501 if (a == 5) continue;
502 if ((a % 5) == 0) break;
503 }
504
505 int b = 0;
506 while (b != 17) {
507 ++b;
508 if (b == 6) continue;
509 if ((b % 6) == 0) break;
510 }
511
512 int c = 0;
513 do {
514 ++c;
515 if (c == 7) continue;
516 if ((c % 7) == 0) break;
517 } while (c != 21);
518
519 return a == 10 && b == 12 && c == 14;
520 }
521 static_assert(breaks_work(), "");
522
523 void not_constexpr();
no_cont_after_break()524 constexpr bool no_cont_after_break() {
525 for (;;) {
526 break;
527 not_constexpr();
528 }
529 while (true) {
530 break;
531 not_constexpr();
532 }
533 do {
534 break;
535 not_constexpr();
536 } while (true);
537 return true;
538 }
539 static_assert(no_cont_after_break(), "");
540
cond()541 constexpr bool cond() {
542 for (int a = 1; bool b = a != 3; ++a) {
543 if (!b)
544 return false;
545 }
546 while (bool b = true) {
547 b = false;
548 break;
549 }
550 return true;
551 }
552 static_assert(cond(), "");
553
range_for()554 constexpr int range_for() {
555 int arr[] = { 1, 2, 3, 4, 5 };
556 int sum = 0;
557 for (int x : arr)
558 sum += x;
559 return sum;
560 }
561 static_assert(range_for() == 15, "");
562
563 template<int...N> struct ints {};
564 template<typename A, typename B> struct join_ints;
565 template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
566 using type = ints<As..., sizeof...(As) + Bs...>;
567 };
568 template<unsigned N> struct make_ints {
569 using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
570 };
571 template<> struct make_ints<0> { using type = ints<>; };
572 template<> struct make_ints<1> { using type = ints<0>; };
573
ignoreloops::ignore574 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
575
576 template<typename T, unsigned N> struct array {
arrayloops::array577 constexpr array() : arr{} {}
578 template<typename ...X>
arrayloops::array579 constexpr array(X ...x) : arr{} {
580 init(typename make_ints<sizeof...(X)>::type{}, x...);
581 }
initloops::array582 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
583 ignore{arr[I] = x ...};
584 }
585 T arr[N];
586 struct iterator {
587 T *p;
iteratorloops::array::iterator588 constexpr explicit iterator(T *p) : p(p) {}
operator !=loops::array::iterator589 constexpr bool operator!=(iterator o) { return p != o.p; }
operator ++loops::array::iterator590 constexpr iterator &operator++() { ++p; return *this; }
operator *loops::array::iterator591 constexpr T &operator*() { return *p; }
592 };
beginloops::array593 constexpr iterator begin() { return iterator(arr); }
endloops::array594 constexpr iterator end() { return iterator(arr + N); }
595 };
596
range_for_2()597 constexpr int range_for_2() {
598 array<int, 5> arr { 1, 2, 3, 4, 5 };
599 int sum = 0;
600 for (int k : arr) {
601 sum += k;
602 if (sum > 8) break;
603 }
604 return sum;
605 }
606 static_assert(range_for_2() == 10, "");
607 }
608
609 namespace assignment_op {
610 struct A {
Aassignment_op::A611 constexpr A() : n(5) {}
612 int n;
613 struct B {
614 int k = 1;
615 union U {
U()616 constexpr U() : y(4) {}
617 int x;
618 int y;
619 } u;
620 } b;
621 };
testA()622 constexpr bool testA() {
623 A a, b;
624 a.n = 7;
625 a.b.u.y = 5;
626 b = a;
627 return b.n == 7 && b.b.u.y == 5 && b.b.k == 1;
628 }
629 static_assert(testA(), "");
630
631 struct B {
632 bool assigned = false;
operator =assignment_op::B633 constexpr B &operator=(const B&) {
634 assigned = true;
635 return *this;
636 }
637 };
638 struct C : B {
639 B b;
640 int n = 5;
641 };
testC()642 constexpr bool testC() {
643 C c, d;
644 c.n = 7;
645 d = c;
646 c.n = 3;
647 return d.n == 7 && d.assigned && d.b.assigned;
648 }
649 static_assert(testC(), "");
650 }
651
652 namespace switch_stmt {
no_such_case(int n)653 constexpr bool no_such_case(int n) {
654 switch (n) { case 1: return false; }
655 return true;
656 }
657 static_assert(no_such_case(0), "");
658
f(char k)659 constexpr int f(char k) {
660 bool b = false;
661 int z = 6;
662 switch (k) {
663 return -1;
664 case 0:
665 if (false) {
666 case 1:
667 z = 1;
668 for (; b;) {
669 return 5;
670 while (0)
671 case 2: return 2;
672 case 7: z = 7;
673 do case 6: {
674 return z;
675 if (false)
676 case 3: return 3;
677 case 4: z = 4;
678 } while (1);
679 case 5: b = true;
680 case 9: z = 9;
681 }
682 return z;
683 } else if (false) case 8: z = 8;
684 else if (false) {
685 case 10:
686 z = -10;
687 break;
688 }
689 else z = 0;
690 return z;
691 default:
692 return -1;
693 }
694 return -z;
695 }
696 static_assert(f(0) == 0, "");
697 static_assert(f(1) == 1, "");
698 static_assert(f(2) == 2, "");
699 static_assert(f(3) == 3, "");
700 static_assert(f(4) == 4, "");
701 static_assert(f(5) == 5, "");
702 static_assert(f(6) == 6, "");
703 static_assert(f(7) == 7, "");
704 static_assert(f(8) == 8, "");
705 static_assert(f(9) == 9, "");
706 static_assert(f(10) == 10, "");
707
708 // Check that we can continue an outer loop from within a switch.
contin()709 constexpr bool contin() {
710 for (int n = 0; n != 10; ++n) {
711 switch (n) {
712 case 0:
713 ++n;
714 continue;
715 case 1:
716 return false;
717 case 2:
718 return true;
719 }
720 }
721 return false;
722 }
723 static_assert(contin(), "");
724
switch_into_for()725 constexpr bool switch_into_for() {
726 int n = 0;
727 switch (n) {
728 for (; n == 1; ++n) {
729 return n == 1;
730 case 0: ;
731 }
732 }
733 return false;
734 }
735 static_assert(switch_into_for(), "");
736
duff_copy(char * a,const char * b,int n)737 constexpr void duff_copy(char *a, const char *b, int n) {
738 switch ((n - 1) % 8 + 1) {
739 for ( ; n; n = (n - 1) & ~7) {
740 case 8: a[n-8] = b[n-8];
741 case 7: a[n-7] = b[n-7];
742 case 6: a[n-6] = b[n-6];
743 case 5: a[n-5] = b[n-5];
744 case 4: a[n-4] = b[n-4];
745 case 3: a[n-3] = b[n-3];
746 case 2: a[n-2] = b[n-2];
747 case 1: a[n-1] = b[n-1];
748 }
749 case 0: ;
750 }
751 }
752
test_copy(const char * str,int n)753 constexpr bool test_copy(const char *str, int n) {
754 char buffer[16] = {};
755 duff_copy(buffer, str, n);
756 for (int i = 0; i != sizeof(buffer); ++i)
757 if (buffer[i] != (i < n ? str[i] : 0))
758 return false;
759 return true;
760 }
761 static_assert(test_copy("foo", 0), "");
762 static_assert(test_copy("foo", 1), "");
763 static_assert(test_copy("foo", 2), "");
764 static_assert(test_copy("hello world", 0), "");
765 static_assert(test_copy("hello world", 7), "");
766 static_assert(test_copy("hello world", 8), "");
767 static_assert(test_copy("hello world", 9), "");
768 static_assert(test_copy("hello world", 10), "");
769 static_assert(test_copy("hello world", 10), "");
770 }
771
772 namespace deduced_return_type {
f()773 constexpr auto f() { return 0; }
g(T t)774 template<typename T> constexpr auto g(T t) { return t; }
775 static_assert(f() == 0, "");
776 static_assert(g(true), "");
777 }
778
779 namespace modify_temporary_during_construction {
780 struct A { int &&temporary; int x; int y; };
f(int & r)781 constexpr int f(int &r) { r *= 9; return r - 12; }
782 constexpr A a = { 6, f(a.temporary), a.temporary }; // expected-note {{temporary created here}}
783 static_assert(a.x == 42, "");
784 static_assert(a.y == 54, "");
785 constexpr int k = a.temporary++; // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
786 }
787
788 namespace std {
789 typedef decltype(sizeof(int)) size_t;
790
791 template <class _E>
792 class initializer_list
793 {
794 const _E* __begin_;
795 size_t __size_;
796
initializer_list(const _E * __b,size_t __s)797 constexpr initializer_list(const _E* __b, size_t __s)
798 : __begin_(__b),
799 __size_(__s)
800 {}
801
802 public:
803 typedef _E value_type;
804 typedef const _E& reference;
805 typedef const _E& const_reference;
806 typedef size_t size_type;
807
808 typedef const _E* iterator;
809 typedef const _E* const_iterator;
810
initializer_list()811 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
812
size() const813 constexpr size_t size() const {return __size_;}
begin() const814 constexpr const _E* begin() const {return __begin_;}
end() const815 constexpr const _E* end() const {return __begin_ + __size_;}
816 };
817 }
818
819 namespace InitializerList {
sum(std::initializer_list<int> ints)820 constexpr int sum(std::initializer_list<int> ints) {
821 int total = 0;
822 for (int n : ints) total += n;
823 return total;
824 }
825 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
826 }
827
828 namespace StmtExpr {
f(int k)829 constexpr int f(int k) {
830 switch (k) {
831 case 0:
832 return 0;
833
834 ({
835 case 1: // expected-note {{not supported}}
836 return 1;
837 });
838 }
839 }
840 static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
841
g()842 constexpr int g() {
843 return ({ int n; n; }); // expected-note {{read of uninitialized object}}
844 }
845 static_assert(g() == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
846
847 // FIXME: We should handle the void statement expression case.
h()848 constexpr int h() { // expected-error {{never produces a constant}}
849 ({ if (true) {} }); // expected-note {{not supported}}
850 return 0;
851 }
852 }
853
854 namespace VirtualFromBase {
855 struct S1 {
856 virtual int f() const;
857 };
858 struct S2 {
859 virtual int f();
860 };
861 template <typename T> struct X : T {
XVirtualFromBase::X862 constexpr X() {}
863 double d = 0.0;
fVirtualFromBase::X864 constexpr int f() { return sizeof(T); }
865 };
866
867 // Non-virtual f(), OK.
868 constexpr X<X<S1>> xxs1;
869 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
870 static_assert(p->f() == sizeof(S1), "");
871
872 // Virtual f(), not OK.
873 constexpr X<X<S2>> xxs2;
874 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
875 static_assert(q->f() == sizeof(X<S2>), ""); // cxx14-error {{constant expression}} cxx14-note {{virtual function}}
876 }
877
878 namespace Lifetime {
get(int && r)879 constexpr int &get(int &&r) { return r; }
880 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
f()881 constexpr int f() {
882 int &r = get(123);
883 return r;
884 // cxx14_20-note@-1 {{read of object outside its lifetime}}
885 }
886 static_assert(f() == 123, ""); // expected-error {{constant expression}} cxx14_20-note {{in call}}
887
g()888 constexpr int g() {
889 int *p = 0;
890 {
891 int n = 0;
892 p = &n;
893 n = 42;
894 }
895 *p = 123; // expected-note {{assignment to object outside its lifetime}}
896 return *p;
897 }
898 static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}}
899
h(int n)900 constexpr int h(int n) {
901 int *p[4] = {};
902 int &&r = 1;
903 p[0] = &r;
904 while (int a = 1) {
905 p[1] = &a;
906 for (int b = 1; int c = 1; ) {
907 p[2] = &b, p[3] = &c;
908 break;
909 }
910 break;
911 }
912 *p[n] = 0; // expected-note 3{{assignment to object outside its lifetime}}
913 return *p[n];
914 }
915 static_assert(h(0) == 0, ""); // ok, lifetime-extended
916 static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
917 static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
918 static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
919
lifetime_versus_loops()920 constexpr void lifetime_versus_loops() {
921 int *p = 0;
922 for (int i = 0; i != 2; ++i) {
923 int *q = p;
924 int n = 0;
925 p = &n;
926 if (i)
927 // This modifies the 'n' from the previous iteration of the loop outside
928 // its lifetime.
929 ++*q; // expected-note {{increment of object outside its lifetime}}
930 }
931 }
932 static_assert((lifetime_versus_loops(), true), ""); // expected-error {{constant expression}} expected-note {{in call}}
933 }
934
935 namespace Bitfields {
936 struct A {
937 bool b : 1;
938 int n : 4;
939 unsigned u : 5;
940 };
test()941 constexpr bool test() {
942 A a {};
943 a.b += 2;
944 --a.n;
945 --a.u;
946 a.n = -a.n * 3;
947 return a.b == true && a.n == 3 && a.u == 31;
948 }
949 static_assert(test(), "");
950 }
951
952 namespace PR17615 {
953 struct A {
954 int &&r;
APR17615::A955 constexpr A(int &&r) : r(static_cast<int &&>(r)) {}
APR17615::A956 constexpr A() : A(0) {
957 (void)+r; // expected-note {{outside its lifetime}}
958 }
959 };
960 constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}}
961 }
962
963 namespace PR17331 {
964 template<typename T, unsigned int N>
sum(const T (& arr)[N])965 constexpr T sum(const T (&arr)[N]) {
966 T result = 0;
967 for (T i : arr)
968 result += i;
969 return result;
970 }
971
972 constexpr int ARR[] = { 1, 2, 3, 4, 5 };
973 static_assert(sum(ARR) == 15, "");
974 }
975
976 namespace EmptyClass {
977 struct E1 {} e1;
978 union E2 {} e2; // expected-note 4{{here}}
979 struct E3 : E1 {} e3;
980
981 template<typename E>
f(E & a,int kind)982 constexpr int f(E &a, int kind) {
983 switch (kind) {
984 case 0: { E e(a); return 0; } // expected-note {{read}} expected-note {{in call}}
985 case 1: { E e(static_cast<E&&>(a)); return 0; } // expected-note {{read}} expected-note {{in call}}
986 case 2: { E e; e = a; return 0; } // expected-note {{read}} expected-note {{in call}}
987 case 3: { E e; e = static_cast<E&&>(a); return 0; } // expected-note {{read}} expected-note {{in call}}
988 }
989 }
990 constexpr int test1 = f(e1, 0);
991 constexpr int test2 = f(e2, 0); // expected-error {{constant expression}} expected-note {{in call}}
992 constexpr int test3 = f(e3, 0);
993 constexpr int test4 = f(e1, 1);
994 constexpr int test5 = f(e2, 1); // expected-error {{constant expression}} expected-note {{in call}}
995 constexpr int test6 = f(e3, 1);
996 constexpr int test7 = f(e1, 2);
997 constexpr int test8 = f(e2, 2); // expected-error {{constant expression}} expected-note {{in call}}
998 constexpr int test9 = f(e3, 2);
999 constexpr int testa = f(e1, 3);
1000 constexpr int testb = f(e2, 3); // expected-error {{constant expression}} expected-note {{in call}}
1001 constexpr int testc = f(e3, 3);
1002 }
1003
1004 namespace SpeculativeEvalWrites {
1005 // Ensure that we don't try to speculatively evaluate writes.
f()1006 constexpr int f() {
1007 int i = 0;
1008 int a = 0;
1009 // __builtin_object_size speculatively evaluates its first argument.
1010 __builtin_object_size((i = 1, &a), 0);
1011 return i;
1012 }
1013
1014 static_assert(!f(), "");
1015 }
1016
1017 namespace PR27989 {
f(int n)1018 constexpr int f(int n) {
1019 int a = (n = 1, 0);
1020 return n;
1021 }
1022 static_assert(f(0) == 1, "");
1023 }
1024
1025 namespace const_char {
1026 template <int N>
sum(const char (& Arr)[N])1027 constexpr int sum(const char (&Arr)[N]) {
1028 int S = 0;
1029 for (unsigned I = 0; I != N; ++I)
1030 S += Arr[I]; // expected-note 2{{read of non-constexpr variable 'Cs' is not allowed}}
1031 return S;
1032 }
1033
1034 // As an extension, we support evaluating some things that are `const` as though
1035 // they were `constexpr` when folding, but it should not be allowed in normal
1036 // constexpr evaluation.
1037 const char Cs[] = {'a', 'b'}; // expected-note 2{{declared here}}
1038 void foo() __attribute__((enable_if(sum(Cs) == 'a' + 'b', "")));
run()1039 void run() { foo(); }
1040
1041 static_assert(sum(Cs) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum(Cs)'}}
1042 constexpr int S = sum(Cs); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}}
1043 }
1044
PR28739(int n)1045 constexpr void PR28739(int n) { // expected-error {{never produces a constant}}
1046 int *p = &n; // expected-note {{array 'p' declared here}}
1047 p += (__int128)(unsigned long)-1; // expected-note {{cannot refer to element 18446744073709551615 of non-array object in a constant expression}}
1048 // expected-warning@-1 {{the pointer incremented by 18446744073709551615 refers past the last possible element for an array in 64-bit address space containing 32-bit (4-byte) elements (max possible 4611686018427387904 elements)}}
1049 }
1050
Void(int n)1051 constexpr void Void(int n) {
1052 void(n + 1);
1053 void();
1054 }
1055 constexpr int void_test = (Void(0), 1);
1056
1057 namespace PR19741 {
addone(int & m)1058 constexpr void addone(int &m) { m++; }
1059
1060 struct S {
1061 int m = 0;
SPR19741::S1062 constexpr S() { addone(m); }
1063 };
evalS()1064 constexpr bool evalS() {
1065 constexpr S s;
1066 return s.m == 1;
1067 }
1068 static_assert(evalS(), "");
1069
1070 struct Nested {
1071 struct First { int x = 42; };
1072 union {
1073 First first;
1074 int second;
1075 };
1076 int x;
NestedPR19741::Nested1077 constexpr Nested(int x) : first(), x(x) { x = 4; }
NestedPR19741::Nested1078 constexpr Nested() : Nested(42) {
1079 addone(first.x);
1080 x = 3;
1081 }
1082 };
evalNested()1083 constexpr bool evalNested() {
1084 constexpr Nested N;
1085 return N.first.x == 43;
1086 }
1087 static_assert(evalNested(), "");
1088 } // namespace PR19741
1089
1090 namespace Mutable {
1091 struct A { mutable int n; }; // expected-note 2{{here}}
1092 constexpr int k = A{123}.n; // ok
1093 static_assert(k == 123, "");
1094
1095 struct Q { A &&a; int b = a.n; };
1096 constexpr Q q = { A{456} }; // expected-note {{temporary}}
1097 static_assert(q.b == 456, "");
1098 static_assert(q.a.n == 456, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
1099
1100 constexpr A a = {123};
1101 constexpr int m = a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1102
1103 constexpr Q r = { static_cast<A&&>(const_cast<A&>(a)) }; // expected-error {{constant expression}} expected-note@-8 {{mutable}}
1104
1105 struct B {
1106 mutable int n; // expected-note {{here}}
1107 int m;
BMutable::B1108 constexpr B() : n(1), m(n) {} // ok
1109 };
1110 constexpr B b;
1111 constexpr int p = b.n; // expected-error {{constant expression}} expected-note {{mutable}}
1112 }
1113
1114 namespace IndirectFields {
1115
1116 // Reference indirect field.
1117 struct A {
1118 struct {
1119 union {
1120 int x = x = 3; // cxx14-note {{outside its lifetime}}
1121 };
1122 };
AIndirectFields::A1123 constexpr A() {}
1124 };
1125 static_assert(A().x == 3, ""); // cxx14-error{{not an integral constant expression}} cxx14-note{{in call to 'A()'}}
1126
1127 // Reference another indirect field, with different 'this'.
1128 struct B {
1129 struct {
1130 union {
1131 int x = 3;
1132 };
1133 int y = x;
1134 };
BIndirectFields::B1135 constexpr B() {}
1136 };
1137 static_assert(B().y == 3, "");
1138
1139 // Nested evaluation of indirect field initializers.
1140 struct C {
1141 union {
1142 int x = 1;
1143 };
1144 };
1145 struct D {
1146 struct {
1147 C c;
1148 int y = c.x + 1;
1149 };
1150 };
1151 static_assert(D().y == 2, "");
1152
1153 // Explicit 'this'.
1154 struct E {
1155 int n = 0;
1156 struct {
1157 void *x = this;
1158 };
1159 void *y = this;
1160 };
1161 constexpr E e1 = E();
1162 static_assert(e1.x != e1.y, "");
1163 constexpr E e2 = E{0};
1164 static_assert(e2.x != e2.y, "");
1165
1166 } // namespace IndirectFields
1167
indirect_builtin_constant_p(const char * __s)1168 constexpr bool indirect_builtin_constant_p(const char *__s) {
1169 return __builtin_constant_p(*__s);
1170 }
1171 constexpr bool n = indirect_builtin_constant_p("a");
1172
1173 __attribute__((enable_if(indirect_builtin_constant_p("a") == n, "OK")))
test_in_enable_if()1174 int test_in_enable_if() { return 0; }
1175 int n2 = test_in_enable_if();
1176
1177 template <bool n = indirect_builtin_constant_p("a")>
test_in_template_param()1178 int test_in_template_param() { return 0; }
1179 int n3 = test_in_template_param();
1180
test_in_case(int n)1181 void test_in_case(int n) {
1182 switch (n) {
1183 case indirect_builtin_constant_p("abc"):
1184 break;
1185 }
1186 }
1187 enum InEnum1 {
1188 ONE = indirect_builtin_constant_p("abc")
1189 };
1190 enum InEnum2 : int {
1191 TWO = indirect_builtin_constant_p("abc")
1192 };
1193 enum class InEnum3 {
1194 THREE = indirect_builtin_constant_p("abc")
1195 };
1196
1197 // [class.ctor]p4:
1198 // A constructor can be invoked for a const, volatile or const volatile
1199 // object. const and volatile semantics are not applied on an object under
1200 // construction. They come into effect when the constructor for the most
1201 // derived object ends.
1202 namespace ObjectsUnderConstruction {
1203 struct A {
1204 int n;
AObjectsUnderConstruction::A1205 constexpr A() : n(1) { n = 2; }
1206 };
1207 struct B {
1208 const A a;
BObjectsUnderConstruction::B1209 constexpr B(bool mutate) {
1210 if (mutate)
1211 const_cast<A &>(a).n = 3; // expected-note {{modification of object of const-qualified type 'const int'}}
1212 }
1213 };
1214 constexpr B b(false);
1215 static_assert(b.a.n == 2, "");
1216 constexpr B bad(true); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'B(true)'}}
1217
1218 struct C {
1219 int n;
CObjectsUnderConstruction::C1220 constexpr C() : n(1) { n = 2; }
1221 };
f(bool get)1222 constexpr int f(bool get) {
1223 volatile C c; // expected-note {{here}}
1224 return get ? const_cast<int&>(c.n) : 0; // expected-note {{read of volatile object 'c'}}
1225 }
1226 static_assert(f(false) == 0, ""); // ok, can modify volatile c.n during c's initialization: it's not volatile then
1227 static_assert(f(true) == 2, ""); // expected-error {{constant}} expected-note {{in call}}
1228
1229 struct Aggregate {
1230 int x = 0;
1231 int y = ++x;
1232 };
1233 constexpr Aggregate aggr1;
1234 static_assert(aggr1.x == 1 && aggr1.y == 1, "");
1235 // FIXME: This is not specified by the standard, but sanity requires it.
1236 constexpr Aggregate aggr2 = {};
1237 static_assert(aggr2.x == 1 && aggr2.y == 1, "");
1238
1239 // The lifetime of 'n' begins at the initialization, not before.
1240 constexpr int n = ++const_cast<int&>(n); // expected-error {{constant expression}} expected-note {{increment of object outside its lifetime}}
1241 }
1242
1243 namespace PR39728 {
1244 struct Comment0 {
1245 Comment0 &operator=(const Comment0 &) = default;
1246 ~Comment0() = default;
1247 };
f()1248 constexpr void f() {
1249 Comment0 a;
1250 a = a;
1251 }
1252 static_assert((f(), true), "");
1253 struct Comment1 {
1254 constexpr Comment1 &operator=(const Comment1 &) = default; // OK
1255 ~Comment1() = default;
1256 };
1257 }
1258
1259 namespace TemporaryWithBadPointer {
get_bad_pointer()1260 constexpr int *get_bad_pointer() {
1261 int n = 0; // expected-note 2{{here}}
1262 return &n; // expected-warning {{stack}}
1263 }
1264 constexpr int *bad_pointer = get_bad_pointer(); // expected-error {{constant expression}} expected-note {{pointer to 'n' is not a constant expression}}
1265
1266 struct DoBadThings { int *&℘ int n; };
1267 constexpr DoBadThings dbt = { // expected-error {{constant expression}}
1268 nullptr, // expected-note {{pointer to 'n' is not a constant expression}}
1269 (dbt.wp = get_bad_pointer(), 0)
1270 };
1271
1272 constexpr DoBadThings dbt2 = { // ok
1273 get_bad_pointer(),
1274 (dbt2.wp = nullptr, 0)
1275 };
1276 }
1277