1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128
2 
3 // A conditional-expression is a core constant expression unless it involves one
4 // of the following as a potentially evaluated subexpression [...]:
5 
6 // - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
7 //   expression, function invocation substitution (7.1.5 [dcl.constexpr])
8 //   replaces each occurrence of this in a constexpr member function with a
9 //   pointer to the class object. -end note];
10 struct This {
11   int this1 : this1; // expected-error {{undeclared}}
12   int this2 : this->this1; // expected-error {{invalid}}
13   void this3() {
14     int n1[this->this1]; // expected-warning {{variable length array}}
15     int n2[this1]; // expected-warning {{variable length array}}
16     (void)n1, (void)n2;
17   }
18 };
19 
20 // - an invocation of a function other than a constexpr constructor for a
21 //   literal class or a constexpr function [ Note: Overload resolution (13.3)
22 //   is applied as usual - end note ];
23 struct NonConstexpr1 {
24   static int f() { return 1; } // expected-note {{here}}
25   int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26 };
27 struct NonConstexpr2 {
28   constexpr NonConstexpr2(); // expected-note {{here}}
29   int n;
30 };
31 struct NonConstexpr3 {
32   NonConstexpr3();
33   int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34 };
35 struct NonConstexpr4 {
36   NonConstexpr4(); // expected-note {{declared here}}
37   int n;
38 };
39 struct NonConstexpr5 {
40   int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
41 };
42 
43 // - an invocation of an undefined constexpr function or an undefined
44 //   constexpr constructor;
45 struct UndefinedConstexpr {
46   constexpr UndefinedConstexpr();
47   static constexpr int undefinedConstexpr1(); // expected-note {{here}}
48   int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
49 };
50 
51 // - an invocation of a constexpr function with arguments that, when substituted
52 //   by function invocation substitution (7.1.5), do not produce a constant
53 //   expression;
54 namespace NonConstExprReturn {
55   static constexpr const int &id_ref(const int &n) {
56     return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
57   }
58   struct NonConstExprFunction {
59     int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
60         16 // expected-note {{temporary created here}}
61         );
62   };
63   constexpr const int *address_of(const int &a) {
64     return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
65   }
66   constexpr const int *return_param(int n) { // expected-note {{declared here}}
67     return address_of(n); // expected-note {{in call to 'address_of(n)'}}
68   }
69   struct S {
70     int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
71   };
72 }
73 
74 // - an invocation of a constexpr constructor with arguments that, when
75 //   substituted by function invocation substitution (7.1.5), do not produce all
76 //   constant expressions for the constructor calls and full-expressions in the
77 //   mem-initializers (including conversions);
78 namespace NonConstExprCtor {
79   struct T {
80     constexpr T(const int &r) :
81       r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
82     }
83     const int &r;
84   };
85   constexpr int n = 0;
86   constexpr T t1(n); // ok
87   constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(0)'}}
88 
89   struct S {
90     int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
91   };
92 }
93 
94 // - an invocation of a constexpr function or a constexpr constructor that would
95 //   exceed the implementation-defined recursion limits (see Annex B);
96 namespace RecursionLimits {
97   constexpr int RecurseForever(int n) {
98     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}}
99   }
100   struct AlsoRecurseForever {
101     constexpr AlsoRecurseForever(int n) :
102       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}}
103     {}
104     int n;
105   };
106   struct S {
107     int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
108     int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
109   };
110 }
111 
112 // FIXME:
113 // - an operation that would have undefined behavior [Note: including, for
114 //   example, signed integer overflow (Clause 5 [expr]), certain pointer
115 //   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
116 //   shift operations (5.8 [expr.shift]) -end note];
117 namespace UndefinedBehavior {
118   void f(int n) {
119     switch (n) {
120     case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
121     case (int)(unsigned)(long long)4.4e9: // ok
122     case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
123     case (int)((float)1e37 / 1e30): // ok
124     case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
125       break;
126     }
127   }
128 
129   struct S {
130     int m;
131   };
132   constexpr S s = { 5 }; // expected-note {{declared here}}
133   constexpr const int *p = &s.m + 1;
134   constexpr const int &f(const int *q) {
135     return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
136   }
137   constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
138   struct T {
139     int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
140   };
141 
142   namespace Ptr {
143     struct A {};
144     struct B : A { int n; };
145     B a[3][3];
146     constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
147     B b = {};
148     constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
149     constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
150     constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
151     constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
152 
153     constexpr A *na = nullptr;
154     constexpr B *nb = nullptr;
155     constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
156     constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
157     static_assert((A*)nb == 0, "");
158     static_assert((B*)na == 0, "");
159     constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
160     constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
161   }
162 }
163 
164 // - a lambda-expression (5.1.2);
165 struct Lambda {
166   // FIXME: clang crashes when trying to parse this! Revisit this check once
167   // lambdas are fully implemented.
168   //int n : []{ return 1; }();
169 };
170 
171 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
172 namespace LValueToRValue {
173   // - a non-volatile glvalue of integral or enumeration type that refers to a
174   //   non-volatile const object with a preceding initialization, initialized
175   //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
176   //   corresponds to an array of such objects. -end note], or
177   volatile const int vi = 1; // expected-note {{here}}
178   const int ci = 1;
179   volatile const int &vrci = ci;
180   static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
181   static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
182   static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
183 
184   // - a non-volatile glvalue of literal type that refers to a non-volatile
185   //   object defined with constexpr, or that refers to a sub-object of such an
186   //   object, or
187   struct S {
188     constexpr S(int=0) : i(1), v(1) {}
189     constexpr S(const S &s) : i(2), v(2) {}
190     int i;
191     volatile int v;
192   };
193   constexpr S s;
194   constexpr volatile S vs; // expected-note {{here}}
195   constexpr const volatile S &vrs = s;
196   static_assert(s.i, "");
197   static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
198   static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
199   static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
200   static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
201 
202   // - a non-volatile glvalue of literal type that refers to a non-volatile
203   //   temporary object whose lifetime has not ended, initialized with a
204   //   constant expression;
205   constexpr volatile S f() { return S(); }
206   static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
207   static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
208 }
209 
210 // FIXME:
211 //
212 // DR1312: The proposed wording for this defect has issues, so we ignore this
213 // bullet and instead prohibit casts from pointers to cv void (see core-20842
214 // and core-20845).
215 //
216 // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
217 // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
218 // are neither the same type nor similar types (4.4 [conv.qual]);
219 
220 // FIXME:
221 // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
222 // refers to a non-active member of a union or a subobject thereof;
223 
224 // - an id-expression that refers to a variable or data member of reference type
225 //   unless the reference has a preceding initialization, initialized with a
226 //   constant expression;
227 namespace References {
228   const int a = 2;
229   int &b = *const_cast<int*>(&a);
230   int c = 10; // expected-note 2 {{here}}
231   int &d = c;
232   constexpr int e = 42;
233   int &f = const_cast<int&>(e);
234   extern int &g;
235   constexpr int &h(); // expected-note 2{{here}}
236   int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
237   constexpr int &j() { return b; }
238   int &k = j();
239 
240   struct S {
241     int A : a;
242     int B : b;
243     int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
244     int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
245     int D2 : &d - &c + 1;
246     int E : e / 2;
247     int F : f - 11;
248     int G : g; // expected-error {{constant expression}}
249     int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
250     int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
251     int J : j();
252     int K : k;
253   };
254 }
255 
256 // - a dynamic_cast (5.2.7);
257 namespace DynamicCast {
258   struct S { int n; };
259   constexpr S s { 16 };
260   struct T {
261     int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
262   };
263 }
264 
265 // - a reinterpret_cast (5.2.10);
266 namespace ReinterpretCast {
267   struct S { int n; };
268   constexpr S s { 16 };
269   struct T {
270     int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
271   };
272   struct U {
273     int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
274   };
275 }
276 
277 // - a pseudo-destructor call (5.2.4);
278 namespace PseudoDtor {
279   int k;
280   typedef int I;
281   struct T {
282     int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
283   };
284 }
285 
286 // - increment or decrement operations (5.2.6, 5.3.2);
287 namespace IncDec {
288   int k = 2;
289   struct T {
290     int n : ++k; // expected-error {{constant expression}}
291     int m : --k; // expected-error {{constant expression}}
292   };
293 }
294 
295 // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
296 namespace std {
297   struct type_info {
298     virtual ~type_info();
299     const char *name;
300   };
301 }
302 namespace TypeId {
303   struct S { virtual void f(); };
304   constexpr S *p = 0;
305   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'}}
306 
307   struct T {} t;
308   constexpr const std::type_info &ti2 = typeid(t);
309 }
310 
311 // - a new-expression (5.3.4);
312 // - a delete-expression (5.3.5);
313 namespace NewDelete {
314   int *p = 0;
315   struct T {
316     int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}}
317     int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}}
318   };
319 }
320 
321 // - a relational (5.9) or equality (5.10) operator where the result is
322 //   unspecified;
323 namespace UnspecifiedRelations {
324   int a, b;
325   constexpr int *p = &a, *q = &b;
326   // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
327   // different objects that are not members of the same array or to different
328   // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
329   // and p>=q are unspecified.
330   constexpr bool u1 = p < q; // expected-error {{constant expression}}
331   constexpr bool u2 = p > q; // expected-error {{constant expression}}
332   constexpr bool u3 = p <= q; // expected-error {{constant expression}}
333   constexpr bool u4 = p >= q; // expected-error {{constant expression}}
334   constexpr bool u5 = p < 0; // expected-error {{constant expression}}
335   constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
336   constexpr bool u7 = p > 0; // expected-error {{constant expression}}
337   constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
338   constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
339   constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
340   constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
341   constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
342   void f(), g();
343 
344   constexpr void (*pf)() = &f, (*pg)() = &g;
345   constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
346   constexpr bool u14 = pf == pg;
347 
348   // FIXME:
349   // If two pointers point to non-static data members of the same object with
350   // different access control, the result is unspecified.
351 
352   // FIXME:
353   // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
354   // represent the same address or are both the null pointer [...]; otherwise
355   // the result is unspecified.
356 
357   // FIXME: Implement comparisons of pointers to members.
358   // [expr.eq]p2: If either is a pointer to a virtual member function and
359   // neither is null, the result is unspecified.
360 }
361 
362 // - an assignment or a compound assignment (5.17); or
363 namespace Assignment {
364   int k;
365   struct T {
366     int n : (k = 9); // expected-error {{constant expression}}
367     int m : (k *= 2); // expected-error {{constant expression}}
368   };
369 
370   struct Literal {
371     constexpr Literal(const char *name) : name(name) {}
372     const char *name;
373   };
374   struct Expr {
375     constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
376     bool IsLiteral;
377     union {
378       Literal l;
379       // ...
380     };
381   };
382   struct MulEq {
383     constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
384     Expr LHS;
385     Expr RHS;
386   };
387   constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
388   Literal a("a");
389   Literal b("b");
390   MulEq c = a *= b; // ok
391 }
392 
393 // - a throw-expression (15.1)
394 namespace Throw {
395   struct S {
396     int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}}
397   };
398 }
399 
400 // PR9999
401 template<unsigned int v>
402 class bitWidthHolding {
403 public:
404   static const
405   unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
406 };
407 
408 static const int width=bitWidthHolding<255>::width;
409 
410 template<bool b>
411 struct always_false {
412   static const bool value = false;
413 };
414 
415 template<bool b>
416 struct and_or {
417   static const bool and_value = b && and_or<always_false<b>::value>::and_value;
418   static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
419 };
420 
421 static const bool and_value = and_or<true>::and_value;
422 static const bool or_value = and_or<true>::or_value;
423 
424 static_assert(and_value == false, "");
425 static_assert(or_value == true, "");
426