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