blob: 78d78b64c7aeeabd92a9d09f1311dd0cef8a05e9 [file] [log] [blame]
Richard Smith864711ee2011-12-16 21:59:02 +00001// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128
Richard Smith357362d2011-12-13 06:39:58 +00002
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];
10struct 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 ];
23struct 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};
27struct NonConstexpr2 {
28 constexpr NonConstexpr2(); // expected-note {{here}}
29 int n;
30};
31struct NonConstexpr3 {
32 NonConstexpr3();
33 int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34};
35struct NonConstexpr4 {
Richard Smithfddd3842011-12-30 21:15:51 +000036 NonConstexpr4(); // expected-note {{declared here}}
Richard Smith357362d2011-12-13 06:39:58 +000037 int n;
38};
39struct NonConstexpr5 {
Richard Smithfddd3842011-12-30 21:15:51 +000040 int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +000041};
42
43// - an invocation of an undefined constexpr function or an undefined
44// constexpr constructor;
45struct 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;
54namespace 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 {
Richard Smithf6f003a2011-12-16 19:06:07 +000059 int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
Richard Smith357362d2011-12-13 06:39:58 +000060 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}}
Richard Smithf6f003a2011-12-16 19:06:07 +000067 return address_of(n); // expected-note {{in call to 'address_of(n)'}}
Richard Smith357362d2011-12-13 06:39:58 +000068 }
69 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +000070 int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
Richard Smith357362d2011-12-13 06:39:58 +000071 };
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);
78namespace NonConstExprCtor {
79 struct T {
80 constexpr T(const int &r) :
Richard Smithd0b4dd62011-12-19 06:19:21 +000081 r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +000082 }
83 const int &r;
84 };
85 constexpr int n = 0;
86 constexpr T t1(n); // ok
Richard Smithd0b4dd62011-12-19 06:19:21 +000087 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)'}}
Richard Smith357362d2011-12-13 06:39:58 +000088
89 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +000090 int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
Richard Smith357362d2011-12-13 06:39:58 +000091 };
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);
96namespace RecursionLimits {
97 constexpr int RecurseForever(int n) {
Richard Smith864711ee2011-12-16 21:59:02 +000098 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}}
Richard Smith357362d2011-12-13 06:39:58 +000099 }
100 struct AlsoRecurseForever {
101 constexpr AlsoRecurseForever(int n) :
Richard Smith864711ee2011-12-16 21:59:02 +0000102 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}}
Richard Smith357362d2011-12-13 06:39:58 +0000103 {}
104 int n;
105 };
106 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +0000107 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}}
Richard Smith357362d2011-12-13 06:39:58 +0000109 };
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];
117namespace 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 (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 struct T {
Richard Smithf6f003a2011-12-16 19:06:07 +0000138 int n : f(p); // expected-error {{not an integer constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
Richard Smith357362d2011-12-13 06:39:58 +0000139 };
Richard Smitha8105bc2012-01-06 16:39:00 +0000140
141 namespace Ptr {
142 struct A {};
143 struct B : A { int n; };
144 B a[3][3];
145 constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
146 B b = {};
147 constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
148 constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
149 constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
150 constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
151
152 constexpr A *na = nullptr;
153 constexpr B *nb = nullptr;
154 constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
155 constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
156 static_assert((A*)nb == 0, "");
157 static_assert((B*)na == 0, "");
158 constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
159 constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
160 }
Richard Smith357362d2011-12-13 06:39:58 +0000161}
162
163// - a lambda-expression (5.1.2);
164struct Lambda {
165 // FIXME: clang crashes when trying to parse this! Revisit this check once
166 // lambdas are fully implemented.
167 //int n : []{ return 1; }();
168};
169
Richard Smith357362d2011-12-13 06:39:58 +0000170// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
Richard Smithf2b681b2011-12-21 05:04:46 +0000171namespace LValueToRValue {
172 // - a non-volatile glvalue of integral or enumeration type that refers to a
173 // non-volatile const object with a preceding initialization, initialized
174 // with a constant expression [Note: a string literal (2.14.5 [lex.string])
175 // corresponds to an array of such objects. -end note], or
176 volatile const int vi = 1; // expected-note {{here}}
177 const int ci = 1;
178 volatile const int &vrci = ci;
179 static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
180 static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
181 static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
182
183 // - a non-volatile glvalue of literal type that refers to a non-volatile
184 // object defined with constexpr, or that refers to a sub-object of such an
185 // object, or
186 struct S {
187 constexpr S(int=0) : i(1), v(1) {}
188 constexpr S(const S &s) : i(2), v(2) {}
189 int i;
190 volatile int v;
191 };
192 constexpr S s;
193 constexpr volatile S vs; // expected-note {{here}}
194 constexpr const volatile S &vrs = s;
195 static_assert(s.i, "");
196 static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
197 static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
198 static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
199 static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
200
201 // - a non-volatile glvalue of literal type that refers to a non-volatile
202 // temporary object whose lifetime has not ended, initialized with a
203 // constant expression;
204 constexpr volatile S f() { return S(); }
205 static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
206 static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
207}
Richard Smith357362d2011-12-13 06:39:58 +0000208
209// FIXME:
210//
Richard Smithf2b681b2011-12-21 05:04:46 +0000211// DR1312: The proposed wording for this defect has issues, so we ignore this
212// bullet and instead prohibit casts from pointers to cv void (see core-20842
213// and core-20845).
Richard Smith357362d2011-12-13 06:39:58 +0000214//
215// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
216// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
217// are neither the same type nor similar types (4.4 [conv.qual]);
218
219// FIXME:
220// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
221// refers to a non-active member of a union or a subobject thereof;
222
Richard Smith357362d2011-12-13 06:39:58 +0000223// - an id-expression that refers to a variable or data member of reference type
224// unless the reference has a preceding initialization, initialized with a
225// constant expression;
226namespace References {
227 const int a = 2;
228 int &b = *const_cast<int*>(&a);
Richard Smithf2b681b2011-12-21 05:04:46 +0000229 int c = 10; // expected-note 2 {{here}}
Richard Smith357362d2011-12-13 06:39:58 +0000230 int &d = c;
231 constexpr int e = 42;
232 int &f = const_cast<int&>(e);
233 extern int &g;
Richard Smithd0b4dd62011-12-19 06:19:21 +0000234 constexpr int &h(); // expected-note 2{{here}}
235 int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000236 constexpr int &j() { return b; }
237 int &k = j();
238
239 struct S {
240 int A : a;
241 int B : b;
Richard Smithf2b681b2011-12-21 05:04:46 +0000242 int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
243 int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
Richard Smith357362d2011-12-13 06:39:58 +0000244 int D2 : &d - &c + 1;
245 int E : e / 2;
246 int F : f - 11;
247 int G : g; // expected-error {{constant expression}}
248 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
Richard Smithd0b4dd62011-12-19 06:19:21 +0000249 int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000250 int J : j();
251 int K : k;
252 };
253}
254
255// - a dynamic_cast (5.2.7);
256namespace DynamicCast {
257 struct S { int n; };
258 constexpr S s { 16 };
259 struct T {
260 int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
261 };
262}
263
264// - a reinterpret_cast (5.2.10);
265namespace ReinterpretCast {
266 struct S { int n; };
267 constexpr S s { 16 };
268 struct T {
269 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
270 };
271 struct U {
272 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
273 };
274}
275
276// - a pseudo-destructor call (5.2.4);
277namespace PseudoDtor {
278 int k;
279 typedef int I;
280 struct T {
281 int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
282 };
283}
284
285// - increment or decrement operations (5.2.6, 5.3.2);
286namespace IncDec {
287 int k = 2;
288 struct T {
289 int n : ++k; // expected-error {{constant expression}}
290 int m : --k; // expected-error {{constant expression}}
291 };
292}
293
294// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
295namespace std {
296 struct type_info {
297 virtual ~type_info();
298 const char *name;
299 };
300}
301namespace TypeId {
302 struct S { virtual void f(); };
303 constexpr S *p = 0;
Richard Smith6e525142011-12-27 12:18:28 +0000304 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'}}
Richard Smith357362d2011-12-13 06:39:58 +0000305
Richard Smith357362d2011-12-13 06:39:58 +0000306 struct T {} t;
Richard Smith6e525142011-12-27 12:18:28 +0000307 constexpr const std::type_info &ti2 = typeid(t);
Richard Smith357362d2011-12-13 06:39:58 +0000308}
309
310// - a new-expression (5.3.4);
311// - a delete-expression (5.3.5);
312namespace NewDelete {
313 int *p = 0;
314 struct T {
315 int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}}
316 int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}}
317 };
318}
319
320// - a relational (5.9) or equality (5.10) operator where the result is
321// unspecified;
322namespace UnspecifiedRelations {
323 int a, b;
324 constexpr int *p = &a, *q = &b;
325 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
326 // different objects that are not members of the same array or to different
327 // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
328 // and p>=q are unspecified.
329 constexpr bool u1 = p < q; // expected-error {{constant expression}}
330 constexpr bool u2 = p > q; // expected-error {{constant expression}}
331 constexpr bool u3 = p <= q; // expected-error {{constant expression}}
332 constexpr bool u4 = p >= q; // expected-error {{constant expression}}
333 constexpr bool u5 = p < 0; // expected-error {{constant expression}}
334 constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
335 constexpr bool u7 = p > 0; // expected-error {{constant expression}}
336 constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
337 constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
338 constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
339 constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
340 constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
341 void f(), g();
342
343 constexpr void (*pf)() = &f, (*pg)() = &g;
344 constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
345 constexpr bool u14 = pf == pg;
346
347 // FIXME:
348 // If two pointers point to non-static data members of the same object with
349 // different access control, the result is unspecified.
350
351 // FIXME:
352 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
353 // represent the same address or are both the null pointer [...]; otherwise
354 // the result is unspecified.
355
356 // FIXME: Implement comparisons of pointers to members.
357 // [expr.eq]p2: If either is a pointer to a virtual member function and
358 // neither is null, the result is unspecified.
359}
360
361// - an assignment or a compound assignment (5.17); or
362namespace Assignment {
363 int k;
364 struct T {
365 int n : (k = 9); // expected-error {{constant expression}}
366 int m : (k *= 2); // expected-error {{constant expression}}
367 };
368
369 struct Literal {
370 constexpr Literal(const char *name) : name(name) {}
371 const char *name;
372 };
373 struct Expr {
374 constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
375 bool IsLiteral;
376 union {
377 Literal l;
378 // ...
379 };
380 };
381 struct MulEq {
382 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
383 Expr LHS;
384 Expr RHS;
385 };
386 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
387 Literal a("a");
388 Literal b("b");
389 MulEq c = a *= b; // ok
390}
391
392// - a throw-expression (15.1)
393namespace Throw {
394 struct S {
395 int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}}
396 };
397}
Douglas Gregorfcafc6e2011-05-24 16:02:01 +0000398
399// PR9999
400template<bool v>
401class bitWidthHolding {
402public:
403 static const
404 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
405};
406
407static const int width=bitWidthHolding<255>::width;
408
409template<bool b>
410struct always_false {
411 static const bool value = false;
412};
413
414template<bool b>
415struct and_or {
416 static const bool and_value = b && and_or<always_false<b>::value>::and_value;
417 static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
418};
419
420static const bool and_value = and_or<true>::and_value;
421static const bool or_value = and_or<true>::or_value;