Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / constant-expression-cxx14.cpp
blob273d7ff3a208e290824fd84343d6e7a07684faa8
1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx20_23,cxx23 %s -fcxx-exceptions -triple=x86_64-linux-gnu
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20_23,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
5 struct S {
6 // dummy ctor to make this a literal type
7 constexpr S(int);
9 S();
11 int arr[10];
13 constexpr int &get(int n) { return arr[n]; }
14 constexpr const int &get(int n) const { return arr[n]; }
17 S s = S();
18 const S &sr = s;
19 static_assert(&s.get(4) - &sr.get(2) == 2, "");
21 // Compound-statements can be used in constexpr functions.
22 constexpr int e() {{{{}} return 5; }}
23 static_assert(e() == 5, "");
25 // Types can be defined in constexpr functions.
26 constexpr int f() {
27 enum E { e1, e2, e3 };
29 struct S {
30 constexpr S(E e) : e(e) {}
31 constexpr int get() { return e; }
32 E e;
35 return S(e2).get();
37 static_assert(f() == 1, "");
39 // Variables can be declared in constexpr functions.
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;
46 static_assert(g(2) == 42, "");
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++23 extension}}
50 return m;
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++23 extension}}
55 return m;
58 // if-statements can be used in constexpr functions.
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;
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, "");
78 // There can be 0 return-statements.
79 constexpr void k() {
82 // If the return type is not 'void', no return statements => never a constant
83 // expression, so still diagnose that case.
84 [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}}
85 fn();
88 // We evaluate the body of a constexpr constructor, to check for side-effects.
89 struct U {
90 constexpr U(int n) {
91 if (j(n)) {} // expected-note {{in call to 'j(2)'}}
94 constexpr U u1{1};
95 constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}}
97 // We allow expression-statements.
98 constexpr int l(bool b) {
99 if (b)
100 throw "invalid value for b!"; // expected-note {{subexpression not valid}}
101 return 5;
103 static_assert(l(false) == 5, "");
104 static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}}
106 // Potential constant expression checking is still applied where possible.
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}}
113 constexpr int maybe_htonl(bool isBigEndian, int x) {
114 if (isBigEndian)
115 return x;
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}}
122 constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}}
124 namespace NS {
125 constexpr int n = 0;
127 constexpr int namespace_alias() {
128 namespace N = NS;
129 return N::n;
132 namespace assign {
133 constexpr int a = 0;
134 const int b = 0;
135 int c = 0; // expected-note {{here}}
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}}
140 constexpr int wrap(int a, int b) {
141 set(a, b);
142 return a;
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)'}}
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'}}
154 namespace string_assign {
155 template<typename T>
156 constexpr void swap(T &a, T &b) {
157 T tmp = a;
158 a = b;
159 b = tmp;
161 template<typename Iterator>
162 constexpr void reverse(Iterator begin, Iterator end) {
163 while (begin != end && begin != --end)
164 swap(*begin++, *end);
166 template<typename Iterator1, typename Iterator2>
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;
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);
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)'}}
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}}
192 namespace array_resize {
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];
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}}
207 namespace potential_const_expr {
208 constexpr void set(int &n) { n = 1; }
209 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error
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}}
214 int n; // expected-note {{declared here}}
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'}}
221 namespace subobject {
222 union A { constexpr A() : y(5) {} int x, y; };
223 struct B { A a; };
224 struct C : B {};
225 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; };
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'}}
231 constexpr bool check(D &d) { return d.c.a.y == 3; }
232 // cxx20_23-note@-1 {{read of member 'y' of union with active member 'x'}}
234 constexpr bool g() { D d; f(d); return d.c.a.y == 3; }
235 static_assert(g(), "");
237 D d;
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}}
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}}
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_23-note@-1 {{in call to 'check(d)'}}
246 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}}
249 namespace lifetime {
250 constexpr int &&id(int &&n) { return static_cast<int&&>(n); }
251 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}}
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}}
256 namespace const_modify {
257 constexpr int modify(int &n) { return n = 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}}
258 constexpr int test1() { int k = 0; return modify(k); }
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}}
265 namespace null {
266 constexpr int test(int *p) {
267 return *p = 123; // expected-note {{assignment to dereferenced null pointer}}
269 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}}
272 namespace incdec {
273 template<typename T> constexpr T &ref(T &&r) { return r; }
274 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
275 template<typename T> constexpr T postinc(T &&r) { return (r++, r); }
276 template<typename T> constexpr T postdec(T &&r) { return (r--, r); }
278 template int &ref<int>(int &&);
279 // cxx23-note@-1 {{in instantiation of function template specialization}}
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
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);
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, "");
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
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
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, "");
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, "");
336 static_assert(++ref(1e100) == 1e100, "");
337 static_assert(--ref(1e100) == 1e100, "");
338 #endif
340 union U {
341 int a, b;
343 constexpr int f(U u) {
344 return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}}
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_23-warning@-1 {{decrement of object of volatile-qualified type 'volatile int' is deprecated}}
350 constexpr int incr(int k) {
351 int x = k;
352 if (x++ == 100)
353 return x;
354 return incr(x);
356 static_assert(incr(0) == 101, "");
359 namespace compound_assign {
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;
392 static_assert(test_int(), "");
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;
406 static_assert(test_float(), "");
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;
430 static_assert(test_bool(), "");
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;
445 static_assert(test_ptr(), "");
447 template<typename T>
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;
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
464 constexpr short test_promotion(short k) {
465 short s = k;
466 s *= s;
467 return s;
469 static_assert(test_promotion(100) == 10000, "");
470 static_assert(test_promotion(200) == -25536, "");
471 static_assert(test_promotion(256) == 0, "");
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}}
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}}
485 namespace loops {
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;
493 return f_k_plus_one;
495 static_assert(fib_loop(46) == 1836311903, "");
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;
505 int b = 0;
506 while (b != 17) {
507 ++b;
508 if (b == 6) continue;
509 if ((b % 6) == 0) break;
512 int c = 0;
513 do {
514 ++c;
515 if (c == 7) continue;
516 if ((c % 7) == 0) break;
517 } while (c != 21);
519 return a == 10 && b == 12 && c == 14;
521 static_assert(breaks_work(), "");
523 void not_constexpr();
524 constexpr bool no_cont_after_break() {
525 for (;;) {
526 break;
527 not_constexpr();
529 while (true) {
530 break;
531 not_constexpr();
533 do {
534 break;
535 not_constexpr();
536 } while (true);
537 return true;
539 static_assert(no_cont_after_break(), "");
541 constexpr bool cond() {
542 for (int a = 1; bool b = a != 3; ++a) {
543 if (!b)
544 return false;
546 while (bool b = true) {
547 b = false;
548 break;
550 return true;
552 static_assert(cond(), "");
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;
561 static_assert(range_for() == 15, "");
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...>;
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;
571 template<> struct make_ints<0> { using type = ints<>; };
572 template<> struct make_ints<1> { using type = ints<0>; };
574 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
576 template<typename T, unsigned N> struct array {
577 constexpr array() : arr{} {}
578 template<typename ...X>
579 constexpr array(X ...x) : arr{} {
580 init(typename make_ints<sizeof...(X)>::type{}, x...);
582 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
583 ignore{arr[I] = x ...};
585 T arr[N];
586 struct iterator {
587 T *p;
588 constexpr explicit iterator(T *p) : p(p) {}
589 constexpr bool operator!=(iterator o) { return p != o.p; }
590 constexpr iterator &operator++() { ++p; return *this; }
591 constexpr T &operator*() { return *p; }
593 constexpr iterator begin() { return iterator(arr); }
594 constexpr iterator end() { return iterator(arr + N); }
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;
604 return sum;
606 static_assert(range_for_2() == 10, "");
609 namespace assignment_op {
610 struct A {
611 constexpr A() : n(5) {}
612 int n;
613 struct B {
614 int k = 1;
615 union U {
616 constexpr U() : y(4) {}
617 int x;
618 int y;
619 } u;
620 } b;
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;
629 static_assert(testA(), "");
631 struct B {
632 bool assigned = false;
633 constexpr B &operator=(const B&) {
634 assigned = true;
635 return *this;
638 struct C : B {
639 B b;
640 int n = 5;
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;
649 static_assert(testC(), "");
652 namespace switch_stmt {
653 constexpr bool no_such_case(int n) {
654 switch (n) { case 1: return false; }
655 return true;
657 static_assert(no_such_case(0), "");
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;
682 return z;
683 } else if (false) case 8: z = 8;
684 else if (false) {
685 case 10:
686 z = -10;
687 break;
689 else z = 0;
690 return z;
691 default:
692 return -1;
694 return -z;
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, "");
708 // Check that we can continue an outer loop from within a switch.
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;
721 return false;
723 static_assert(contin(), "");
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: ;
733 return false;
735 static_assert(switch_into_for(), "");
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];
749 case 0: ;
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;
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), "");
772 namespace deduced_return_type {
773 constexpr auto f() { return 0; }
774 template<typename T> constexpr auto g(T t) { return t; }
775 static_assert(f() == 0, "");
776 static_assert(g(true), "");
779 namespace modify_temporary_during_construction {
780 struct A { int &&temporary; int x; int y; };
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}}
788 namespace std {
789 typedef decltype(sizeof(int)) size_t;
791 template <class _E>
792 class initializer_list
794 const _E* __begin_;
795 size_t __size_;
797 constexpr initializer_list(const _E* __b, size_t __s)
798 : __begin_(__b),
799 __size_(__s)
802 public:
803 typedef _E value_type;
804 typedef const _E& reference;
805 typedef const _E& const_reference;
806 typedef size_t size_type;
808 typedef const _E* iterator;
809 typedef const _E* const_iterator;
811 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
813 constexpr size_t size() const {return __size_;}
814 constexpr const _E* begin() const {return __begin_;}
815 constexpr const _E* end() const {return __begin_ + __size_;}
819 namespace InitializerList {
820 constexpr int sum(std::initializer_list<int> ints) {
821 int total = 0;
822 for (int n : ints) total += n;
823 return total;
825 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
828 namespace StmtExpr {
829 constexpr int f(int k) {
830 switch (k) {
831 case 0:
832 return 0;
834 ({ // expected-note {{jump enters a statement expression}}
835 case 1:// expected-error {{cannot jump from switch statement to this case label}} \
836 // expected-note {{not supported}}
837 return 1;
841 static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
843 constexpr int g() {
844 return ({ int n; n; }); // expected-note {{read of uninitialized object}}
846 static_assert(g() == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
848 // FIXME: We should handle the void statement expression case.
849 constexpr int h() { // expected-error {{never produces a constant}}
850 ({ if (true) {} }); // expected-note {{not supported}}
851 return 0;
855 namespace VirtualFromBase {
856 struct S1 {
857 virtual int f() const;
859 struct S2 {
860 virtual int f();
862 template <typename T> struct X : T {
863 constexpr X() {}
864 double d = 0.0;
865 constexpr int f() { return sizeof(T); }
868 // Non-virtual f(), OK.
869 constexpr X<X<S1>> xxs1;
870 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
871 static_assert(p->f() == sizeof(S1), "");
873 // Virtual f(), not OK.
874 constexpr X<X<S2>> xxs2;
875 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
876 static_assert(q->f() == sizeof(X<S2>), ""); // cxx14-error {{constant expression}} cxx14-note {{virtual function}}
879 namespace Lifetime {
880 constexpr int &get(int &&r) { return r; }
881 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
882 constexpr int f() {
883 int &r = get(123);
884 return r;
885 // cxx14_20-note@-1 {{read of object outside its lifetime}}
887 static_assert(f() == 123, ""); // expected-error {{constant expression}} cxx14_20-note {{in call}}
889 constexpr int g() {
890 int *p = 0;
892 int n = 0;
893 p = &n;
894 n = 42;
896 *p = 123; // expected-note {{assignment to object outside its lifetime}}
897 return *p;
899 static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}}
901 constexpr int h(int n) {
902 int *p[4] = {};
903 int &&r = 1;
904 p[0] = &r;
905 while (int a = 1) {
906 p[1] = &a;
907 for (int b = 1; int c = 1; ) {
908 p[2] = &b, p[3] = &c;
909 break;
911 break;
913 *p[n] = 0; // expected-note 3{{assignment to object outside its lifetime}}
914 return *p[n];
916 static_assert(h(0) == 0, ""); // ok, lifetime-extended
917 static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
918 static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
919 static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
921 constexpr void lifetime_versus_loops() {
922 int *p = 0;
923 for (int i = 0; i != 2; ++i) {
924 int *q = p;
925 int n = 0;
926 p = &n;
927 if (i)
928 // This modifies the 'n' from the previous iteration of the loop outside
929 // its lifetime.
930 ++*q; // expected-note {{increment of object outside its lifetime}}
933 static_assert((lifetime_versus_loops(), true), ""); // expected-error {{constant expression}} expected-note {{in call}}
936 namespace Bitfields {
937 struct A {
938 bool b : 1;
939 int n : 4;
940 unsigned u : 5;
942 constexpr bool test() {
943 A a {};
944 a.b += 2;
945 --a.n;
946 --a.u;
947 a.n = -a.n * 3;
948 return a.b == true && a.n == 3 && a.u == 31;
950 static_assert(test(), "");
953 namespace PR17615 {
954 struct A {
955 int &&r;
956 constexpr A(int &&r) : r(static_cast<int &&>(r)) {}
957 constexpr A() : A(0) {
958 (void)+r; // expected-note {{outside its lifetime}}
961 constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}}
964 namespace PR17331 {
965 template<typename T, unsigned int N>
966 constexpr T sum(const T (&arr)[N]) {
967 T result = 0;
968 for (T i : arr)
969 result += i;
970 return result;
973 constexpr int ARR[] = { 1, 2, 3, 4, 5 };
974 static_assert(sum(ARR) == 15, "");
977 namespace EmptyClass {
978 struct E1 {} e1;
979 union E2 {} e2; // expected-note 4{{here}}
980 struct E3 : E1 {} e3;
982 template<typename E>
983 constexpr int f(E &a, int kind) {
984 switch (kind) {
985 case 0: { E e(a); return 0; } // expected-note {{read}} expected-note {{in call}}
986 case 1: { E e(static_cast<E&&>(a)); return 0; } // expected-note {{read}} expected-note {{in call}}
987 case 2: { E e; e = a; return 0; } // expected-note {{read}} expected-note {{in call}}
988 case 3: { E e; e = static_cast<E&&>(a); return 0; } // expected-note {{read}} expected-note {{in call}}
991 constexpr int test1 = f(e1, 0);
992 constexpr int test2 = f(e2, 0); // expected-error {{constant expression}} expected-note {{in call}}
993 constexpr int test3 = f(e3, 0);
994 constexpr int test4 = f(e1, 1);
995 constexpr int test5 = f(e2, 1); // expected-error {{constant expression}} expected-note {{in call}}
996 constexpr int test6 = f(e3, 1);
997 constexpr int test7 = f(e1, 2);
998 constexpr int test8 = f(e2, 2); // expected-error {{constant expression}} expected-note {{in call}}
999 constexpr int test9 = f(e3, 2);
1000 constexpr int testa = f(e1, 3);
1001 constexpr int testb = f(e2, 3); // expected-error {{constant expression}} expected-note {{in call}}
1002 constexpr int testc = f(e3, 3);
1005 namespace SpeculativeEvalWrites {
1006 // Ensure that we don't try to speculatively evaluate writes.
1007 constexpr int f() {
1008 int i = 0;
1009 int a = 0;
1010 // __builtin_object_size speculatively evaluates its first argument.
1011 __builtin_object_size((i = 1, &a), 0);
1012 return i;
1015 static_assert(!f(), "");
1018 namespace PR27989 {
1019 constexpr int f(int n) {
1020 int a = (n = 1, 0);
1021 return n;
1023 static_assert(f(0) == 1, "");
1026 namespace const_char {
1027 template <int N>
1028 constexpr int sum(const char (&Arr)[N]) {
1029 int S = 0;
1030 for (unsigned I = 0; I != N; ++I)
1031 S += Arr[I]; // expected-note 2{{read of non-constexpr variable 'Cs' is not allowed}}
1032 return S;
1035 // As an extension, we support evaluating some things that are `const` as though
1036 // they were `constexpr` when folding, but it should not be allowed in normal
1037 // constexpr evaluation.
1038 const char Cs[] = {'a', 'b'}; // expected-note 2{{declared here}}
1039 void foo() __attribute__((enable_if(sum(Cs) == 'a' + 'b', "")));
1040 void run() { foo(); }
1042 static_assert(sum(Cs) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum<2>(Cs)'}}
1043 constexpr int S = sum(Cs); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}}
1046 constexpr void PR28739(int n) { // expected-error {{never produces a constant}}
1047 int *p = &n; // expected-note {{array 'p' declared here}}
1048 p += (__int128)(unsigned long)-1; // expected-note {{cannot refer to element 18446744073709551615 of non-array object in a constant expression}}
1049 // 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)}}
1052 constexpr void Void(int n) {
1053 void(n + 1);
1054 void();
1056 constexpr int void_test = (Void(0), 1);
1058 namespace PR19741 {
1059 constexpr void addone(int &m) { m++; }
1061 struct S {
1062 int m = 0;
1063 constexpr S() { addone(m); }
1065 constexpr bool evalS() {
1066 constexpr S s;
1067 return s.m == 1;
1069 static_assert(evalS(), "");
1071 struct Nested {
1072 struct First { int x = 42; };
1073 union {
1074 First first;
1075 int second;
1077 int x;
1078 constexpr Nested(int x) : first(), x(x) { x = 4; }
1079 constexpr Nested() : Nested(42) {
1080 addone(first.x);
1081 x = 3;
1084 constexpr bool evalNested() {
1085 constexpr Nested N;
1086 return N.first.x == 43;
1088 static_assert(evalNested(), "");
1089 } // namespace PR19741
1091 namespace Mutable {
1092 struct A { mutable int n; }; // expected-note 2{{here}}
1093 constexpr int k = A{123}.n; // ok
1094 static_assert(k == 123, "");
1096 struct Q { A &&a; int b = a.n; };
1097 constexpr Q q = { A{456} }; // expected-note {{temporary}}
1098 static_assert(q.b == 456, "");
1099 static_assert(q.a.n == 456, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
1101 constexpr A a = {123};
1102 constexpr int m = a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1104 constexpr Q r = { static_cast<A&&>(const_cast<A&>(a)) }; // expected-error {{constant expression}} expected-note@-8 {{mutable}}
1106 struct B {
1107 mutable int n; // expected-note {{here}}
1108 int m;
1109 constexpr B() : n(1), m(n) {} // ok
1111 constexpr B b;
1112 constexpr int p = b.n; // expected-error {{constant expression}} expected-note {{mutable}}
1115 namespace IndirectFields {
1117 // Reference indirect field.
1118 struct A {
1119 struct {
1120 union {
1121 int x = x = 3; // cxx14-note {{outside its lifetime}}
1124 constexpr A() {}
1126 static_assert(A().x == 3, ""); // cxx14-error{{not an integral constant expression}} cxx14-note{{in call to 'A()'}}
1128 // Reference another indirect field, with different 'this'.
1129 struct B {
1130 struct {
1131 union {
1132 int x = 3;
1134 int y = x;
1136 constexpr B() {}
1138 static_assert(B().y == 3, "");
1140 // Nested evaluation of indirect field initializers.
1141 struct C {
1142 union {
1143 int x = 1;
1146 struct D {
1147 struct {
1148 C c;
1149 int y = c.x + 1;
1152 static_assert(D().y == 2, "");
1154 // Explicit 'this'.
1155 struct E {
1156 int n = 0;
1157 struct {
1158 void *x = this;
1160 void *y = this;
1162 constexpr E e1 = E();
1163 static_assert(e1.x != e1.y, "");
1164 constexpr E e2 = E{0};
1165 static_assert(e2.x != e2.y, "");
1167 } // namespace IndirectFields
1169 constexpr bool indirect_builtin_constant_p(const char *__s) {
1170 return __builtin_constant_p(*__s);
1172 constexpr bool n = indirect_builtin_constant_p("a");
1174 __attribute__((enable_if(indirect_builtin_constant_p("a") == n, "OK")))
1175 int test_in_enable_if() { return 0; }
1176 int n2 = test_in_enable_if();
1178 template <bool n = indirect_builtin_constant_p("a")>
1179 int test_in_template_param() { return 0; }
1180 int n3 = test_in_template_param();
1182 void test_in_case(int n) {
1183 switch (n) {
1184 case indirect_builtin_constant_p("abc"):
1185 break;
1188 enum InEnum1 {
1189 ONE = indirect_builtin_constant_p("abc")
1191 enum InEnum2 : int {
1192 TWO = indirect_builtin_constant_p("abc")
1194 enum class InEnum3 {
1195 THREE = indirect_builtin_constant_p("abc")
1198 // [class.ctor]p4:
1199 // A constructor can be invoked for a const, volatile or const volatile
1200 // object. const and volatile semantics are not applied on an object under
1201 // construction. They come into effect when the constructor for the most
1202 // derived object ends.
1203 namespace ObjectsUnderConstruction {
1204 struct A {
1205 int n;
1206 constexpr A() : n(1) { n = 2; }
1208 struct B {
1209 const A a;
1210 constexpr B(bool mutate) {
1211 if (mutate)
1212 const_cast<A &>(a).n = 3; // expected-note {{modification of object of const-qualified type 'const int'}}
1215 constexpr B b(false);
1216 static_assert(b.a.n == 2, "");
1217 constexpr B bad(true); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'B(true)'}}
1219 struct C {
1220 int n;
1221 constexpr C() : n(1) { n = 2; }
1223 constexpr int f(bool get) {
1224 volatile C c; // expected-note {{here}}
1225 return get ? const_cast<int&>(c.n) : 0; // expected-note {{read of volatile object 'c'}}
1227 static_assert(f(false) == 0, ""); // ok, can modify volatile c.n during c's initialization: it's not volatile then
1228 static_assert(f(true) == 2, ""); // expected-error {{constant}} expected-note {{in call}}
1230 struct Aggregate {
1231 int x = 0;
1232 int y = ++x;
1234 constexpr Aggregate aggr1;
1235 static_assert(aggr1.x == 1 && aggr1.y == 1, "");
1236 // FIXME: This is not specified by the standard, but sanity requires it.
1237 constexpr Aggregate aggr2 = {};
1238 static_assert(aggr2.x == 1 && aggr2.y == 1, "");
1240 // The lifetime of 'n' begins at the initialization, not before.
1241 constexpr int n = ++const_cast<int&>(n); // expected-error {{constant expression}} expected-note {{increment of object outside its lifetime}}
1244 namespace PR39728 {
1245 struct Comment0 {
1246 Comment0 &operator=(const Comment0 &) = default;
1247 ~Comment0() = default;
1249 constexpr void f() {
1250 Comment0 a;
1251 a = a;
1253 static_assert((f(), true), "");
1254 struct Comment1 {
1255 constexpr Comment1 &operator=(const Comment1 &) = default; // OK
1256 ~Comment1() = default;
1260 namespace TemporaryWithBadPointer {
1261 constexpr int *get_bad_pointer() {
1262 int n = 0; // expected-note 2{{here}}
1263 return &n; // expected-warning {{stack}}
1265 constexpr int *bad_pointer = get_bad_pointer(); // expected-error {{constant expression}} expected-note {{pointer to 'n' is not a constant expression}}
1267 struct DoBadThings { int *&&wp; int n; };
1268 constexpr DoBadThings dbt = { // expected-error {{constant expression}}
1269 nullptr, // expected-note {{pointer to 'n' is not a constant expression}}
1270 (dbt.wp = get_bad_pointer(), 0)
1273 constexpr DoBadThings dbt2 = { // ok
1274 get_bad_pointer(),
1275 (dbt2.wp = nullptr, 0)
1279 namespace UninitCompoundAssign {
1280 constexpr int scalar(int a) {
1281 int sum; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
1282 sum += a; // expected-note {{read of uninitialized object}};
1283 return 0;
1285 static_assert(scalar(3), ""); // expected-error {{constant expression}} \
1286 // expected-note {{in call to 'scalar(3)'}}
1288 constexpr int array(int a) {
1289 int arr[3]; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
1290 arr[1] += a; // expected-note {{read of uninitialized object}};
1291 return 0;
1293 static_assert(array(3), ""); // expected-error {{constant expression}} \
1294 // expected-note {{in call to 'array(3)'}}
1296 struct Foo {
1297 int val; // cxx14-note{{member not initialized by constructor}}
1298 constexpr Foo() {} // cxx14-warning {{constexpr constructor that does not initialize all members is a C++20 extension}}
1300 constexpr int field(int a) {
1301 Foo f;
1302 f.val += a; // expected-note {{read of uninitialized object}};
1303 return 0;
1305 static_assert(field(3), ""); // expected-error {{constant expression}} \
1306 // expected-note {{in call to 'field(3)'}}