blob: 0d134e3cc817b2d4b6b2a9025b6829592e0fd6de [file] [log] [blame]
Richard Smithd9f663b2013-04-22 15:31:51 +00001// RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
2
3struct S {
4 // dummy ctor to make this a literal type
5 constexpr S(int);
6
7 S();
8
9 int arr[10];
10
11 constexpr int &get(int n) { return arr[n]; }
12 constexpr const int &get(int n) const { return arr[n]; }
13};
14
15S s = S();
16const S &sr = s;
17static_assert(&s.get(4) - &sr.get(2) == 2, "");
18
19// Compound-statements can be used in constexpr functions.
20constexpr int e() {{{{}} return 5; }}
21static_assert(e() == 5, "");
22
23// Types can be defined in constexpr functions.
24constexpr 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}
35static_assert(f() == 1, "");
36
37// Variables can be declared in constexpr functions.
38constexpr 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}
44static_assert(g(2) == 42, "");
45constexpr int h(int n) {
46 static const int m = n; // expected-error {{static variable not permitted in a constexpr function}}
47 return m;
48}
49constexpr 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.
55constexpr 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}}
67static_assert(j(0) == -3, "");
68static_assert(j(1) == 5, "");
69static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}}
70static_assert(j(3) == 3, "");
71static_assert(j(4) == 5, "");
72static_assert(j(5) == 1, "");
73
74// There can be 0 return-statements.
75constexpr 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.
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.
85struct U {
86 constexpr U(int n) {
87 if (j(n)) {} // expected-note {{in call to 'j(2)'}}
88 }
89};
90constexpr U u1{1};
91constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}}
92
93// We allow expression-statements.
94constexpr int l(bool b) {
95 if (b)
96 throw "invalid value for b!"; // expected-note {{subexpression not valid}}
97 return 5;
98}
99static_assert(l(false) == 5, "");
100static_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.
103constexpr 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
109constexpr 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
118constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}}
119
120namespace NS {
121 constexpr int n = 0;
122}
123constexpr int namespace_alias() {
124 namespace N = NS;
125 return N::n;
126}
Richard Smith3da88fa2013-04-26 14:36:30 +0000127
128namespace assign {
129 constexpr int a = 0;
130 const int b = 0;
131 int c = 0; // expected-note 2{{here}}
132
133 constexpr void set(const int &a, int b) {
134 const_cast<int&>(a) = b; // expected-note 2{{constant expression cannot modify an object that is visible outside that expression}}
135 }
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 {{read of non-const variable 'c'}}
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
150namespace string_assign {
151 template<typename T>
152 constexpr void swap(T &a, T &b) {
153 T tmp = a;
154 a = b;
155 b = tmp;
156 }
157 template<typename Iterator>
158 constexpr void reverse(Iterator begin, Iterator end) {
Richard Smith3da88fa2013-04-26 14:36:30 +0000159 while (begin != end && begin != --end)
160 swap(*begin++, *end);
Richard Smith3da88fa2013-04-26 14:36:30 +0000161 }
162 template<typename Iterator1, typename Iterator2>
163 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) {
Richard Smith4e18ca52013-05-06 05:56:11 +0000164 while (a != ae && b != be)
Richard Smith243ef902013-05-05 23:31:59 +0000165 if (*a++ != *b++)
Richard Smith3da88fa2013-04-26 14:36:30 +0000166 return false;
Richard Smith3da88fa2013-04-26 14:36:30 +0000167 return a == ae && b == be;
168 }
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
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
190namespace array_resize {
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
205namespace potential_const_expr {
206 constexpr void set(int &n) { n = 1; }
207 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error
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 }
Richard Smith3229b742013-05-05 21:17:10 +0000212 int n; // expected-note {{declared here}}
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 }
Richard Smith3da88fa2013-04-26 14:36:30 +0000217}
218
219namespace subobject {
220 union A { constexpr A() : y(5) {} int x, y; };
221 struct B { A a; };
222 struct C : B {};
223 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; };
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 }
229 constexpr bool check(D &d) { return d.c.a.y == 3; }
230
231 constexpr bool g() { D d; f(d); return d.c.a.y == 3; }
232 static_assert(g(), "");
233
234 D d;
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
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
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
245namespace lifetime {
246 constexpr int &&id(int &&n) { return static_cast<int&&>(n); }
247 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}}
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
252namespace const_modify {
253 constexpr int modify(int &n) { return n = 1; } // expected-note {{modification of object of const-qualified type 'const int'}}
254 constexpr int test1() { int k = 0; return modify(k); }
255 constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note {{in call}}
256 static_assert(test1() == 1, "");
257 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
258}
259
260namespace null {
261 constexpr int test(int *p) {
262 return *p = 123; // expected-note {{assignment to dereferenced null pointer}}
263 }
264 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}}
265}
Richard Smith243ef902013-05-05 23:31:59 +0000266
267namespace incdec {
268 template<typename T> constexpr T &ref(T &&r) { return r; }
269 template<typename T> constexpr T postinc(T &&r) { return (r++, r); }
270 template<typename T> constexpr T postdec(T &&r) { return (r--, r); }
271
272 static_assert(++ref(0) == 1, "");
273 static_assert(ref(0)++ == 0, "");
274 static_assert(postinc(0) == 1, "");
275 static_assert(--ref(0) == -1, "");
276 static_assert(ref(0)-- == 0, "");
277 static_assert(postdec(0) == -1, "");
278
279 constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}}
280 constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++;
281 constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}}
282 constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe);
283
284 // inc/dec on short can't overflow because we promote to int first
285 static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, "");
286 static_assert(--ref<short>(0x8000) == 0x7fff, "");
287
288 // inc on bool sets to true
289 static_assert(++ref(false), ""); // expected-warning {{deprecated}}
290 static_assert(++ref(true), ""); // expected-warning {{deprecated}}
291
292 int arr[10];
293 static_assert(++ref(&arr[0]) == &arr[1], "");
294 static_assert(++ref(&arr[9]) == &arr[10], "");
295 static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
296 static_assert(ref(&arr[0])++ == &arr[0], "");
297 static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
298 static_assert(postinc(&arr[0]) == &arr[1], "");
299 static_assert(--ref(&arr[10]) == &arr[9], "");
300 static_assert(--ref(&arr[1]) == &arr[0], "");
301 static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
302 static_assert(ref(&arr[1])-- == &arr[1], "");
303 static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
304 static_assert(postdec(&arr[1]) == &arr[0], "");
305
306 int x;
307 static_assert(++ref(&x) == &x + 1, "");
308
309 static_assert(++ref(0.0) == 1.0, "");
310 static_assert(ref(0.0)++ == 0.0, "");
311 static_assert(postinc(0.0) == 1.0, "");
312 static_assert(--ref(0.0) == -1.0, "");
313 static_assert(ref(0.0)-- == 0.0, "");
314 static_assert(postdec(0.0) == -1.0, "");
315
316 static_assert(++ref(1e100) == 1e100, "");
317 static_assert(--ref(1e100) == 1e100, "");
318
319 union U {
320 int a, b;
321 };
322 constexpr int f(U u) {
323 return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}}
324 }
325 constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}}
326 constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}}
327
328 constexpr int incr(int k) {
329 int x = k;
330 if (x++ == 100)
331 return x;
332 return incr(x);
333 }
334 static_assert(incr(0) == 101, "");
335}
Richard Smith4e18ca52013-05-06 05:56:11 +0000336
Richard Smith43e77732013-05-07 04:50:00 +0000337namespace compound_assign {
338 constexpr bool test_int() {
339 int a = 3;
340 a += 6;
341 if (a != 9) throw 0;
342 a -= 2;
343 if (a != 7) throw 0;
344 a *= 3;
345 if (a != 21) throw 0;
346 a /= 10;
347 if (a != 2) throw 0;
348 a <<= 3;
349 if (a != 16) throw 0;
350 a %= 6;
351 if (a != 4) throw 0;
352 a >>= 1;
353 if (a != 2) throw 0;
354 a ^= 10;
355 if (a != 8) throw 0;
356 a |= 5;
357 if (a != 13) throw 0;
358 a &= 14;
359 if (a != 12) throw 0;
360 return true;
361 }
362 static_assert(test_int(), "");
363
364 template<typename T>
365 constexpr bool test_overflow() {
366 T a = 1;
367 while (a)
368 a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }}
369 return true;
370 }
371
372 static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}}
373 static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined
374 static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication
375 static_assert(test_overflow<unsigned short>(), ""); // ok
376 static_assert(test_overflow<unsigned long long>(), ""); // ok
377 static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}}
378
379 constexpr short test_promotion(short k) {
380 short s = k;
381 s *= s;
382 return s;
383 }
384 static_assert(test_promotion(100) == 10000, "");
385 static_assert(test_promotion(200) == -25536, "");
386 static_assert(test_promotion(256) == 0, "");
387}
388
Richard Smith4e18ca52013-05-06 05:56:11 +0000389namespace loops {
390 constexpr int fib_loop(int a) {
391 int f_k = 0, f_k_plus_one = 1;
392 for (int k = 1; k != a; ++k) {
393 int f_k_plus_two = f_k + f_k_plus_one;
394 f_k = f_k_plus_one;
395 f_k_plus_one = f_k_plus_two;
396 }
397 return f_k_plus_one;
398 }
399 static_assert(fib_loop(46) == 1836311903, "");
400
401 constexpr bool breaks_work() {
402 int a = 0;
403 for (int n = 0; n != 100; ++n) {
404 ++a;
405 if (a == 5) continue;
406 if ((a % 5) == 0) break;
407 }
408
409 int b = 0;
410 while (b != 17) {
411 ++b;
412 if (b == 6) continue;
413 if ((b % 6) == 0) break;
414 }
415
416 int c = 0;
417 do {
418 ++c;
419 if (c == 7) continue;
420 if ((c % 7) == 0) break;
421 } while (c != 21);
422
423 return a == 10 && b == 12 & c == 14;
424 }
425 static_assert(breaks_work(), "");
426
427 void not_constexpr();
428 constexpr bool no_cont_after_break() {
429 for (;;) {
430 break;
431 not_constexpr();
432 }
433 while (true) {
434 break;
435 not_constexpr();
436 }
437 do {
438 break;
439 not_constexpr();
440 } while (true);
441 return true;
442 }
443 static_assert(no_cont_after_break(), "");
444
445 constexpr bool cond() {
446 for (int a = 1; bool b = a != 3; ++a) {
447 if (!b)
448 return false;
449 }
450 while (bool b = true) {
451 b = false;
452 break;
453 }
454 return true;
455 }
456 static_assert(cond(), "");
Richard Smith896e0d72013-05-06 06:51:17 +0000457
458 constexpr int range_for() {
459 int arr[] = { 1, 2, 3, 4, 5 };
460 int sum = 0;
461 for (int x : arr)
Richard Smith43e77732013-05-07 04:50:00 +0000462 sum += x;
Richard Smith896e0d72013-05-06 06:51:17 +0000463 return sum;
464 }
465 static_assert(range_for() == 15, "");
466
467 template<int...N> struct ints {};
468 template<typename A, typename B> struct join_ints;
469 template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
470 using type = ints<As..., sizeof...(As) + Bs...>;
471 };
472 template<unsigned N> struct make_ints {
473 using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
474 };
475 template<> struct make_ints<0> { using type = ints<>; };
476 template<> struct make_ints<1> { using type = ints<0>; };
477
478 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
479
480 template<typename T, unsigned N> struct array {
481 constexpr array() : arr{} {}
482 template<typename ...X>
483 constexpr array(X ...x) : arr{} {
484 init(typename make_ints<sizeof...(X)>::type{}, x...);
485 }
486 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
487 ignore{arr[I] = x ...};
488 }
489 T arr[N];
490 struct iterator {
491 T *p;
492 constexpr explicit iterator(T *p) : p(p) {}
493 constexpr bool operator!=(iterator o) { return p != o.p; }
494 constexpr iterator &operator++() { ++p; return *this; }
495 constexpr T &operator*() { return *p; }
496 };
497 constexpr iterator begin() { return iterator(arr); }
498 constexpr iterator end() { return iterator(arr + N); }
499 };
500
501 constexpr int range_for_2() {
502 array<int, 5> arr { 1, 2, 3, 4, 5 };
503 int sum = 0;
504 for (int k : arr) {
Richard Smith43e77732013-05-07 04:50:00 +0000505 sum += k;
Richard Smith896e0d72013-05-06 06:51:17 +0000506 if (sum > 8) break;
507 }
508 return sum;
509 }
510 static_assert(range_for_2() == 10, "");
Richard Smith4e18ca52013-05-06 05:56:11 +0000511}
Richard Smith99005e62013-05-07 03:19:20 +0000512
Richard Smith43e77732013-05-07 04:50:00 +0000513namespace assignment_op {
Richard Smith99005e62013-05-07 03:19:20 +0000514 struct A {
515 constexpr A() : n(5) {}
516 int n;
517 struct B {
518 int k = 1;
519 union U {
520 constexpr U() : y(4) {}
521 int x;
522 int y;
523 } u;
524 } b;
525 };
526 constexpr bool testA() {
527 A a, b;
528 a.n = 7;
529 a.b.u.y = 5;
530 b = a;
531 return b.n == 7 && b.b.u.y == 5 && b.b.k == 1;
532 }
533 static_assert(testA(), "");
534
535 struct B {
536 bool assigned = false;
537 constexpr B &operator=(const B&) {
538 assigned = true;
539 return *this;
540 }
541 };
542 struct C : B {
543 B b;
544 int n = 5;
545 };
546 constexpr bool testC() {
547 C c, d;
548 c.n = 7;
549 d = c;
550 c.n = 3;
551 return d.n == 7 && d.assigned && d.b.assigned;
552 }
553 static_assert(testC(), "");
554}