1 // RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
2
3 struct S {
4 // dummy ctor to make this a literal type
5 constexpr S(int);
6
7 S();
8
9 int arr[10];
10
getS11 constexpr int &get(int n) { return arr[n]; }
getS12 constexpr const int &get(int n) const { return arr[n]; }
13 };
14
15 S s = S();
16 const S &sr = s;
17 static_assert(&s.get(4) - &sr.get(2) == 2, "");
18
19 // Compound-statements can be used in constexpr functions.
e()20 constexpr int e() {{{{}} return 5; }}
21 static_assert(e() == 5, "");
22
23 // Types can be defined in constexpr functions.
f()24 constexpr int f() {
25 enum E { e1, e2, e3 };
26
27 struct S {
28 constexpr S(E e) : e(e) {}
29 constexpr int get() { return e; }
30 E e;
31 };
32
33 return S(e2).get();
34 }
35 static_assert(f() == 1, "");
36
37 // Variables can be declared in constexpr functions.
g(int k)38 constexpr int g(int k) {
39 const int n = 9;
40 int k2 = k * k;
41 int k3 = k2 * k;
42 return 3 * k3 + 5 * k2 + n * k - 20;
43 }
44 static_assert(g(2) == 42, "");
h(int n)45 constexpr int h(int n) {
46 static const int m = n; // expected-error {{static variable not permitted in a constexpr function}}
47 return m;
48 }
i(int n)49 constexpr int i(int n) {
50 thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}}
51 return m;
52 }
53
54 // if-statements can be used in constexpr functions.
j(int k)55 constexpr int j(int k) {
56 if (k == 5)
57 return 1;
58 if (k == 1)
59 return 5;
60 else {
61 if (int n = 2 * k - 4) {
62 return n + 1;
63 return 2;
64 }
65 }
66 } // expected-note 2{{control reached end of constexpr function}}
67 static_assert(j(0) == -3, "");
68 static_assert(j(1) == 5, "");
69 static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}}
70 static_assert(j(3) == 3, "");
71 static_assert(j(4) == 5, "");
72 static_assert(j(5) == 1, "");
73
74 // There can be 0 return-statements.
k()75 constexpr void k() {
76 }
77
78 // If the return type is not 'void', no return statements => never a constant
79 // expression, so still diagnose that case.
fn()80 [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}}
81 fn();
82 }
83
84 // We evaluate the body of a constexpr constructor, to check for side-effects.
85 struct U {
UU86 constexpr U(int n) {
87 if (j(n)) {} // expected-note {{in call to 'j(2)'}}
88 }
89 };
90 constexpr U u1{1};
91 constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}}
92
93 // We allow expression-statements.
l(bool b)94 constexpr int l(bool b) {
95 if (b)
96 throw "invalid value for b!"; // expected-note {{subexpression not valid}}
97 return 5;
98 }
99 static_assert(l(false) == 5, "");
100 static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}}
101
102 // Potential constant expression checking is still applied where possible.
htonl(int x)103 constexpr int htonl(int x) { // expected-error {{never produces a constant expression}}
104 typedef unsigned char uchar;
105 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) };
106 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
107 }
108
maybe_htonl(bool isBigEndian,int x)109 constexpr int maybe_htonl(bool isBigEndian, int x) {
110 if (isBigEndian)
111 return x;
112
113 typedef unsigned char uchar;
114 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) };
115 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
116 }
117
118 constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}}
119
120 namespace NS {
121 constexpr int n = 0;
122 }
namespace_alias()123 constexpr int namespace_alias() {
124 namespace N = NS;
125 return N::n;
126 }
127
128 namespace assign {
129 constexpr int a = 0;
130 const int b = 0;
131 int c = 0; // expected-note {{here}}
132
set(const int & a,int b)133 constexpr void set(const int &a, int b) {
134 const_cast<int&>(a) = b; // expected-note 3{{constant expression cannot modify an object that is visible outside that expression}}
135 }
wrap(int a,int b)136 constexpr int wrap(int a, int b) {
137 set(a, b);
138 return a;
139 }
140
141 static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}}
142 static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}}
143 static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(c, 1)'}}
144
145 static_assert(wrap(a, 1) == 1, "");
146 static_assert(wrap(b, 1) == 1, "");
147 static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
148 }
149
150 namespace string_assign {
151 template<typename T>
swap(T & a,T & b)152 constexpr void swap(T &a, T &b) {
153 T tmp = a;
154 a = b;
155 b = tmp;
156 }
157 template<typename Iterator>
reverse(Iterator begin,Iterator end)158 constexpr void reverse(Iterator begin, Iterator end) {
159 while (begin != end && begin != --end)
160 swap(*begin++, *end);
161 }
162 template<typename Iterator1, typename Iterator2>
equal(Iterator1 a,Iterator1 ae,Iterator2 b,Iterator2 be)163 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) {
164 while (a != ae && b != be)
165 if (*a++ != *b++)
166 return false;
167 return a == ae && b == be;
168 }
test1(int n)169 constexpr bool test1(int n) {
170 char stuff[100] = "foobarfoo";
171 const char stuff2[100] = "oofraboof";
172 reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}}
173 return equal(stuff, stuff + n, stuff2, stuff2 + n);
174 }
175 static_assert(!test1(1), "");
176 static_assert(test1(3), "");
177 static_assert(!test1(6), "");
178 static_assert(test1(9), "");
179 static_assert(!test1(100), "");
180 static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}}
181
182 // FIXME: We should be able to reject this before it's called
f()183 constexpr void f() {
184 char foo[10] = { "z" }; // expected-note {{here}}
185 foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}}
186 }
187 constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}}
188 }
189
190 namespace array_resize {
do_stuff(int k1,int k2)191 constexpr int do_stuff(int k1, int k2) {
192 int arr[1234] = { 1, 2, 3, 4 };
193 arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}}
194 return arr[k2];
195 }
196 static_assert(do_stuff(1, 2) == 3, "");
197 static_assert(do_stuff(0, 0) == 5, "");
198 static_assert(do_stuff(1233, 1233) == 5, "");
199 static_assert(do_stuff(1233, 0) == 1, "");
200 static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
201 static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
202 static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
203 }
204
205 namespace potential_const_expr {
set(int & n)206 constexpr void set(int &n) { n = 1; }
div_zero_1()207 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error
div_zero_2()208 constexpr int div_zero_2() { // expected-error {{never produces a constant expression}}
209 int z = 0;
210 return 100 / (set(z), 0); // expected-note {{division by zero}}
211 }
212 int n; // expected-note {{declared here}}
ref()213 constexpr int ref() { // expected-error {{never produces a constant expression}}
214 int &r = n;
215 return r; // expected-note {{read of non-const variable 'n'}}
216 }
217 }
218
219 namespace subobject {
A()220 union A { constexpr A() : y(5) {} int x, y; };
221 struct B { A a; };
222 struct C : B {};
D(int n)223 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; };
f(D & d)224 constexpr void f(D &d) {
225 d.c.a.y = 3;
226 // expected-note@-1 {{cannot modify an object that is visible outside}}
227 // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}}
228 }
check(D & d)229 constexpr bool check(D &d) { return d.c.a.y == 3; }
230
g()231 constexpr bool g() { D d; f(d); return d.c.a.y == 3; }
232 static_assert(g(), "");
233
234 D d;
h()235 constexpr bool h() { f(d); return check(d); } // expected-note {{in call}}
236 static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}}
237
i()238 constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}}
239 static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}}
240
j()241 constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // expected-note {{assignment to member 'x' of union with active member 'y'}}
242 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}}
243 }
244
245 namespace lifetime {
id(int && n)246 constexpr int &&id(int &&n) { return static_cast<int&&>(n); }
dead()247 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}}
bad()248 constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}}
249 static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}}
250 }
251
252 namespace const_modify {
modify(int & n)253 constexpr int modify(int &n) { return n = 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}}
test1()254 constexpr int test1() { int k = 0; return modify(k); }
test2()255 constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note 2 {{in call}}
256 static_assert(test1() == 1, "");
257 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
258 constexpr int i = test2(); // expected-error {{constant expression}} expected-note {{in call}}
259 }
260
261 namespace null {
test(int * p)262 constexpr int test(int *p) {
263 return *p = 123; // expected-note {{assignment to dereferenced null pointer}}
264 }
265 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}}
266 }
267
268 namespace incdec {
ref(T && r)269 template<typename T> constexpr T &ref(T &&r) { return r; }
postinc(T && r)270 template<typename T> constexpr T postinc(T &&r) { return (r++, r); }
postdec(T && r)271 template<typename T> constexpr T postdec(T &&r) { return (r--, r); }
272
273 static_assert(++ref(0) == 1, "");
274 static_assert(ref(0)++ == 0, "");
275 static_assert(postinc(0) == 1, "");
276 static_assert(--ref(0) == -1, "");
277 static_assert(ref(0)-- == 0, "");
278 static_assert(postdec(0) == -1, "");
279
280 constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}}
281 constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++;
282 constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}}
283 constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe);
284
285 // inc/dec on short can't overflow because we promote to int first
286 static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, "");
287 static_assert(--ref<short>(0x8000) == 0x7fff, "");
288
289 // inc on bool sets to true
290 static_assert(++ref(false), ""); // expected-warning {{deprecated}}
291 static_assert(++ref(true), ""); // expected-warning {{deprecated}}
292
293 int arr[10];
294 static_assert(++ref(&arr[0]) == &arr[1], "");
295 static_assert(++ref(&arr[9]) == &arr[10], "");
296 static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
297 static_assert(ref(&arr[0])++ == &arr[0], "");
298 static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
299 static_assert(postinc(&arr[0]) == &arr[1], "");
300 static_assert(--ref(&arr[10]) == &arr[9], "");
301 static_assert(--ref(&arr[1]) == &arr[0], "");
302 static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
303 static_assert(ref(&arr[1])-- == &arr[1], "");
304 static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
305 static_assert(postdec(&arr[1]) == &arr[0], "");
306
307 int x;
308 static_assert(++ref(&x) == &x + 1, "");
309
310 static_assert(++ref(0.0) == 1.0, "");
311 static_assert(ref(0.0)++ == 0.0, "");
312 static_assert(postinc(0.0) == 1.0, "");
313 static_assert(--ref(0.0) == -1.0, "");
314 static_assert(ref(0.0)-- == 0.0, "");
315 static_assert(postdec(0.0) == -1.0, "");
316
317 static_assert(++ref(1e100) == 1e100, "");
318 static_assert(--ref(1e100) == 1e100, "");
319
320 union U {
321 int a, b;
322 };
f(U u)323 constexpr int f(U u) {
324 return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}}
325 }
326 constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}}
327 constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}}
328
incr(int k)329 constexpr int incr(int k) {
330 int x = k;
331 if (x++ == 100)
332 return x;
333 return incr(x);
334 }
335 static_assert(incr(0) == 101, "");
336 }
337
338 namespace compound_assign {
test_int()339 constexpr bool test_int() {
340 int a = 3;
341 a += 6;
342 if (a != 9) return false;
343 a -= 2;
344 if (a != 7) return false;
345 a *= 3;
346 if (a != 21) return false;
347 if (&(a /= 10) != &a) return false;
348 if (a != 2) return false;
349 a <<= 3;
350 if (a != 16) return false;
351 a %= 6;
352 if (a != 4) return false;
353 a >>= 1;
354 if (a != 2) return false;
355 a ^= 10;
356 if (a != 8) return false;
357 a |= 5;
358 if (a != 13) return false;
359 a &= 14;
360 if (a != 12) return false;
361 return true;
362 }
363 static_assert(test_int(), "");
364
test_float()365 constexpr bool test_float() {
366 float f = 123.;
367 f *= 2;
368 if (f != 246.) return false;
369 if ((f -= 0.5) != 245.5) return false;
370 if (f != 245.5) return false;
371 f /= 0.5;
372 if (f != 491.) return false;
373 f += -40;
374 if (f != 451.) return false;
375 return true;
376 }
377 static_assert(test_float(), "");
378
test_ptr()379 constexpr bool test_ptr() {
380 int arr[123] = {};
381 int *p = arr;
382 if ((p += 4) != &arr[4]) return false;
383 if (p != &arr[4]) return false;
384 p += -1;
385 if (p != &arr[3]) return false;
386 if ((p -= -10) != &arr[13]) return false;
387 if (p != &arr[13]) return false;
388 p -= 11;
389 if (p != &arr[2]) return false;
390 return true;
391 }
392 static_assert(test_ptr(), "");
393
394 template<typename T>
test_overflow()395 constexpr bool test_overflow() {
396 T a = 1;
397 while (a != a / 2)
398 a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }} expected-note {{floating point arithmetic produces an infinity}}
399 return true;
400 }
401
402 static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}}
403 static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined
404 static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication
405 static_assert(test_overflow<unsigned short>(), ""); // ok
406 static_assert(test_overflow<unsigned long long>(), ""); // ok
407 static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}}
408 static_assert(test_overflow<float>(), ""); // expected-error {{constant}} expected-note {{call}}
409
test_promotion(short k)410 constexpr short test_promotion(short k) {
411 short s = k;
412 s *= s;
413 return s;
414 }
415 static_assert(test_promotion(100) == 10000, "");
416 static_assert(test_promotion(200) == -25536, "");
417 static_assert(test_promotion(256) == 0, "");
418
test_bounds(const char * p,int o)419 constexpr const char *test_bounds(const char *p, int o) {
420 return p += o; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}}
421 }
422 static_assert(test_bounds("foo", 0)[0] == 'f', "");
423 static_assert(test_bounds("foo", 3)[0] == 0, "");
424 static_assert(test_bounds("foo", 4)[-3] == 'o', "");
425 static_assert(test_bounds("foo" + 4, -4)[0] == 'f', "");
426 static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}}
427 static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}}
428 static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}}
429 }
430
431 namespace loops {
fib_loop(int a)432 constexpr int fib_loop(int a) {
433 int f_k = 0, f_k_plus_one = 1;
434 for (int k = 1; k != a; ++k) {
435 int f_k_plus_two = f_k + f_k_plus_one;
436 f_k = f_k_plus_one;
437 f_k_plus_one = f_k_plus_two;
438 }
439 return f_k_plus_one;
440 }
441 static_assert(fib_loop(46) == 1836311903, "");
442
breaks_work()443 constexpr bool breaks_work() {
444 int a = 0;
445 for (int n = 0; n != 100; ++n) {
446 ++a;
447 if (a == 5) continue;
448 if ((a % 5) == 0) break;
449 }
450
451 int b = 0;
452 while (b != 17) {
453 ++b;
454 if (b == 6) continue;
455 if ((b % 6) == 0) break;
456 }
457
458 int c = 0;
459 do {
460 ++c;
461 if (c == 7) continue;
462 if ((c % 7) == 0) break;
463 } while (c != 21);
464
465 return a == 10 && b == 12 & c == 14;
466 }
467 static_assert(breaks_work(), "");
468
469 void not_constexpr();
no_cont_after_break()470 constexpr bool no_cont_after_break() {
471 for (;;) {
472 break;
473 not_constexpr();
474 }
475 while (true) {
476 break;
477 not_constexpr();
478 }
479 do {
480 break;
481 not_constexpr();
482 } while (true);
483 return true;
484 }
485 static_assert(no_cont_after_break(), "");
486
cond()487 constexpr bool cond() {
488 for (int a = 1; bool b = a != 3; ++a) {
489 if (!b)
490 return false;
491 }
492 while (bool b = true) {
493 b = false;
494 break;
495 }
496 return true;
497 }
498 static_assert(cond(), "");
499
range_for()500 constexpr int range_for() {
501 int arr[] = { 1, 2, 3, 4, 5 };
502 int sum = 0;
503 for (int x : arr)
504 sum += x;
505 return sum;
506 }
507 static_assert(range_for() == 15, "");
508
509 template<int...N> struct ints {};
510 template<typename A, typename B> struct join_ints;
511 template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
512 using type = ints<As..., sizeof...(As) + Bs...>;
513 };
514 template<unsigned N> struct make_ints {
515 using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
516 };
517 template<> struct make_ints<0> { using type = ints<>; };
518 template<> struct make_ints<1> { using type = ints<0>; };
519
ignoreloops::ignore520 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
521
522 template<typename T, unsigned N> struct array {
arrayloops::array523 constexpr array() : arr{} {}
524 template<typename ...X>
arrayloops::array525 constexpr array(X ...x) : arr{} {
526 init(typename make_ints<sizeof...(X)>::type{}, x...);
527 }
initloops::array528 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
529 ignore{arr[I] = x ...};
530 }
531 T arr[N];
532 struct iterator {
533 T *p;
iteratorloops::array::iterator534 constexpr explicit iterator(T *p) : p(p) {}
operator !=loops::array::iterator535 constexpr bool operator!=(iterator o) { return p != o.p; }
operator ++loops::array::iterator536 constexpr iterator &operator++() { ++p; return *this; }
operator *loops::array::iterator537 constexpr T &operator*() { return *p; }
538 };
beginloops::array539 constexpr iterator begin() { return iterator(arr); }
endloops::array540 constexpr iterator end() { return iterator(arr + N); }
541 };
542
range_for_2()543 constexpr int range_for_2() {
544 array<int, 5> arr { 1, 2, 3, 4, 5 };
545 int sum = 0;
546 for (int k : arr) {
547 sum += k;
548 if (sum > 8) break;
549 }
550 return sum;
551 }
552 static_assert(range_for_2() == 10, "");
553 }
554
555 namespace assignment_op {
556 struct A {
Aassignment_op::A557 constexpr A() : n(5) {}
558 int n;
559 struct B {
560 int k = 1;
561 union U {
U()562 constexpr U() : y(4) {}
563 int x;
564 int y;
565 } u;
566 } b;
567 };
testA()568 constexpr bool testA() {
569 A a, b;
570 a.n = 7;
571 a.b.u.y = 5;
572 b = a;
573 return b.n == 7 && b.b.u.y == 5 && b.b.k == 1;
574 }
575 static_assert(testA(), "");
576
577 struct B {
578 bool assigned = false;
operator =assignment_op::B579 constexpr B &operator=(const B&) {
580 assigned = true;
581 return *this;
582 }
583 };
584 struct C : B {
585 B b;
586 int n = 5;
587 };
testC()588 constexpr bool testC() {
589 C c, d;
590 c.n = 7;
591 d = c;
592 c.n = 3;
593 return d.n == 7 && d.assigned && d.b.assigned;
594 }
595 static_assert(testC(), "");
596 }
597
598 namespace switch_stmt {
f(char k)599 constexpr int f(char k) {
600 bool b = false;
601 int z = 6;
602 switch (k) {
603 return -1;
604 case 0:
605 if (false) {
606 case 1:
607 z = 1;
608 for (; b;) {
609 return 5;
610 while (0)
611 case 2: return 2;
612 case 7: z = 7;
613 do case 6: {
614 return z;
615 if (false)
616 case 3: return 3;
617 case 4: z = 4;
618 } while (1);
619 case 5: b = true;
620 case 9: z = 9;
621 }
622 return z;
623 } else if (false) case 8: z = 8;
624 else if (false) {
625 case 10:
626 z = -10;
627 break;
628 }
629 else z = 0;
630 return z;
631 default:
632 return -1;
633 }
634 return -z;
635 }
636 static_assert(f(0) == 0, "");
637 static_assert(f(1) == 1, "");
638 static_assert(f(2) == 2, "");
639 static_assert(f(3) == 3, "");
640 static_assert(f(4) == 4, "");
641 static_assert(f(5) == 5, "");
642 static_assert(f(6) == 6, "");
643 static_assert(f(7) == 7, "");
644 static_assert(f(8) == 8, "");
645 static_assert(f(9) == 9, "");
646 static_assert(f(10) == 10, "");
647
648 // Check that we can continue an outer loop from within a switch.
contin()649 constexpr bool contin() {
650 for (int n = 0; n != 10; ++n) {
651 switch (n) {
652 case 0:
653 ++n;
654 continue;
655 case 1:
656 return false;
657 case 2:
658 return true;
659 }
660 }
661 return false;
662 }
663 static_assert(contin(), "");
664
switch_into_for()665 constexpr bool switch_into_for() {
666 int n = 0;
667 switch (n) {
668 for (; n == 1; ++n) {
669 return n == 1;
670 case 0: ;
671 }
672 }
673 return false;
674 }
675 static_assert(switch_into_for(), "");
676
duff_copy(char * a,const char * b,int n)677 constexpr void duff_copy(char *a, const char *b, int n) {
678 switch ((n - 1) % 8 + 1) {
679 for ( ; n; n = (n - 1) & ~7) {
680 case 8: a[n-8] = b[n-8];
681 case 7: a[n-7] = b[n-7];
682 case 6: a[n-6] = b[n-6];
683 case 5: a[n-5] = b[n-5];
684 case 4: a[n-4] = b[n-4];
685 case 3: a[n-3] = b[n-3];
686 case 2: a[n-2] = b[n-2];
687 case 1: a[n-1] = b[n-1];
688 }
689 case 0: ;
690 }
691 }
692
test_copy(const char * str,int n)693 constexpr bool test_copy(const char *str, int n) {
694 char buffer[16] = {};
695 duff_copy(buffer, str, n);
696 for (int i = 0; i != sizeof(buffer); ++i)
697 if (buffer[i] != (i < n ? str[i] : 0))
698 return false;
699 return true;
700 }
701 static_assert(test_copy("foo", 0), "");
702 static_assert(test_copy("foo", 1), "");
703 static_assert(test_copy("foo", 2), "");
704 static_assert(test_copy("hello world", 0), "");
705 static_assert(test_copy("hello world", 7), "");
706 static_assert(test_copy("hello world", 8), "");
707 static_assert(test_copy("hello world", 9), "");
708 static_assert(test_copy("hello world", 10), "");
709 static_assert(test_copy("hello world", 10), "");
710 }
711
712 namespace deduced_return_type {
f()713 constexpr auto f() { return 0; }
g(T t)714 template<typename T> constexpr auto g(T t) { return t; }
715 static_assert(f() == 0, "");
716 static_assert(g(true), "");
717 }
718
719 namespace modify_temporary_during_construction {
720 struct A { int &&temporary; int x; int y; };
f(int & r)721 constexpr int f(int &r) { r *= 9; return r - 12; }
722 constexpr A a = { 6, f(a.temporary), a.temporary }; // expected-note {{temporary created here}}
723 static_assert(a.x == 42, "");
724 static_assert(a.y == 54, "");
725 constexpr int k = a.temporary++; // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
726 }
727
728 namespace std {
729 typedef decltype(sizeof(int)) size_t;
730
731 template <class _E>
732 class initializer_list
733 {
734 const _E* __begin_;
735 size_t __size_;
736
initializer_list(const _E * __b,size_t __s)737 constexpr initializer_list(const _E* __b, size_t __s)
738 : __begin_(__b),
739 __size_(__s)
740 {}
741
742 public:
743 typedef _E value_type;
744 typedef const _E& reference;
745 typedef const _E& const_reference;
746 typedef size_t size_type;
747
748 typedef const _E* iterator;
749 typedef const _E* const_iterator;
750
initializer_list()751 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
752
size() const753 constexpr size_t size() const {return __size_;}
begin() const754 constexpr const _E* begin() const {return __begin_;}
end() const755 constexpr const _E* end() const {return __begin_ + __size_;}
756 };
757 }
758
759 namespace InitializerList {
sum(std::initializer_list<int> ints)760 constexpr int sum(std::initializer_list<int> ints) {
761 int total = 0;
762 for (int n : ints) total += n;
763 return total;
764 }
765 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
766 }
767
768 namespace StmtExpr {
f(int k)769 constexpr int f(int k) {
770 switch (k) {
771 case 0:
772 return 0;
773
774 ({
775 case 1: // expected-note {{not supported}}
776 return 1;
777 });
778 }
779 }
780 static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
781
g()782 constexpr int g() { // expected-error {{never produces a constant}}
783 return ({ int n; n; }); // expected-note {{object of type 'int' is not initialized}}
784 }
785
786 // FIXME: We should handle the void statement expression case.
h()787 constexpr int h() { // expected-error {{never produces a constant}}
788 ({ if (true) {} }); // expected-note {{not supported}}
789 return 0;
790 }
791 }
792
793 namespace VirtualFromBase {
794 struct S1 {
795 virtual int f() const;
796 };
797 struct S2 {
798 virtual int f();
799 };
800 template <typename T> struct X : T {
XVirtualFromBase::X801 constexpr X() {}
802 double d = 0.0;
fVirtualFromBase::X803 constexpr int f() { return sizeof(T); }
804 };
805
806 // Non-virtual f(), OK.
807 constexpr X<X<S1>> xxs1;
808 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
809 static_assert(p->f() == sizeof(S1), "");
810
811 // Virtual f(), not OK.
812 constexpr X<X<S2>> xxs2;
813 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
814 static_assert(q->f() == sizeof(X<S2>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
815 }
816
817 namespace Lifetime {
get(int && r)818 constexpr int &get(int &&r) { return r; }
f()819 constexpr int f() {
820 int &r = get(123);
821 return r; // expected-note {{read of object outside its lifetime}}
822 }
823 static_assert(f() == 123, ""); // expected-error {{constant expression}} expected-note {{in call}}
824
g()825 constexpr int g() {
826 int *p = 0;
827 {
828 int n = 0;
829 p = &n;
830 n = 42;
831 }
832 *p = 123; // expected-note {{assignment to object outside its lifetime}}
833 return *p;
834 }
835 static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}}
836
h(int n)837 constexpr int h(int n) {
838 int *p[4] = {};
839 int &&r = 1;
840 p[0] = &r;
841 while (int a = 1) {
842 p[1] = &a;
843 for (int b = 1; int c = 1; ) {
844 p[2] = &b, p[3] = &c;
845 break;
846 }
847 break;
848 }
849 *p[n] = 0; // expected-note 3{{assignment to object outside its lifetime}}
850 return *p[n];
851 }
852 static_assert(h(0) == 0, ""); // ok, lifetime-extended
853 static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
854 static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
855 static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
856
857 // FIXME: This function should be treated as non-constant.
lifetime_versus_loops()858 constexpr void lifetime_versus_loops() {
859 int *p = 0;
860 for (int i = 0; i != 2; ++i) {
861 int *q = p;
862 int n = 0;
863 p = &n;
864 if (i)
865 // This modifies the 'n' from the previous iteration of the loop outside
866 // its lifetime.
867 ++*q;
868 }
869 }
870 static_assert((lifetime_versus_loops(), true), "");
871 }
872
873 namespace Bitfields {
874 struct A {
875 bool b : 3;
876 int n : 4;
877 unsigned u : 5;
878 };
test()879 constexpr bool test() {
880 A a {};
881 a.b += 2;
882 --a.n;
883 --a.u;
884 a.n = -a.n * 3;
885 return a.b == false && a.n == 3 && a.u == 31;
886 }
887 static_assert(test(), "");
888 }
889
890 namespace PR17615 {
891 struct A {
892 int &&r;
APR17615::A893 constexpr A(int &&r) : r(static_cast<int &&>(r)) {}
APR17615::A894 constexpr A() : A(0) {
895 (void)+r; // expected-note {{outside its lifetime}}
896 }
897 };
898 constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}}
899 }
900
901 namespace PR17331 {
902 template<typename T, unsigned int N>
sum(const T (& arr)[N])903 constexpr T sum(const T (&arr)[N]) {
904 T result = 0;
905 for (T i : arr)
906 result += i;
907 return result;
908 }
909
910 constexpr int ARR[] = { 1, 2, 3, 4, 5 };
911 static_assert(sum(ARR) == 15, "");
912 }
913
914 namespace EmptyClass {
915 struct E1 {} e1;
916 union E2 {} e2; // expected-note 4{{here}}
917 struct E3 : E1 {} e3;
918
919 template<typename E>
f(E & a,int kind)920 constexpr int f(E &a, int kind) {
921 switch (kind) {
922 case 0: { E e(a); return 0; } // expected-note {{read}} expected-note {{in call}}
923 case 1: { E e(static_cast<E&&>(a)); return 0; } // expected-note {{read}} expected-note {{in call}}
924 case 2: { E e; e = a; return 0; } // expected-note {{read}} expected-note {{in call}}
925 case 3: { E e; e = static_cast<E&&>(a); return 0; } // expected-note {{read}} expected-note {{in call}}
926 }
927 }
928 constexpr int test1 = f(e1, 0);
929 constexpr int test2 = f(e2, 0); // expected-error {{constant expression}} expected-note {{in call}}
930 constexpr int test3 = f(e3, 0);
931 constexpr int test4 = f(e1, 1);
932 constexpr int test5 = f(e2, 1); // expected-error {{constant expression}} expected-note {{in call}}
933 constexpr int test6 = f(e3, 1);
934 constexpr int test7 = f(e1, 2);
935 constexpr int test8 = f(e2, 2); // expected-error {{constant expression}} expected-note {{in call}}
936 constexpr int test9 = f(e3, 2);
937 constexpr int testa = f(e1, 3);
938 constexpr int testb = f(e2, 3); // expected-error {{constant expression}} expected-note {{in call}}
939 constexpr int testc = f(e3, 3);
940 }
941