xref: /aosp_15_r20/external/clang/test/CXX/expr/expr.const/p2-0x.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
2*67e74705SXin Li 
3*67e74705SXin Li // A conditional-expression is a core constant expression unless it involves one
4*67e74705SXin Li // of the following as a potentially evaluated subexpression [...]:
5*67e74705SXin Li 
6*67e74705SXin Li // - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
7*67e74705SXin Li //   expression, function invocation substitution (7.1.5 [dcl.constexpr])
8*67e74705SXin Li //   replaces each occurrence of this in a constexpr member function with a
9*67e74705SXin Li //   pointer to the class object. -end note];
10*67e74705SXin Li struct This {
11*67e74705SXin Li   int this1 : this1; // expected-error {{undeclared}}
12*67e74705SXin Li   int this2 : this->this1; // expected-error {{invalid}}
this3This13*67e74705SXin Li   void this3() {
14*67e74705SXin Li     int n1[this->this1]; // expected-warning {{variable length array}}
15*67e74705SXin Li     int n2[this1]; // expected-warning {{variable length array}}
16*67e74705SXin Li     (void)n1, (void)n2;
17*67e74705SXin Li   }
18*67e74705SXin Li };
19*67e74705SXin Li 
20*67e74705SXin Li // - an invocation of a function other than a constexpr constructor for a
21*67e74705SXin Li //   literal class or a constexpr function [ Note: Overload resolution (13.3)
22*67e74705SXin Li //   is applied as usual - end note ];
23*67e74705SXin Li struct NonConstexpr1 {
fNonConstexpr124*67e74705SXin Li   static int f() { return 1; } // expected-note {{here}}
25*67e74705SXin Li   int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26*67e74705SXin Li };
27*67e74705SXin Li struct NonConstexpr2 {
28*67e74705SXin Li   constexpr NonConstexpr2(); // expected-note {{here}}
29*67e74705SXin Li   int n;
30*67e74705SXin Li };
31*67e74705SXin Li struct NonConstexpr3 {
32*67e74705SXin Li   NonConstexpr3();
33*67e74705SXin Li   int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34*67e74705SXin Li };
35*67e74705SXin Li struct NonConstexpr4 {
36*67e74705SXin Li   NonConstexpr4(); // expected-note {{declared here}}
37*67e74705SXin Li   int n;
38*67e74705SXin Li };
39*67e74705SXin Li struct NonConstexpr5 {
40*67e74705SXin Li   int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
41*67e74705SXin Li };
42*67e74705SXin Li 
43*67e74705SXin Li // - an invocation of an undefined constexpr function or an undefined
44*67e74705SXin Li //   constexpr constructor;
45*67e74705SXin Li struct UndefinedConstexpr {
46*67e74705SXin Li   constexpr UndefinedConstexpr();
47*67e74705SXin Li   static constexpr int undefinedConstexpr1(); // expected-note {{here}}
48*67e74705SXin Li   int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
49*67e74705SXin Li };
50*67e74705SXin Li 
51*67e74705SXin Li // - an invocation of a constexpr function with arguments that, when substituted
52*67e74705SXin Li //   by function invocation substitution (7.1.5), do not produce a core constant
53*67e74705SXin Li //   expression;
54*67e74705SXin Li namespace NonConstExprReturn {
id_ref(const int & n)55*67e74705SXin Li   static constexpr const int &id_ref(const int &n) {
56*67e74705SXin Li     return n;
57*67e74705SXin Li   }
58*67e74705SXin Li   struct NonConstExprFunction {
59*67e74705SXin Li     int n : id_ref(16); // ok
60*67e74705SXin Li   };
address_of(const int & a)61*67e74705SXin Li   constexpr const int *address_of(const int &a) {
62*67e74705SXin Li     return &a;
63*67e74705SXin Li   }
return_param(int n)64*67e74705SXin Li   constexpr const int *return_param(int n) { // expected-note {{declared here}}
65*67e74705SXin Li     return address_of(n);
66*67e74705SXin Li   }
67*67e74705SXin Li   struct S {
68*67e74705SXin Li     int n : *return_param(0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
69*67e74705SXin Li   };
70*67e74705SXin Li }
71*67e74705SXin Li 
72*67e74705SXin Li // - an invocation of a constexpr constructor with arguments that, when
73*67e74705SXin Li //   substituted by function invocation substitution (7.1.5), do not produce all
74*67e74705SXin Li //   constant expressions for the constructor calls and full-expressions in the
75*67e74705SXin Li //   mem-initializers (including conversions);
76*67e74705SXin Li namespace NonConstExprCtor {
77*67e74705SXin Li   struct T {
TNonConstExprCtor::T78*67e74705SXin Li     constexpr T(const int &r) :
79*67e74705SXin Li       r(r) {
80*67e74705SXin Li     }
81*67e74705SXin Li     const int &r;
82*67e74705SXin Li   };
83*67e74705SXin Li   constexpr int n = 0;
84*67e74705SXin Li   constexpr T t1(n); // ok
85*67e74705SXin Li   constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{reference to temporary is not a constant expression}}
86*67e74705SXin Li 
87*67e74705SXin Li   struct S {
88*67e74705SXin Li     int n : T(4).r; // ok
89*67e74705SXin Li   };
90*67e74705SXin Li }
91*67e74705SXin Li 
92*67e74705SXin Li // - an invocation of a constexpr function or a constexpr constructor that would
93*67e74705SXin Li //   exceed the implementation-defined recursion limits (see Annex B);
94*67e74705SXin Li namespace RecursionLimits {
RecurseForever(int n)95*67e74705SXin Li   constexpr int RecurseForever(int n) {
96*67e74705SXin Li     return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 118 calls}}
97*67e74705SXin Li   }
98*67e74705SXin Li   struct AlsoRecurseForever {
AlsoRecurseForeverRecursionLimits::AlsoRecurseForever99*67e74705SXin Li     constexpr AlsoRecurseForever(int n) :
100*67e74705SXin Li       n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 118 calls}}
101*67e74705SXin Li     {}
102*67e74705SXin Li     int n;
103*67e74705SXin Li   };
104*67e74705SXin Li   struct S {
105*67e74705SXin Li     int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
106*67e74705SXin Li     int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
107*67e74705SXin Li   };
108*67e74705SXin Li }
109*67e74705SXin Li 
110*67e74705SXin Li // DR1458: taking the address of an object of incomplete class type
111*67e74705SXin Li namespace IncompleteClassTypeAddr {
112*67e74705SXin Li   struct S;
113*67e74705SXin Li   extern S s;
114*67e74705SXin Li   constexpr S *p = &s; // ok
115*67e74705SXin Li   static_assert(p, "");
116*67e74705SXin Li 
117*67e74705SXin Li   extern S sArr[];
118*67e74705SXin Li   constexpr S (*p2)[] = &sArr; // ok
119*67e74705SXin Li 
120*67e74705SXin Li   struct S {
operator &IncompleteClassTypeAddr::S121*67e74705SXin Li     constexpr S *operator&() const { return nullptr; }
122*67e74705SXin Li   };
123*67e74705SXin Li   constexpr S *q = &s; // ok
124*67e74705SXin Li   static_assert(!q, "");
125*67e74705SXin Li }
126*67e74705SXin Li 
127*67e74705SXin Li // - an operation that would have undefined behavior [Note: including, for
128*67e74705SXin Li //   example, signed integer overflow (Clause 5 [expr]), certain pointer
129*67e74705SXin Li //   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
130*67e74705SXin Li //   shift operations (5.8 [expr.shift]) -end note];
131*67e74705SXin Li namespace UndefinedBehavior {
f(int n)132*67e74705SXin Li   void f(int n) {
133*67e74705SXin Li     switch (n) {
134*67e74705SXin Li     case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
135*67e74705SXin Li     case (int)0x80000000u: // ok
136*67e74705SXin Li     case (int)10000000000ll: // expected-note {{here}}
137*67e74705SXin Li     case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
138*67e74705SXin Li     case (int)(unsigned)(long long)4.4e9: // ok
139*67e74705SXin Li     case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
140*67e74705SXin Li     case (int)((float)1e37 / 1e30): // ok
141*67e74705SXin Li     case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type '__fp16'}}
142*67e74705SXin Li       break;
143*67e74705SXin Li     }
144*67e74705SXin Li   }
145*67e74705SXin Li 
146*67e74705SXin Li   constexpr int int_min = ~0x7fffffff;
147*67e74705SXin Li   constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
148*67e74705SXin Li   constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
149*67e74705SXin Li   constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
150*67e74705SXin Li   constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
151*67e74705SXin Li   constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
152*67e74705SXin Li 
153*67e74705SXin Li   constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
154*67e74705SXin Li   constexpr int shl_0 = 0 << 0; // ok
155*67e74705SXin Li   constexpr int shl_31 = 0 << 31; // ok
156*67e74705SXin Li   constexpr int shl_32 = 0 << 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type 'int' (32}} expected-warning {{>= width of type}}
157*67e74705SXin Li   constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
158*67e74705SXin Li   constexpr int shl_unsigned_into_sign = 1u << 31; // ok
159*67e74705SXin Li   constexpr int shl_unsigned_overflow = 1024u << 31; // ok
160*67e74705SXin Li   constexpr int shl_signed_negative = (-3) << 1; // expected-warning {{shifting a negative signed value is undefined}} // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
161*67e74705SXin Li   constexpr int shl_signed_ok = 1 << 30; // ok
162*67e74705SXin Li   constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
163*67e74705SXin Li   constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
164*67e74705SXin Li   constexpr int shl_signed_off_end = 2 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x100000000) requires 34 bits to represent, but 'int' only has 32 bits}}
165*67e74705SXin Li   constexpr int shl_signed_off_end_2 = 0x7fffffff << 2; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x1FFFFFFFC) requires 34 bits to represent, but 'int' only has 32 bits}}
166*67e74705SXin Li   constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{requires 43 bits to represent}}
167*67e74705SXin Li   constexpr int shl_signed_ok2 = 1024 << 20; // ok
168*67e74705SXin Li 
169*67e74705SXin Li   constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
170*67e74705SXin Li   constexpr int shr_0 = 0 >> 0; // ok
171*67e74705SXin Li   constexpr int shr_31 = 0 >> 31; // ok
172*67e74705SXin Li   constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
173*67e74705SXin Li 
174*67e74705SXin Li   struct S {
175*67e74705SXin Li     int m;
176*67e74705SXin Li   };
177*67e74705SXin Li   constexpr S s = { 5 };
178*67e74705SXin Li   constexpr const int *p = &s.m + 1;
f(const int * q)179*67e74705SXin Li   constexpr const int &f(const int *q) {
180*67e74705SXin Li     return q[0];
181*67e74705SXin Li   }
182*67e74705SXin Li   constexpr int n = (f(p), 0); // ok
183*67e74705SXin Li   struct T {
184*67e74705SXin Li     int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
185*67e74705SXin Li   };
186*67e74705SXin Li 
187*67e74705SXin Li   namespace Ptr {
188*67e74705SXin Li     struct A {};
189*67e74705SXin Li     struct B : A { int n; };
190*67e74705SXin Li     B a[3][3];
191*67e74705SXin Li     constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
192*67e74705SXin Li     B b = {};
193*67e74705SXin Li     constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
194*67e74705SXin Li     constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
195*67e74705SXin Li     constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
196*67e74705SXin Li     constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
197*67e74705SXin Li 
198*67e74705SXin Li     constexpr A *na = nullptr;
199*67e74705SXin Li     constexpr B *nb = nullptr;
200*67e74705SXin Li     constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
201*67e74705SXin Li     constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
202*67e74705SXin Li     static_assert((A*)nb == 0, "");
203*67e74705SXin Li     static_assert((B*)na == 0, "");
204*67e74705SXin Li     constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
205*67e74705SXin Li     constexpr const int *np1 = (int*)nullptr + 0; // ok
206*67e74705SXin Li     constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
207*67e74705SXin Li     constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
208*67e74705SXin Li 
209*67e74705SXin Li     struct C {
fUndefinedBehavior::Ptr::C210*67e74705SXin Li       constexpr int f() const { return 0; }
211*67e74705SXin Li     } constexpr c = C();
212*67e74705SXin Li     constexpr int k1 = c.f(); // ok
213*67e74705SXin Li     constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
214*67e74705SXin Li     constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
215*67e74705SXin Li     C c2;
216*67e74705SXin Li     constexpr int k4 = c2.f(); // ok!
217*67e74705SXin Li 
218*67e74705SXin Li     constexpr int diff1 = &a[2] - &a[0];
219*67e74705SXin Li     constexpr int diff2 = &a[1][3] - &a[1][0];
220*67e74705SXin Li     constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
221*67e74705SXin Li     static_assert(&a[2][0] == &a[1][3], "");
222*67e74705SXin Li     constexpr int diff4 = (&b + 1) - &b;
223*67e74705SXin Li     constexpr int diff5 = &a[1][2].n - &a[1][0].n; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
224*67e74705SXin Li     constexpr int diff6 = &a[1][2].n - &a[1][2].n;
225*67e74705SXin Li     constexpr int diff7 = (A*)&a[0][1] - (A*)&a[0][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
226*67e74705SXin Li   }
227*67e74705SXin Li 
228*67e74705SXin Li   namespace Overflow {
229*67e74705SXin Li     // Signed int overflow.
230*67e74705SXin Li     constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
231*67e74705SXin Li     constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
232*67e74705SXin Li     constexpr int n3 = n1 + 1; // ok
233*67e74705SXin Li     constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
234*67e74705SXin Li     constexpr int n5 = -65536 * 32768; // ok
235*67e74705SXin Li     constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
236*67e74705SXin Li     constexpr int n7 = -n3 + -1; // ok
237*67e74705SXin Li     constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
238*67e74705SXin Li     constexpr int n9 = n3 - 0; // ok
239*67e74705SXin Li     constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
240*67e74705SXin Li     constexpr int n11 = -1 - n3; // ok
241*67e74705SXin Li     constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
242*67e74705SXin Li     constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
243*67e74705SXin Li     constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
244*67e74705SXin Li     constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
245*67e74705SXin Li     constexpr signed char c1 = 100 * 2; // ok expected-warning{{changes value}}
246*67e74705SXin Li     constexpr signed char c2 = '\x64' * '\2'; // also ok  expected-warning{{changes value}}
247*67e74705SXin Li     constexpr long long ll1 = 0x7fffffffffffffff; // ok
248*67e74705SXin Li     constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
249*67e74705SXin Li     constexpr long long ll3 = -ll1 - 1; // ok
250*67e74705SXin Li     constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
251*67e74705SXin Li     constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
252*67e74705SXin Li 
253*67e74705SXin Li     // Yikes.
254*67e74705SXin Li     char melchizedek[2200000000];
255*67e74705SXin Li     typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
256*67e74705SXin Li     constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
257*67e74705SXin Li     constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
258*67e74705SXin Li     constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
259*67e74705SXin Li     constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
260*67e74705SXin Li 
261*67e74705SXin Li     // Unsigned int overflow.
262*67e74705SXin Li     static_assert(65536u * 65536u == 0u, ""); // ok
263*67e74705SXin Li     static_assert(4294967295u + 1u == 0u, ""); // ok
264*67e74705SXin Li     static_assert(0u - 1u == 4294967295u, ""); // ok
265*67e74705SXin Li     static_assert(~0u * ~0u == 1u, ""); // ok
266*67e74705SXin Li 
267*67e74705SXin Li     // Floating-point overflow and NaN.
268*67e74705SXin Li     constexpr float f1 = 1e38f * 3.4028f; // ok
269*67e74705SXin Li     constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
270*67e74705SXin Li     constexpr float f3 = 1e38f / -.2939f; // ok
271*67e74705SXin Li     constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
272*67e74705SXin Li     constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
273*67e74705SXin Li     constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
274*67e74705SXin Li     constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
275*67e74705SXin Li   }
276*67e74705SXin Li }
277*67e74705SXin Li 
278*67e74705SXin Li // - a lambda-expression (5.1.2);
279*67e74705SXin Li struct Lambda {
__anonf96f42a50102Lambda280*67e74705SXin Li   int n : []{ return 1; }(); // expected-error {{constant expression}} expected-error {{integral constant expression}} expected-note {{non-literal type}}
281*67e74705SXin Li };
282*67e74705SXin Li 
283*67e74705SXin Li // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
284*67e74705SXin Li namespace LValueToRValue {
285*67e74705SXin Li   // - a non-volatile glvalue of integral or enumeration type that refers to a
286*67e74705SXin Li   //   non-volatile const object with a preceding initialization, initialized
287*67e74705SXin Li   //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
288*67e74705SXin Li   //   corresponds to an array of such objects. -end note], or
289*67e74705SXin Li   volatile const int vi = 1; // expected-note 2{{here}}
290*67e74705SXin Li   const int ci = 1;
291*67e74705SXin Li   volatile const int &vrci = ci;
292*67e74705SXin Li   static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
293*67e74705SXin Li   static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
294*67e74705SXin Li   static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
295*67e74705SXin Li 
296*67e74705SXin Li   // - a non-volatile glvalue of literal type that refers to a non-volatile
297*67e74705SXin Li   //   object defined with constexpr, or that refers to a sub-object of such an
298*67e74705SXin Li   //   object, or
299*67e74705SXin Li   struct V {
VLValueToRValue::V300*67e74705SXin Li     constexpr V() : v(1) {}
301*67e74705SXin Li     volatile int v; // expected-note {{not literal because}}
302*67e74705SXin Li   };
303*67e74705SXin Li   constexpr V v; // expected-error {{non-literal type}}
304*67e74705SXin Li   struct S {
SLValueToRValue::S305*67e74705SXin Li     constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
SLValueToRValue::S306*67e74705SXin Li     constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
307*67e74705SXin Li     int i;
308*67e74705SXin Li     volatile int &v;
309*67e74705SXin Li   };
310*67e74705SXin Li   constexpr S s; // ok
311*67e74705SXin Li   constexpr volatile S vs; // expected-note {{here}}
312*67e74705SXin Li   constexpr const volatile S &vrs = s; // ok
313*67e74705SXin Li   static_assert(s.i, "");
314*67e74705SXin Li   static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
315*67e74705SXin Li   static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
316*67e74705SXin Li   static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
317*67e74705SXin Li   static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
318*67e74705SXin Li   static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
319*67e74705SXin Li 
320*67e74705SXin Li   // - a non-volatile glvalue of literal type that refers to a non-volatile
321*67e74705SXin Li   //   temporary object whose lifetime has not ended, initialized with a
322*67e74705SXin Li   //   constant expression;
f()323*67e74705SXin Li   constexpr volatile S f() { return S(); }
324*67e74705SXin Li   static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
325*67e74705SXin Li   static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
326*67e74705SXin Li }
327*67e74705SXin Li 
328*67e74705SXin Li // DR1312: The proposed wording for this defect has issues, so we ignore this
329*67e74705SXin Li // bullet and instead prohibit casts from pointers to cv void (see core-20842
330*67e74705SXin Li // and core-20845).
331*67e74705SXin Li //
332*67e74705SXin Li // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
333*67e74705SXin Li // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
334*67e74705SXin Li // are neither the same type nor similar types (4.4 [conv.qual]);
335*67e74705SXin Li 
336*67e74705SXin Li // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
337*67e74705SXin Li // refers to a non-active member of a union or a subobject thereof;
338*67e74705SXin Li namespace LValueToRValueUnion {
339*67e74705SXin Li   // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
340*67e74705SXin Li   // of this.
341*67e74705SXin Li   union U { int a, b; } constexpr u = U();
342*67e74705SXin Li   static_assert(u.a == 0, "");
343*67e74705SXin Li   constexpr const int *bp = &u.b;
344*67e74705SXin Li   constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
345*67e74705SXin Li 
346*67e74705SXin Li   extern const U pu;
347*67e74705SXin Li   constexpr const int *pua = &pu.a;
348*67e74705SXin Li   constexpr const int *pub = &pu.b;
349*67e74705SXin Li   constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
350*67e74705SXin Li   constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
351*67e74705SXin Li   constexpr const int b2 = *pub; // ok
352*67e74705SXin Li }
353*67e74705SXin Li 
354*67e74705SXin Li // - an id-expression that refers to a variable or data member of reference type
355*67e74705SXin Li //   unless the reference has a preceding initialization, initialized with a
356*67e74705SXin Li //   constant expression;
357*67e74705SXin Li namespace References {
358*67e74705SXin Li   const int a = 2;
359*67e74705SXin Li   int &b = *const_cast<int*>(&a);
360*67e74705SXin Li   int c = 10; // expected-note 2 {{here}}
361*67e74705SXin Li   int &d = c;
362*67e74705SXin Li   constexpr int e = 42;
363*67e74705SXin Li   int &f = const_cast<int&>(e);
364*67e74705SXin Li   extern int &g;
365*67e74705SXin Li   constexpr int &h(); // expected-note {{here}}
366*67e74705SXin Li   int &i = h(); // expected-note {{here}}
j()367*67e74705SXin Li   constexpr int &j() { return b; }
368*67e74705SXin Li   int &k = j();
369*67e74705SXin Li 
370*67e74705SXin Li   struct S {
371*67e74705SXin Li     int A : a;
372*67e74705SXin Li     int B : b;
373*67e74705SXin Li     int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
374*67e74705SXin Li     int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
375*67e74705SXin Li     int D2 : &d - &c + 1;
376*67e74705SXin Li     int E : e / 2;
377*67e74705SXin Li     int F : f - 11;
378*67e74705SXin Li     int G : g; // expected-error {{constant expression}}
379*67e74705SXin Li     int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
380*67e74705SXin Li     int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
381*67e74705SXin Li     int J : j();
382*67e74705SXin Li     int K : k;
383*67e74705SXin Li   };
384*67e74705SXin Li }
385*67e74705SXin Li 
386*67e74705SXin Li // - a dynamic_cast (5.2.7);
387*67e74705SXin Li namespace DynamicCast {
388*67e74705SXin Li   struct S { int n; };
389*67e74705SXin Li   constexpr S s { 16 };
390*67e74705SXin Li   struct T {
391*67e74705SXin Li     int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
392*67e74705SXin Li   };
393*67e74705SXin Li }
394*67e74705SXin Li 
395*67e74705SXin Li // - a reinterpret_cast (5.2.10);
396*67e74705SXin Li namespace ReinterpretCast {
397*67e74705SXin Li   struct S { int n; };
398*67e74705SXin Li   constexpr S s { 16 };
399*67e74705SXin Li   struct T {
400*67e74705SXin Li     int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
401*67e74705SXin Li   };
402*67e74705SXin Li   struct U {
403*67e74705SXin Li     int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
404*67e74705SXin Li   };
405*67e74705SXin Li }
406*67e74705SXin Li 
407*67e74705SXin Li // - a pseudo-destructor call (5.2.4);
408*67e74705SXin Li namespace PseudoDtor {
409*67e74705SXin Li   int k;
410*67e74705SXin Li   typedef int I;
411*67e74705SXin Li   struct T {
412*67e74705SXin Li     int n : (k.~I(), 0); // expected-error {{constant expression}}
413*67e74705SXin Li   };
414*67e74705SXin Li }
415*67e74705SXin Li 
416*67e74705SXin Li // - increment or decrement operations (5.2.6, 5.3.2);
417*67e74705SXin Li namespace IncDec {
418*67e74705SXin Li   int k = 2;
419*67e74705SXin Li   struct T {
420*67e74705SXin Li     int n : ++k; // expected-error {{constant expression}}
421*67e74705SXin Li     int m : --k; // expected-error {{constant expression}}
422*67e74705SXin Li   };
423*67e74705SXin Li }
424*67e74705SXin Li 
425*67e74705SXin Li // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
426*67e74705SXin Li namespace std {
427*67e74705SXin Li   struct type_info {
428*67e74705SXin Li     virtual ~type_info();
429*67e74705SXin Li     const char *name;
430*67e74705SXin Li   };
431*67e74705SXin Li }
432*67e74705SXin Li namespace TypeId {
433*67e74705SXin Li   struct S { virtual void f(); };
434*67e74705SXin Li   constexpr S *p = 0;
435*67e74705SXin Li   constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} expected-note {{typeid applied to expression of polymorphic type 'TypeId::S'}}
436*67e74705SXin Li 
437*67e74705SXin Li   struct T {} t;
438*67e74705SXin Li   constexpr const std::type_info &ti2 = typeid(t);
439*67e74705SXin Li }
440*67e74705SXin Li 
441*67e74705SXin Li // - a new-expression (5.3.4);
442*67e74705SXin Li // - a delete-expression (5.3.5);
443*67e74705SXin Li namespace NewDelete {
444*67e74705SXin Li   int *p = 0;
445*67e74705SXin Li   struct T {
446*67e74705SXin Li     int n : *new int(4); // expected-error {{constant expression}}
447*67e74705SXin Li     int m : (delete p, 2); // expected-error {{constant expression}}
448*67e74705SXin Li   };
449*67e74705SXin Li }
450*67e74705SXin Li 
451*67e74705SXin Li // - a relational (5.9) or equality (5.10) operator where the result is
452*67e74705SXin Li //   unspecified;
453*67e74705SXin Li namespace UnspecifiedRelations {
454*67e74705SXin Li   int a, b;
455*67e74705SXin Li   constexpr int *p = &a, *q = &b;
456*67e74705SXin Li   // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
457*67e74705SXin Li   // different objects that are not members of the same array or to different
458*67e74705SXin Li   // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
459*67e74705SXin Li   // and p>=q are unspecified.
460*67e74705SXin Li   constexpr bool u1 = p < q; // expected-error {{constant expression}}
461*67e74705SXin Li   constexpr bool u2 = p > q; // expected-error {{constant expression}}
462*67e74705SXin Li   constexpr bool u3 = p <= q; // expected-error {{constant expression}}
463*67e74705SXin Li   constexpr bool u4 = p >= q; // expected-error {{constant expression}}
464*67e74705SXin Li   constexpr bool u5 = p < 0; // expected-error {{constant expression}}
465*67e74705SXin Li   constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
466*67e74705SXin Li   constexpr bool u7 = p > 0; // expected-error {{constant expression}}
467*67e74705SXin Li   constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
468*67e74705SXin Li   constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
469*67e74705SXin Li   constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
470*67e74705SXin Li   constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
471*67e74705SXin Li   constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
472*67e74705SXin Li   void f(), g();
473*67e74705SXin Li 
474*67e74705SXin Li   constexpr void (*pf)() = &f, (*pg)() = &g;
475*67e74705SXin Li   constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
476*67e74705SXin Li   constexpr bool u14 = pf == pg;
477*67e74705SXin Li 
478*67e74705SXin Li   // If two pointers point to non-static data members of the same object with
479*67e74705SXin Li   // different access control, the result is unspecified.
480*67e74705SXin Li   struct A {
481*67e74705SXin Li   public:
AUnspecifiedRelations::A482*67e74705SXin Li     constexpr A() : a(0), b(0) {}
483*67e74705SXin Li     int a;
cmpUnspecifiedRelations::A484*67e74705SXin Li     constexpr bool cmp() const { return &a < &b; } // expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
485*67e74705SXin Li   private:
486*67e74705SXin Li     int b;
487*67e74705SXin Li   };
488*67e74705SXin Li   static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
489*67e74705SXin Li   class B {
490*67e74705SXin Li   public:
491*67e74705SXin Li     A a;
cmp() const492*67e74705SXin Li     constexpr bool cmp() const { return &a.a < &b.a; } // expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
493*67e74705SXin Li   protected:
494*67e74705SXin Li     A b;
495*67e74705SXin Li   };
496*67e74705SXin Li   static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
497*67e74705SXin Li 
498*67e74705SXin Li   // If two pointers point to different base sub-objects of the same object, or
499*67e74705SXin Li   // one points to a base subobject and the other points to a member, the result
500*67e74705SXin Li   // of the comparison is unspecified. This is not explicitly called out by
501*67e74705SXin Li   // [expr.rel]p2, but is covered by 'Other pointer comparisons are
502*67e74705SXin Li   // unspecified'.
503*67e74705SXin Li   struct C {
504*67e74705SXin Li     int c[2];
505*67e74705SXin Li   };
506*67e74705SXin Li   struct D {
507*67e74705SXin Li     int d;
508*67e74705SXin Li   };
509*67e74705SXin Li   struct E : C, D {
510*67e74705SXin Li     struct Inner {
511*67e74705SXin Li       int f;
512*67e74705SXin Li     } e;
513*67e74705SXin Li   } e;
514*67e74705SXin Li   constexpr bool base1 = &e.c[0] < &e.d; // expected-error {{constant expression}} expected-note {{comparison of addresses of subobjects of different base classes has unspecified value}}
515*67e74705SXin Li   constexpr bool base2 = &e.c[1] < &e.e.f; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'C' of class 'E' to field 'e' has unspecified value}}
516*67e74705SXin Li   constexpr bool base3 = &e.e.f < &e.d; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'D' of class 'E' to field 'e' has unspecified value}}
517*67e74705SXin Li 
518*67e74705SXin Li   // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
519*67e74705SXin Li   // represent the same address or are both the null pointer [...]; otherwise
520*67e74705SXin Li   // the result is unspecified.
521*67e74705SXin Li   struct S { int a, b; } s;
522*67e74705SXin Li   constexpr void *null = 0;
523*67e74705SXin Li   constexpr void *pv = (void*)&s.a;
524*67e74705SXin Li   constexpr void *qv = (void*)&s.b;
525*67e74705SXin Li   constexpr bool v1 = null < 0;
526*67e74705SXin Li   constexpr bool v2 = null < pv; // expected-error {{constant expression}}
527*67e74705SXin Li   constexpr bool v3 = null == pv; // ok
528*67e74705SXin Li   constexpr bool v4 = qv == pv; // ok
529*67e74705SXin Li   constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
530*67e74705SXin Li   constexpr bool v6 = qv > null; // expected-error {{constant expression}}
531*67e74705SXin Li   constexpr bool v7 = qv <= (void*)&s.b; // ok
532*67e74705SXin Li   constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
533*67e74705SXin Li }
534*67e74705SXin Li 
535*67e74705SXin Li // - an assignment or a compound assignment (5.17); or
536*67e74705SXin Li namespace Assignment {
537*67e74705SXin Li   int k;
538*67e74705SXin Li   struct T {
539*67e74705SXin Li     int n : (k = 9); // expected-error {{constant expression}}
540*67e74705SXin Li     int m : (k *= 2); // expected-error {{constant expression}}
541*67e74705SXin Li   };
542*67e74705SXin Li 
543*67e74705SXin Li   struct Literal {
LiteralAssignment::Literal544*67e74705SXin Li     constexpr Literal(const char *name) : name(name) {}
545*67e74705SXin Li     const char *name;
546*67e74705SXin Li   };
547*67e74705SXin Li   struct Expr {
ExprAssignment::Expr548*67e74705SXin Li     constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
549*67e74705SXin Li     bool IsLiteral;
550*67e74705SXin Li     union {
551*67e74705SXin Li       Literal l;
552*67e74705SXin Li       // ...
553*67e74705SXin Li     };
554*67e74705SXin Li   };
555*67e74705SXin Li   struct MulEq {
MulEqAssignment::MulEq556*67e74705SXin Li     constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
557*67e74705SXin Li     Expr LHS;
558*67e74705SXin Li     Expr RHS;
559*67e74705SXin Li   };
operator *=(Expr a,Expr b)560*67e74705SXin Li   constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
561*67e74705SXin Li   Literal a("a");
562*67e74705SXin Li   Literal b("b");
563*67e74705SXin Li   MulEq c = a *= b; // ok
564*67e74705SXin Li }
565*67e74705SXin Li 
566*67e74705SXin Li // - a throw-expression (15.1)
567*67e74705SXin Li namespace Throw {
568*67e74705SXin Li   struct S {
569*67e74705SXin Li     int n : (throw "hello", 10); // expected-error {{constant expression}}
570*67e74705SXin Li   };
571*67e74705SXin Li }
572*67e74705SXin Li 
573*67e74705SXin Li // PR9999
574*67e74705SXin Li template<unsigned int v>
575*67e74705SXin Li class bitWidthHolding {
576*67e74705SXin Li public:
577*67e74705SXin Li   static const
578*67e74705SXin Li   unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
579*67e74705SXin Li };
580*67e74705SXin Li 
581*67e74705SXin Li static const int width=bitWidthHolding<255>::width;
582*67e74705SXin Li 
583*67e74705SXin Li template<bool b>
584*67e74705SXin Li struct always_false {
585*67e74705SXin Li   static const bool value = false;
586*67e74705SXin Li };
587*67e74705SXin Li 
588*67e74705SXin Li template<bool b>
589*67e74705SXin Li struct and_or {
590*67e74705SXin Li   static const bool and_value = b && and_or<always_false<b>::value>::and_value;
591*67e74705SXin Li   static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
592*67e74705SXin Li };
593*67e74705SXin Li 
594*67e74705SXin Li static const bool and_value = and_or<true>::and_value;
595*67e74705SXin Li static const bool or_value = and_or<true>::or_value;
596*67e74705SXin Li 
597*67e74705SXin Li static_assert(and_value == false, "");
598*67e74705SXin Li static_assert(or_value == true, "");
599*67e74705SXin Li 
600*67e74705SXin Li namespace rdar13090123 {
601*67e74705SXin Li   typedef __INTPTR_TYPE__ intptr_t;
602*67e74705SXin Li 
f(intptr_t x)603*67e74705SXin Li   constexpr intptr_t f(intptr_t x) {
604*67e74705SXin Li     return (((x) >> 21) * 8);
605*67e74705SXin Li   }
606*67e74705SXin Li 
607*67e74705SXin Li   extern "C" int foo;
608*67e74705SXin Li 
609*67e74705SXin Li   constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
610*67e74705SXin Li   // expected-note{{reinterpret_cast}}
611*67e74705SXin Li }
612