constant-expression-cxx1y.cpp revision bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41c
1// RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu 2 3struct S { 4 // dummy ctor to make this a literal type 5 constexpr S(int); 6 7 S(); 8 9 int arr[10]; 10 11 constexpr int &get(int n) { return arr[n]; } 12 constexpr const int &get(int n) const { return arr[n]; } 13}; 14 15S s = S(); 16const S &sr = s; 17static_assert(&s.get(4) - &sr.get(2) == 2, ""); 18 19// Compound-statements can be used in constexpr functions. 20constexpr int e() {{{{}} return 5; }} 21static_assert(e() == 5, ""); 22 23// Types can be defined in constexpr functions. 24constexpr int f() { 25 enum E { e1, e2, e3 }; 26 27 struct S { 28 constexpr S(E e) : e(e) {} 29 constexpr int get() { return e; } 30 E e; 31 }; 32 33 return S(e2).get(); 34} 35static_assert(f() == 1, ""); 36 37// Variables can be declared in constexpr functions. 38constexpr int g(int k) { 39 const int n = 9; 40 int k2 = k * k; 41 int k3 = k2 * k; 42 return 3 * k3 + 5 * k2 + n * k - 20; 43} 44static_assert(g(2) == 42, ""); 45constexpr int h(int n) { 46 static const int m = n; // expected-error {{static variable not permitted in a constexpr function}} 47 return m; 48} 49constexpr int i(int n) { 50 thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}} 51 return m; 52} 53 54// if-statements can be used in constexpr functions. 55constexpr int j(int k) { 56 if (k == 5) 57 return 1; 58 if (k == 1) 59 return 5; 60 else { 61 if (int n = 2 * k - 4) { 62 return n + 1; 63 return 2; 64 } 65 } 66} // expected-note 2{{control reached end of constexpr function}} 67static_assert(j(0) == -3, ""); 68static_assert(j(1) == 5, ""); 69static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}} 70static_assert(j(3) == 3, ""); 71static_assert(j(4) == 5, ""); 72static_assert(j(5) == 1, ""); 73 74// There can be 0 return-statements. 75constexpr void k() { 76} 77 78// If the return type is not 'void', no return statements => never a constant 79// expression, so still diagnose that case. 80[[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}} 81 fn(); 82} 83 84// We evaluate the body of a constexpr constructor, to check for side-effects. 85struct U { 86 constexpr U(int n) { 87 if (j(n)) {} // expected-note {{in call to 'j(2)'}} 88 } 89}; 90constexpr U u1{1}; 91constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}} 92 93// We allow expression-statements. 94constexpr int l(bool b) { 95 if (b) 96 throw "invalid value for b!"; // expected-note {{subexpression not valid}} 97 return 5; 98} 99static_assert(l(false) == 5, ""); 100static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}} 101 102// Potential constant expression checking is still applied where possible. 103constexpr int htonl(int x) { // expected-error {{never produces a constant expression}} 104 typedef unsigned char uchar; 105 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; 106 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} 107} 108 109constexpr int maybe_htonl(bool isBigEndian, int x) { 110 if (isBigEndian) 111 return x; 112 113 typedef unsigned char uchar; 114 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; 115 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} 116} 117 118constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}} 119 120namespace NS { 121 constexpr int n = 0; 122} 123constexpr int namespace_alias() { 124 namespace N = NS; 125 return N::n; 126} 127 128namespace assign { 129 constexpr int a = 0; 130 const int b = 0; 131 int c = 0; // expected-note 2{{here}} 132 133 constexpr void set(const int &a, int b) { 134 const_cast<int&>(a) = b; // expected-note 2{{constant expression cannot modify an object that is visible outside that expression}} 135 } 136 constexpr int wrap(int a, int b) { 137 set(a, b); 138 return a; 139 } 140 141 static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}} 142 static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}} 143 static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 144 145 static_assert(wrap(a, 1) == 1, ""); 146 static_assert(wrap(b, 1) == 1, ""); 147 static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 148} 149 150namespace string_assign { 151 template<typename T> 152 constexpr void swap(T &a, T &b) { 153 T tmp = a; 154 a = b; 155 b = tmp; 156 } 157 template<typename Iterator> 158 constexpr void reverse(Iterator begin, Iterator end) { 159#if 0 // FIXME: once implementation is complete... 160 while (begin != end && begin != --end) 161 swap(*begin++, *end); 162#else 163 if (begin != end) { 164 end = end - 1; 165 if (begin == end) 166 return; 167 swap(*begin, *end); 168 begin = begin + 1; 169 reverse(begin, end); 170 } 171#endif 172 } 173 template<typename Iterator1, typename Iterator2> 174 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { 175#if 0 // FIXME: once implementation is complete... 176 while (a != ae && b != be) { 177 if (*a != *b) 178 return false; 179 ++a, ++b; 180 } 181#else 182 if (a != ae && b != be) { 183 if (*a != *b) 184 return false; 185 a = a + 1; 186 b = b + 1; 187 return equal(a, ae, b, be); 188 } 189#endif 190 return a == ae && b == be; 191 } 192 constexpr bool test1(int n) { 193 char stuff[100] = "foobarfoo"; 194 const char stuff2[100] = "oofraboof"; 195 reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} 196 return equal(stuff, stuff + n, stuff2, stuff2 + n); 197 } 198 static_assert(!test1(1), ""); 199 static_assert(test1(3), ""); 200 static_assert(!test1(6), ""); 201 static_assert(test1(9), ""); 202 static_assert(!test1(100), ""); 203 static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} 204 205 // FIXME: We should be able to reject this before it's called 206 constexpr void f() { 207 char foo[10] = { "z" }; // expected-note {{here}} 208 foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}} 209 } 210 constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}} 211} 212 213namespace array_resize { 214 constexpr int do_stuff(int k1, int k2) { 215 int arr[1234] = { 1, 2, 3, 4 }; 216 arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} 217 return arr[k2]; 218 } 219 static_assert(do_stuff(1, 2) == 3, ""); 220 static_assert(do_stuff(0, 0) == 5, ""); 221 static_assert(do_stuff(1233, 1233) == 5, ""); 222 static_assert(do_stuff(1233, 0) == 1, ""); 223 static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 224 static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 225 static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 226} 227 228namespace potential_const_expr { 229 constexpr void set(int &n) { n = 1; } 230 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error 231 constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} 232 int z = 0; 233 return 100 / (set(z), 0); // expected-note {{division by zero}} 234 } 235} 236 237namespace subobject { 238 union A { constexpr A() : y(5) {} int x, y; }; 239 struct B { A a; }; 240 struct C : B {}; 241 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; 242 constexpr void f(D &d) { 243 d.c.a.y = 3; 244 // expected-note@-1 {{cannot modify an object that is visible outside}} 245 // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} 246 } 247 constexpr bool check(D &d) { return d.c.a.y == 3; } 248 249 constexpr bool g() { D d; f(d); return d.c.a.y == 3; } 250 static_assert(g(), ""); 251 252 D d; 253 constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} 254 static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} 255 256 constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} 257 static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} 258 259 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'}} 260 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} 261} 262 263namespace lifetime { 264 constexpr int &&id(int &&n) { return static_cast<int&&>(n); } 265 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} 266 constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} 267 static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} 268} 269 270namespace const_modify { 271 constexpr int modify(int &n) { return n = 1; } // expected-note {{modification of object of const-qualified type 'const int'}} 272 constexpr int test1() { int k = 0; return modify(k); } 273 constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note {{in call}} 274 static_assert(test1() == 1, ""); 275 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 276} 277 278namespace null { 279 constexpr int test(int *p) { 280 return *p = 123; // expected-note {{assignment to dereferenced null pointer}} 281 } 282 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} 283} 284