| 1 |
// RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify |
| 2 |
|
| 3 |
typedef __SIZE_TYPE__ size_t; |
| 4 |
|
| 5 |
namespace basic_sema { |
| 6 |
|
| 7 |
consteval int f1(int i) { |
| 8 |
return i; |
| 9 |
} |
| 10 |
|
| 11 |
consteval constexpr int f2(int i) { |
| 12 |
//expected-error@-1 {{cannot combine}} |
| 13 |
return i; |
| 14 |
} |
| 15 |
|
| 16 |
constexpr auto l_eval = [](int i) consteval { |
| 17 |
// expected-note@-1+ {{declared here}} |
| 18 |
|
| 19 |
return i; |
| 20 |
}; |
| 21 |
|
| 22 |
constexpr consteval int f3(int i) { |
| 23 |
//expected-error@-1 {{cannot combine}} |
| 24 |
return i; |
| 25 |
} |
| 26 |
|
| 27 |
struct A { |
| 28 |
consteval int f1(int i) const { |
| 29 |
// expected-note@-1 {{declared here}} |
| 30 |
return i; |
| 31 |
} |
| 32 |
consteval A(int i); |
| 33 |
consteval A() = default; |
| 34 |
consteval ~A() = default; // expected-error {{destructor cannot be declared consteval}} |
| 35 |
}; |
| 36 |
|
| 37 |
consteval struct B {}; // expected-error {{struct cannot be marked consteval}} |
| 38 |
|
| 39 |
consteval typedef B b; // expected-error {{typedef cannot be consteval}} |
| 40 |
|
| 41 |
consteval int redecl() {return 0;} // expected-note {{previous declaration is here}} |
| 42 |
constexpr int redecl() {return 0;} // expected-error {{constexpr declaration of 'redecl' follows consteval declaration}} |
| 43 |
|
| 44 |
consteval int i = 0; // expected-error {{consteval can only be used in function declarations}} |
| 45 |
|
| 46 |
consteval int; // expected-error {{consteval can only be used in function declarations}} |
| 47 |
|
| 48 |
consteval int f1() {} // expected-error {{no return statement in consteval function}} |
| 49 |
|
| 50 |
struct C { |
| 51 |
C() {} |
| 52 |
~C() {} |
| 53 |
}; |
| 54 |
|
| 55 |
struct D { |
| 56 |
C c; |
| 57 |
consteval D() = default; // expected-error {{cannot be marked consteval}} |
| 58 |
consteval ~D() = default; // expected-error {{destructor cannot be declared consteval}} |
| 59 |
}; |
| 60 |
|
| 61 |
struct E : C { |
| 62 |
consteval ~E() {} // expected-error {{cannot be declared consteval}} |
| 63 |
}; |
| 64 |
} |
| 65 |
|
| 66 |
consteval int main() { // expected-error {{'main' is not allowed to be declared consteval}} |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
consteval int f_eval(int i) { |
| 71 |
// expected-note@-1+ {{declared here}} |
| 72 |
return i; |
| 73 |
} |
| 74 |
|
| 75 |
namespace taking_address { |
| 76 |
|
| 77 |
using func_type = int(int); |
| 78 |
|
| 79 |
func_type* p1 = (&f_eval); |
| 80 |
// expected-error@-1 {{take address}} |
| 81 |
func_type* p7 = __builtin_addressof(f_eval); |
| 82 |
// expected-error@-1 {{take address}} |
| 83 |
|
| 84 |
auto p = f_eval; |
| 85 |
// expected-error@-1 {{take address}} |
| 86 |
|
| 87 |
auto m1 = &basic_sema::A::f1; |
| 88 |
// expected-error@-1 {{take address}} |
| 89 |
auto l1 = &decltype(basic_sema::l_eval)::operator(); |
| 90 |
// expected-error@-1 {{take address}} |
| 91 |
|
| 92 |
consteval int f(int i) { |
| 93 |
// expected-note@-1+ {{declared here}} |
| 94 |
return i; |
| 95 |
} |
| 96 |
|
| 97 |
auto ptr = &f; |
| 98 |
// expected-error@-1 {{take address}} |
| 99 |
|
| 100 |
auto f1() { |
| 101 |
return &f; |
| 102 |
// expected-error@-1 {{take address}} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
namespace invalid_function { |
| 108 |
|
| 109 |
struct A { |
| 110 |
consteval void *operator new(size_t count); |
| 111 |
// expected-error@-1 {{'operator new' cannot be declared consteval}} |
| 112 |
consteval void *operator new[](size_t count); |
| 113 |
// expected-error@-1 {{'operator new[]' cannot be declared consteval}} |
| 114 |
consteval void operator delete(void* ptr); |
| 115 |
// expected-error@-1 {{'operator delete' cannot be declared consteval}} |
| 116 |
consteval void operator delete[](void* ptr); |
| 117 |
// expected-error@-1 {{'operator delete[]' cannot be declared consteval}} |
| 118 |
consteval ~A() {} |
| 119 |
// expected-error@-1 {{destructor cannot be declared consteval}} |
| 120 |
}; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
namespace nested { |
| 125 |
consteval int f() { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
consteval int f1(...) { |
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
enum E {}; |
| 134 |
|
| 135 |
using T = int(&)(); |
| 136 |
|
| 137 |
consteval auto operator+ (E, int(*a)()) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
void d() { |
| 142 |
auto i = f1(E() + &f); |
| 143 |
} |
| 144 |
|
| 145 |
auto l0 = [](auto) consteval { |
| 146 |
return 0; |
| 147 |
}; |
| 148 |
|
| 149 |
int i0 = l0(&f1); |
| 150 |
|
| 151 |
int i1 = f1(l0(4)); |
| 152 |
|
| 153 |
int i2 = f1(&f1, &f1, &f1, &f1, &f1, &f1, &f1); |
| 154 |
|
| 155 |
int i3 = f1(f1(f1(&f1, &f1), f1(&f1, &f1), f1(f1(&f1, &f1), &f1))); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
namespace user_defined_literal { |
| 160 |
|
| 161 |
consteval int operator""_test(unsigned long long i) { |
| 162 |
// expected-note@-1+ {{declared here}} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
int i = 0_test; |
| 167 |
|
| 168 |
auto ptr = &operator""_test; |
| 169 |
// expected-error@-1 {{take address}} |
| 170 |
|
| 171 |
consteval auto operator""_test1(unsigned long long i) { |
| 172 |
return &f_eval; |
| 173 |
} |
| 174 |
|
| 175 |
auto i1 = 0_test1; // expected-error {{is not a constant expression}} |
| 176 |
// expected-note@-1 {{is not a constant expression}} |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
namespace return_address { |
| 181 |
|
| 182 |
consteval int f() { |
| 183 |
// expected-note@-1 {{declared here}} |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
consteval int(*ret1(int i))() { |
| 188 |
return &f; |
| 189 |
} |
| 190 |
|
| 191 |
auto ptr = ret1(0); |
| 192 |
// expected-error@-1 {{is not a constant expression}} |
| 193 |
// expected-note@-2 {{pointer to a consteval}} |
| 194 |
|
| 195 |
struct A { |
| 196 |
consteval int f(int) { |
| 197 |
// expected-note@-1+ {{declared here}} |
| 198 |
return 0; |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
using mem_ptr_type = int (A::*)(int); |
| 203 |
|
| 204 |
template<mem_ptr_type ptr> |
| 205 |
struct C {}; |
| 206 |
|
| 207 |
C<&A::f> c; |
| 208 |
// expected-error@-1 {{is not a constant expression}} |
| 209 |
// expected-note@-2 {{pointer to a consteval}} |
| 210 |
|
| 211 |
consteval mem_ptr_type ret2() { |
| 212 |
return &A::f; |
| 213 |
} |
| 214 |
|
| 215 |
C<ret2()> c1; |
| 216 |
// expected-error@-1 {{is not a constant expression}} |
| 217 |
// expected-note@-2 {{pointer to a consteval}} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
namespace context { |
| 222 |
|
| 223 |
int g_i; |
| 224 |
// expected-note@-1 {{declared here}} |
| 225 |
|
| 226 |
consteval int f(int) { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
constexpr int c_i = 0; |
| 231 |
|
| 232 |
int t1 = f(g_i); |
| 233 |
// expected-error@-1 {{is not a constant expression}} |
| 234 |
// expected-note@-2 {{read of non-const variable}} |
| 235 |
int t3 = f(c_i); |
| 236 |
|
| 237 |
constexpr int f_c(int i) { |
| 238 |
// expected-note@-1 {{declared here}} |
| 239 |
int t = f(i); |
| 240 |
// expected-error@-1 {{is not a constant expression}} |
| 241 |
// expected-note@-2 {{function parameter}} |
| 242 |
return f(0); |
| 243 |
} |
| 244 |
|
| 245 |
consteval int f_eval(int i) { |
| 246 |
return f(i); |
| 247 |
} |
| 248 |
|
| 249 |
auto l0 = [](int i) consteval { |
| 250 |
return f(i); |
| 251 |
}; |
| 252 |
|
| 253 |
auto l1 = [](int i) constexpr { // expected-error{{cannot take address of immediate call operator}} \ |
| 254 |
// expected-note {{declared here}} |
| 255 |
int t = f(i); |
| 256 |
return f(0); |
| 257 |
}; |
| 258 |
|
| 259 |
int(*test)(int) = l1; |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
namespace consteval_lambda_in_template { |
| 264 |
struct S { |
| 265 |
int *value; |
| 266 |
constexpr S(int v) : value(new int {v}) {} |
| 267 |
constexpr ~S() { delete value; } |
| 268 |
}; |
| 269 |
consteval S fn() { return S(5); } |
| 270 |
|
| 271 |
template <typename T> |
| 272 |
void fn2() { |
| 273 |
(void)[]() consteval -> int { |
| 274 |
return *(fn().value); // OK, immediate context |
| 275 |
}; |
| 276 |
} |
| 277 |
|
| 278 |
void caller() { |
| 279 |
fn2<int>(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
namespace std { |
| 284 |
|
| 285 |
template <typename T> struct remove_reference { using type = T; }; |
| 286 |
template <typename T> struct remove_reference<T &> { using type = T; }; |
| 287 |
template <typename T> struct remove_reference<T &&> { using type = T; }; |
| 288 |
|
| 289 |
template <typename T> |
| 290 |
constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept { |
| 291 |
return static_cast<typename std::remove_reference<T>::type &&>(t); |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
namespace temporaries { |
| 297 |
|
| 298 |
struct A { |
| 299 |
consteval int ret_i() const { return 0; } |
| 300 |
consteval A ret_a() const { return A{}; } |
| 301 |
constexpr ~A() { } |
| 302 |
}; |
| 303 |
|
| 304 |
consteval int by_value_a(A a) { return a.ret_i(); } |
| 305 |
|
| 306 |
consteval int const_a_ref(const A &a) { |
| 307 |
return a.ret_i(); |
| 308 |
} |
| 309 |
|
| 310 |
consteval int rvalue_ref(const A &&a) { |
| 311 |
return a.ret_i(); |
| 312 |
} |
| 313 |
|
| 314 |
consteval const A &to_lvalue_ref(const A &&a) { |
| 315 |
return a; |
| 316 |
} |
| 317 |
|
| 318 |
void test() { |
| 319 |
constexpr A a {}; |
| 320 |
{ int k = A().ret_i(); } |
| 321 |
{ A k = A().ret_a(); } |
| 322 |
{ A k = to_lvalue_ref(A()); }// expected-error {{is not a constant expression}} |
| 323 |
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} |
| 324 |
{ A k = to_lvalue_ref(A().ret_a()); } // expected-error {{is not a constant expression}} |
| 325 |
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} |
| 326 |
{ int k = A().ret_a().ret_i(); } |
| 327 |
{ int k = by_value_a(A()); } |
| 328 |
{ int k = const_a_ref(A()); } |
| 329 |
{ int k = const_a_ref(a); } |
| 330 |
{ int k = rvalue_ref(A()); } |
| 331 |
{ int k = rvalue_ref(std::move(a)); } |
| 332 |
{ int k = const_a_ref(A().ret_a()); } |
| 333 |
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); } |
| 334 |
{ int k = const_a_ref(to_lvalue_ref(std::move(a))); } |
| 335 |
{ int k = by_value_a(A().ret_a()); } |
| 336 |
{ int k = by_value_a(to_lvalue_ref(std::move(a))); } |
| 337 |
{ int k = (A().ret_a(), A().ret_i()); } |
| 338 |
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); }// |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
namespace alloc { |
| 344 |
|
| 345 |
consteval int f() { |
| 346 |
int *A = new int(0); |
| 347 |
// expected-note@-1+ {{allocation performed here was not deallocated}} |
| 348 |
return *A; |
| 349 |
} |
| 350 |
|
| 351 |
int i1 = f(); // expected-error {{is not a constant expression}} |
| 352 |
|
| 353 |
struct A { |
| 354 |
int* p = new int(42); |
| 355 |
// expected-note@-1+ {{heap allocation performed here}} |
| 356 |
consteval int ret_i() const { return p ? *p : 0; } |
| 357 |
consteval A ret_a() const { return A{}; } |
| 358 |
constexpr ~A() { delete p; } |
| 359 |
}; |
| 360 |
|
| 361 |
consteval int by_value_a(A a) { return a.ret_i(); } |
| 362 |
|
| 363 |
consteval int const_a_ref(const A &a) { |
| 364 |
return a.ret_i(); |
| 365 |
} |
| 366 |
|
| 367 |
consteval int rvalue_ref(const A &&a) { |
| 368 |
return a.ret_i(); |
| 369 |
} |
| 370 |
|
| 371 |
consteval const A &to_lvalue_ref(const A &&a) { |
| 372 |
return a; |
| 373 |
} |
| 374 |
|
| 375 |
void test() { |
| 376 |
constexpr A a{ nullptr }; |
| 377 |
{ int k = A().ret_i(); } |
| 378 |
{ A k = A().ret_a(); } // expected-error {{is not a constant expression}} |
| 379 |
// expected-note@-1 {{is not a constant expression}} |
| 380 |
{ A k = to_lvalue_ref(A()); } // expected-error {{is not a constant expression}} |
| 381 |
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} |
| 382 |
{ A k = to_lvalue_ref(A().ret_a()); } |
| 383 |
// expected-note@-1 {{reference to temporary is not a constant expression}} |
| 384 |
// expected-error@-2 {{'alloc::to_lvalue_ref' is not a constant expression}} |
| 385 |
// expected-note@-3 {{temporary created here}} |
| 386 |
{ int k = A().ret_a().ret_i(); } |
| 387 |
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} |
| 388 |
// expected-note@-2 {{heap-allocated object is not a constant expression}} |
| 389 |
{ int k = by_value_a(A()); } |
| 390 |
{ int k = const_a_ref(A()); } |
| 391 |
{ int k = const_a_ref(a); } |
| 392 |
{ int k = rvalue_ref(A()); } |
| 393 |
{ int k = rvalue_ref(std::move(a)); } |
| 394 |
{ int k = const_a_ref(A().ret_a()); } |
| 395 |
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); } |
| 396 |
{ int k = const_a_ref(to_lvalue_ref(std::move(a))); } |
| 397 |
{ int k = by_value_a(A().ret_a()); } |
| 398 |
{ int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); } |
| 399 |
{ int k = (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}} |
| 400 |
// expected-note@-1 {{is not a constant expression}} |
| 401 |
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); } |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
namespace self_referencing { |
| 407 |
|
| 408 |
struct S { |
| 409 |
S* ptr = nullptr; |
| 410 |
constexpr S(int i) : ptr(this) { |
| 411 |
if (this == ptr && i) |
| 412 |
ptr = nullptr; |
| 413 |
} |
| 414 |
constexpr ~S() {} |
| 415 |
}; |
| 416 |
|
| 417 |
consteval S f(int i) { |
| 418 |
return S(i); |
| 419 |
} |
| 420 |
|
| 421 |
void test() { |
| 422 |
S s(1); |
| 423 |
s = f(1); |
| 424 |
s = f(0); // expected-error {{is not a constant expression}} |
| 425 |
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} |
| 426 |
} |
| 427 |
|
| 428 |
struct S1 { |
| 429 |
S1* ptr = nullptr; |
| 430 |
consteval S1(int i) : ptr(this) { |
| 431 |
if (this == ptr && i) |
| 432 |
ptr = nullptr; |
| 433 |
} |
| 434 |
constexpr ~S1() {} |
| 435 |
}; |
| 436 |
|
| 437 |
void test1() { |
| 438 |
S1 s(1); |
| 439 |
s = S1(1); |
| 440 |
s = S1(0); // expected-error {{is not a constant expression}} |
| 441 |
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} |
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
namespace ctor { |
| 446 |
|
| 447 |
consteval int f_eval() { // expected-note+ {{declared here}} |
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
namespace std { |
| 452 |
struct strong_ordering { |
| 453 |
int n; |
| 454 |
static const strong_ordering less, equal, greater; |
| 455 |
}; |
| 456 |
constexpr strong_ordering strong_ordering::less = {-1}; |
| 457 |
constexpr strong_ordering strong_ordering::equal = {0}; |
| 458 |
constexpr strong_ordering strong_ordering::greater = {1}; |
| 459 |
constexpr bool operator!=(strong_ordering, int); |
| 460 |
} |
| 461 |
|
| 462 |
namespace override { |
| 463 |
struct A { |
| 464 |
virtual consteval void f(); // expected-note {{overridden}} |
| 465 |
virtual void g(); // expected-note {{overridden}} |
| 466 |
}; |
| 467 |
struct B : A { |
| 468 |
consteval void f(); |
| 469 |
void g(); |
| 470 |
}; |
| 471 |
struct C : A { |
| 472 |
void f(); // expected-error {{non-consteval function 'f' cannot override a consteval function}} |
| 473 |
consteval void g(); // expected-error {{consteval function 'g' cannot override a non-consteval function}} |
| 474 |
}; |
| 475 |
|
| 476 |
namespace implicit_equals_1 { |
| 477 |
struct Y; |
| 478 |
struct X { |
| 479 |
std::strong_ordering operator<=>(const X&) const; |
| 480 |
constexpr bool operator==(const X&) const; |
| 481 |
virtual consteval bool operator==(const Y&) const; // expected-note {{here}} |
| 482 |
}; |
| 483 |
struct Y : X { |
| 484 |
std::strong_ordering operator<=>(const Y&) const = default; |
| 485 |
// expected-error@-1 {{non-consteval function 'operator==' cannot override a consteval function}} |
| 486 |
}; |
| 487 |
} |
| 488 |
|
| 489 |
namespace implicit_equals_2 { |
| 490 |
struct Y; |
| 491 |
struct X { |
| 492 |
constexpr std::strong_ordering operator<=>(const X&) const; |
| 493 |
constexpr bool operator==(const X&) const; |
| 494 |
virtual bool operator==(const Y&) const; // expected-note {{here}} |
| 495 |
}; |
| 496 |
struct Y : X { |
| 497 |
consteval std::strong_ordering operator<=>(const Y&) const = default; |
| 498 |
// expected-error@-1 {{consteval function 'operator==' cannot override a non-consteval function}} |
| 499 |
}; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
namespace operator_rewrite { |
| 504 |
struct A { |
| 505 |
friend consteval int operator<=>(const A&, const A&) { return 0; } |
| 506 |
}; |
| 507 |
const bool k = A() < A(); |
| 508 |
static_assert(!k); |
| 509 |
|
| 510 |
A a; |
| 511 |
bool k2 = A() < a; // OK, does not access 'a'. |
| 512 |
|
| 513 |
struct B { |
| 514 |
friend consteval int operator<=>(const B &l, const B &r) { return r.n - l.n; } // expected-note {{read of }} |
| 515 |
int n; |
| 516 |
}; |
| 517 |
static_assert(B() >= B()); |
| 518 |
B b; // expected-note {{here}} |
| 519 |
bool k3 = B() < b; // expected-error-re {{call to consteval function '{{.*}}::operator<=>' is not a constant expression}} expected-note {{in call}} |
| 520 |
} |
| 521 |
|
| 522 |
struct A { |
| 523 |
int(*ptr)(); |
| 524 |
consteval A(int(*p)() = nullptr) : ptr(p) {} |
| 525 |
}; |
| 526 |
|
| 527 |
struct B { |
| 528 |
int(*ptr)(); |
| 529 |
B() : ptr(nullptr) {} |
| 530 |
consteval B(int(*p)(), int) : ptr(p) {} |
| 531 |
}; |
| 532 |
|
| 533 |
void test() { |
| 534 |
{ A a; } |
| 535 |
{ A a(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 536 |
{ B b(nullptr, 0); } |
| 537 |
{ B b(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 538 |
{ A a{}; } |
| 539 |
{ A a{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 540 |
{ B b{nullptr, 0}; } |
| 541 |
{ B b{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 542 |
{ A a = A(); } |
| 543 |
{ A a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 544 |
{ B b = B(nullptr, 0); } |
| 545 |
{ B b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 546 |
{ A a = A{}; } |
| 547 |
{ A a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 548 |
{ B b = B{nullptr, 0}; } |
| 549 |
{ B b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 550 |
{ A a; a = A(); } |
| 551 |
{ A a; a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 552 |
{ B b; b = B(nullptr, 0); } |
| 553 |
{ B b; b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 554 |
{ A a; a = A{}; } |
| 555 |
{ A a; a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 556 |
{ B b; b = B{nullptr, 0}; } |
| 557 |
{ B b; b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 558 |
{ A* a; a = new A(); } |
| 559 |
{ A* a; a = new A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 560 |
{ B* b; b = new B(nullptr, 0); } |
| 561 |
{ B* b; b = new B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 562 |
{ A* a; a = new A{}; } |
| 563 |
{ A* a; a = new A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 564 |
{ B* b; b = new B{nullptr, 0}; } |
| 565 |
{ B* b; b = new B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 566 |
} |
| 567 |
|
| 568 |
} |
| 569 |
|
| 570 |
namespace copy_ctor { |
| 571 |
|
| 572 |
consteval int f_eval() { // expected-note+ {{declared here}} |
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
struct Copy { |
| 577 |
int(*ptr)(); |
| 578 |
constexpr Copy(int(*p)() = nullptr) : ptr(p) {} |
| 579 |
consteval Copy(const Copy&) = default; |
| 580 |
}; |
| 581 |
|
| 582 |
constexpr const Copy &to_lvalue_ref(const Copy &&a) { |
| 583 |
return a; |
| 584 |
} |
| 585 |
|
| 586 |
void test() { |
| 587 |
constexpr const Copy C; |
| 588 |
// there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision. |
| 589 |
// so we need to test with both prvalue and xvalues. |
| 590 |
{ Copy c(C); } |
| 591 |
{ Copy c((Copy(&f_eval))); }// expected-error {{cannot take address of consteval}} |
| 592 |
{ Copy c(std::move(C)); } |
| 593 |
{ Copy c(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 594 |
{ Copy c(to_lvalue_ref((Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 595 |
{ Copy c(to_lvalue_ref(std::move(C))); } |
| 596 |
{ Copy c(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 597 |
{ Copy c = Copy(C); } |
| 598 |
{ Copy c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} |
| 599 |
{ Copy c = Copy(std::move(C)); } |
| 600 |
{ Copy c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 601 |
{ Copy c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 602 |
{ Copy c = Copy(to_lvalue_ref(std::move(C))); } |
| 603 |
{ Copy c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 604 |
{ Copy c; c = Copy(C); } |
| 605 |
{ Copy c; c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} |
| 606 |
{ Copy c; c = Copy(std::move(C)); } |
| 607 |
{ Copy c; c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 608 |
{ Copy c; c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 609 |
{ Copy c; c = Copy(to_lvalue_ref(std::move(C))); } |
| 610 |
{ Copy c; c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 611 |
{ Copy* c; c = new Copy(C); } |
| 612 |
{ Copy* c; c = new Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} |
| 613 |
{ Copy* c; c = new Copy(std::move(C)); } |
| 614 |
{ Copy* c; c = new Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 615 |
{ Copy* c; c = new Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 616 |
{ Copy* c; c = new Copy(to_lvalue_ref(std::move(C))); } |
| 617 |
{ Copy* c; c = new Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} |
| 618 |
} |
| 619 |
|
| 620 |
} // namespace special_ctor |
| 621 |
|
| 622 |
namespace unevaluated { |
| 623 |
|
| 624 |
template <typename T, typename U> struct is_same { static const bool value = false; }; |
| 625 |
template <typename T> struct is_same<T, T> { static const bool value = true; }; |
| 626 |
|
| 627 |
long f(); // expected-note {{declared here}} |
| 628 |
auto consteval g(auto a) { |
| 629 |
return a; |
| 630 |
} |
| 631 |
|
| 632 |
auto e = g(f()); // expected-error {{is not a constant expression}} |
| 633 |
// expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}} |
| 634 |
|
| 635 |
using T = decltype(g(f())); |
| 636 |
static_assert(is_same<long, T>::value); |
| 637 |
|
| 638 |
} // namespace unevaluated |
| 639 |
|
| 640 |
namespace value_dependent { |
| 641 |
|
| 642 |
consteval int foo(int x) { |
| 643 |
return x; |
| 644 |
} |
| 645 |
|
| 646 |
template <int X> constexpr int bar() { |
| 647 |
// Previously this call was rejected as value-dependent constant expressions |
| 648 |
// can't be immediately evaluated. Now we show that we don't immediately |
| 649 |
// evaluate them until they are instantiated. |
| 650 |
return foo(X); |
| 651 |
} |
| 652 |
|
| 653 |
template <typename T> constexpr int baz() { |
| 654 |
constexpr int t = sizeof(T); |
| 655 |
// Previously this call was rejected as `t` is value-dependent and its value |
| 656 |
// is unknown until the function is instantiated. Now we show that we don't |
| 657 |
// reject such calls. |
| 658 |
return foo(t); |
| 659 |
} |
| 660 |
|
| 661 |
static_assert(bar<15>() == 15); |
| 662 |
static_assert(baz<int>() == sizeof(int)); |
| 663 |
} // namespace value_dependent |
| 664 |
|
| 665 |
// https://github.com/llvm/llvm-project/issues/55601 |
| 666 |
namespace issue_55601 { |
| 667 |
template<typename T> |
| 668 |
class Bar { |
| 669 |
consteval static T x() { return 5; } // expected-note {{non-constexpr constructor 'derp' cannot be used in a constant expression}} |
| 670 |
public: |
| 671 |
Bar() : a(x()) {} // expected-error {{call to consteval function 'issue_55601::Bar<issue_55601::derp>::x' is not a constant expression}} |
| 672 |
// expected-error@-1 {{call to consteval function 'issue_55601::derp::operator int' is not a constant expression}} |
| 673 |
// expected-note@-2 {{in call to 'x()'}} |
| 674 |
// expected-note@-3 {{non-literal type 'issue_55601::derp' cannot be used in a constant expression}} |
| 675 |
private: |
| 676 |
int a; |
| 677 |
}; |
| 678 |
Bar<int> f; |
| 679 |
Bar<float> g; |
| 680 |
|
| 681 |
struct derp { |
| 682 |
// Can't be used in a constant expression |
| 683 |
derp(int); // expected-note {{declared here}} |
| 684 |
consteval operator int() const { return 5; } |
| 685 |
}; |
| 686 |
Bar<derp> a; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}} |
| 687 |
|
| 688 |
struct constantDerp { |
| 689 |
// Can be used in a constant expression. |
| 690 |
consteval constantDerp(int) {} |
| 691 |
consteval operator int() const { return 5; } |
| 692 |
}; |
| 693 |
Bar<constantDerp> b; |
| 694 |
|
| 695 |
} // namespace issue_55601 |
| 696 |
|
| 697 |
namespace default_argument { |
| 698 |
|
| 699 |
// Previously calls of consteval functions in default arguments were rejected. |
| 700 |
// Now we show that we don't reject such calls. |
| 701 |
consteval int foo() { return 1; } |
| 702 |
consteval int bar(int i = foo()) { return i * i; } |
| 703 |
|
| 704 |
struct Test1 { |
| 705 |
Test1(int i = bar(13)) {} |
| 706 |
void v(int i = bar(13) * 2 + bar(15)) {} |
| 707 |
}; |
| 708 |
Test1 t1; |
| 709 |
|
| 710 |
struct Test2 { |
| 711 |
constexpr Test2(int i = bar()) {} |
| 712 |
constexpr void v(int i = bar(bar(bar(foo())))) {} |
| 713 |
}; |
| 714 |
Test2 t2; |
| 715 |
|
| 716 |
} // namespace default_argument |
| 717 |
|
| 718 |
namespace PR50779 { |
| 719 |
struct derp { |
| 720 |
int b = 0; |
| 721 |
}; |
| 722 |
|
| 723 |
constexpr derp d; |
| 724 |
|
| 725 |
struct test { |
| 726 |
consteval int operator[](int i) const { return {}; } |
| 727 |
consteval const derp * operator->() const { return &d; } |
| 728 |
consteval int f() const { return 12; } // expected-note 2{{declared here}} |
| 729 |
}; |
| 730 |
|
| 731 |
constexpr test a; |
| 732 |
|
| 733 |
// We previously rejected both of these overloaded operators as taking the |
| 734 |
// address of a consteval function outside of an immediate context, but we |
| 735 |
// accepted direct calls to the overloaded operator. Now we show that we accept |
| 736 |
// both forms. |
| 737 |
constexpr int s = a.operator[](1); |
| 738 |
constexpr int t = a[1]; |
| 739 |
constexpr int u = a.operator->()->b; |
| 740 |
constexpr int v = a->b; |
| 741 |
// FIXME: I believe this case should work, but we currently reject. |
| 742 |
constexpr int w = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}} |
| 743 |
constexpr int x = a.f(); |
| 744 |
|
| 745 |
// Show that we reject when not in an immediate context. |
| 746 |
int w2 = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}} |
| 747 |
} |
| 748 |
|
| 749 |
namespace PR48235 { |
| 750 |
consteval int d() { |
| 751 |
return 1; |
| 752 |
} |
| 753 |
|
| 754 |
struct A { |
| 755 |
consteval int a() const { return 1; } |
| 756 |
|
| 757 |
void b() { |
| 758 |
this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \ |
| 759 |
// expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} |
| 760 |
} |
| 761 |
|
| 762 |
void c() { |
| 763 |
a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \ |
| 764 |
// expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} |
| 765 |
} |
| 766 |
}; |
| 767 |
} // PR48235 |
| 768 |
|
| 769 |
namespace NamespaceScopeConsteval { |
| 770 |
struct S { |
| 771 |
int Val; // expected-note {{subobject declared here}} |
| 772 |
consteval S() {} |
| 773 |
}; |
| 774 |
|
| 775 |
S s1; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \ |
| 776 |
expected-note {{subobject 'Val' is not initialized}} |
| 777 |
|
| 778 |
template <typename Ty> |
| 779 |
struct T { |
| 780 |
Ty Val; // expected-note {{subobject declared here}} |
| 781 |
consteval T() {} |
| 782 |
}; |
| 783 |
|
| 784 |
T<int> t; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \ |
| 785 |
expected-note {{subobject 'Val' is not initialized}} |
| 786 |
|
| 787 |
} // namespace NamespaceScopeConsteval |
| 788 |
|
| 789 |
namespace Issue54578 { |
| 790 |
// We expect the user-defined literal to be resovled entirely at compile time |
| 791 |
// despite being instantiated through a template. |
| 792 |
inline consteval unsigned char operator""_UC(const unsigned long long n) { |
| 793 |
return static_cast<unsigned char>(n); |
| 794 |
} |
| 795 |
|
| 796 |
inline constexpr char f1(const auto octet) { |
| 797 |
return 4_UC; |
| 798 |
} |
| 799 |
|
| 800 |
template <typename Ty> |
| 801 |
inline constexpr char f2(const Ty octet) { |
| 802 |
return 4_UC; |
| 803 |
} |
| 804 |
|
| 805 |
void test() { |
| 806 |
static_assert(f1('a') == 4); |
| 807 |
static_assert(f2('a') == 4); |
| 808 |
constexpr int c = f1('a') + f2('a'); |
| 809 |
static_assert(c == 8); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
namespace defaulted_special_member_template { |
| 814 |
template <typename T> |
| 815 |
struct default_ctor { |
| 816 |
T data; |
| 817 |
consteval default_ctor() = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}} |
| 818 |
}; |
| 819 |
|
| 820 |
template <typename T> |
| 821 |
struct copy { |
| 822 |
T data; |
| 823 |
|
| 824 |
consteval copy(const copy &) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}} |
| 825 |
consteval copy &operator=(const copy &) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}} |
| 826 |
copy() = default; |
| 827 |
}; |
| 828 |
|
| 829 |
template <typename T> |
| 830 |
struct move { |
| 831 |
T data; |
| 832 |
|
| 833 |
consteval move(move &&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}} |
| 834 |
consteval move &operator=(move &&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}} |
| 835 |
move() = default; |
| 836 |
}; |
| 837 |
|
| 838 |
struct foo { |
| 839 |
foo() {} // expected-note {{declared here}} |
| 840 |
foo(const foo &) {} // expected-note {{declared here}} |
| 841 |
foo(foo &&) {} // expected-note {{declared here}} |
| 842 |
|
| 843 |
foo& operator=(const foo &) { return *this; } // expected-note {{declared here}} |
| 844 |
foo& operator=(foo &&) { return *this; } // expected-note {{declared here}} |
| 845 |
}; |
| 846 |
|
| 847 |
void func() { |
| 848 |
default_ctor<foo> fail0; // expected-error {{call to consteval function 'defaulted_special_member_template::default_ctor<defaulted_special_member_template::foo>::default_ctor' is not a constant expression}} \ |
| 849 |
expected-note {{in call to 'default_ctor()'}} |
| 850 |
|
| 851 |
copy<foo> good0; |
| 852 |
copy<foo> fail1{good0}; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::copy' is not a constant expression}} \ |
| 853 |
expected-note {{in call to 'copy(good0)'}} |
| 854 |
fail1 = good0; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \ |
| 855 |
expected-note {{in call to 'fail1.operator=(good0)'}} |
| 856 |
|
| 857 |
move<foo> good1; |
| 858 |
move<foo> fail2{static_cast<move<foo>&&>(good1)}; // expected-error {{call to consteval function 'defaulted_special_member_template::move<defaulted_special_member_template::foo>::move' is not a constant expression}} \ |
| 859 |
expected-note {{in call to 'move(good1)'}} |
| 860 |
fail2 = static_cast<move<foo>&&>(good1); // expected-error {{call to consteval function 'defaulted_special_member_template::move<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \ |
| 861 |
expected-note {{in call to 'fail2.operator=(good1)'}} |
| 862 |
} |
| 863 |
} // namespace defaulted_special_member_template |
| 864 |
|
| 865 |
namespace multiple_default_constructors { |
| 866 |
struct Foo { |
| 867 |
Foo() {} // expected-note {{declared here}} |
| 868 |
}; |
| 869 |
struct Bar { |
| 870 |
Bar() = default; |
| 871 |
}; |
| 872 |
struct Baz { |
| 873 |
consteval Baz() {} |
| 874 |
}; |
| 875 |
|
| 876 |
template <typename T, unsigned N> |
| 877 |
struct S { |
| 878 |
T data; |
| 879 |
S() requires (N==1) = default; |
| 880 |
// This cannot be used in constexpr context. |
| 881 |
S() requires (N==2) {} // expected-note {{declared here}} |
| 882 |
consteval S() requires (N==3) = default; // expected-note {{non-constexpr constructor 'Foo' cannot be used in a constant expression}} |
| 883 |
}; |
| 884 |
|
| 885 |
void func() { |
| 886 |
// Explicitly defaulted constructor. |
| 887 |
S<Foo, 1> s1; |
| 888 |
S<Bar, 1> s2; |
| 889 |
// User provided constructor. |
| 890 |
S<Foo, 2> s3; |
| 891 |
S<Bar, 2> s4; |
| 892 |
// Consteval explicitly defaulted constructor. |
| 893 |
S<Foo, 3> s5; // expected-error {{call to consteval function 'multiple_default_constructors::S<multiple_default_constructors::Foo, 3>::S' is not a constant expression}} \ |
| 894 |
expected-note {{in call to 'S()'}} |
| 895 |
S<Bar, 3> s6; |
| 896 |
S<Baz, 3> s7; |
| 897 |
} |
| 898 |
|
| 899 |
consteval int aConstevalFunction() { // expected-error {{consteval function never produces a constant expression}} |
| 900 |
// Defaulted default constructors are implicitly consteval. |
| 901 |
S<Bar, 1> s1; |
| 902 |
|
| 903 |
S<Baz, 2> s4; // expected-note {{non-constexpr constructor 'S' cannot be used in a constant expression}} |
| 904 |
|
| 905 |
S<Bar, 3> s2; |
| 906 |
S<Baz, 3> s3; |
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
} // namespace multiple_default_constructors |
| 911 |
|
| 912 |
namespace GH50055 { |
| 913 |
enum E {e1=0, e2=1}; |
| 914 |
consteval int testDefaultArgForParam(E eParam = (E)-1) { |
| 915 |
// expected-note@-1 {{integer value -1 is outside the valid range of values [0, 1] for the enumeration type 'E'}} |
| 916 |
return (int)eParam; |
| 917 |
} |
| 918 |
|
| 919 |
int test() { |
| 920 |
return testDefaultArgForParam() + testDefaultArgForParam((E)1); |
| 921 |
// expected-error@-1 {{call to consteval function 'GH50055::testDefaultArgForParam' is not a constant expression}} |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
namespace GH51182 { |
| 926 |
// Nested consteval function. |
| 927 |
consteval int f(int v) { |
| 928 |
return v; |
| 929 |
} |
| 930 |
|
| 931 |
template <typename T> |
| 932 |
consteval int g(T a) { |
| 933 |
// An immediate function context. |
| 934 |
int n = f(a); |
| 935 |
return n; |
| 936 |
} |
| 937 |
static_assert(g(100) == 100); |
| 938 |
// -------------------------------------- |
| 939 |
template <typename T> |
| 940 |
consteval T max(const T& a, const T& b) { |
| 941 |
return (a > b) ? a : b; |
| 942 |
} |
| 943 |
template <typename T> |
| 944 |
consteval T mid(const T& a, const T& b, const T& c) { |
| 945 |
T m = max(max(a, b), c); |
| 946 |
if (m == a) |
| 947 |
return max(b, c); |
| 948 |
if (m == b) |
| 949 |
return max(a, c); |
| 950 |
return max(a, b); |
| 951 |
} |
| 952 |
static_assert(max(1,2)==2); |
| 953 |
static_assert(mid(1,2,3)==2); |
| 954 |
} // namespace GH51182 |
| 955 |
|
| 956 |
// https://github.com/llvm/llvm-project/issues/56183 |
| 957 |
namespace GH56183 { |
| 958 |
consteval auto Foo(auto c) { return c; } |
| 959 |
consteval auto Bar(auto f) { return f(); } |
| 960 |
void test() { |
| 961 |
constexpr auto x = Foo(Bar([] { return 'a'; })); |
| 962 |
static_assert(x == 'a'); |
| 963 |
} |
| 964 |
} // namespace GH56183 |
| 965 |
|
| 966 |
// https://github.com/llvm/llvm-project/issues/51695 |
| 967 |
namespace GH51695 { |
| 968 |
// Original ======================================== |
| 969 |
template <typename T> |
| 970 |
struct type_t {}; |
| 971 |
|
| 972 |
template <typename...> |
| 973 |
struct list_t {}; |
| 974 |
|
| 975 |
template <typename T, typename... Ts> |
| 976 |
consteval auto pop_front(list_t<T, Ts...>) -> auto { |
| 977 |
return list_t<Ts...>{}; |
| 978 |
} |
| 979 |
|
| 980 |
template <typename... Ts, typename F> |
| 981 |
consteval auto apply(list_t<Ts...>, F fn) -> auto { |
| 982 |
return fn(type_t<Ts>{}...); |
| 983 |
} |
| 984 |
|
| 985 |
void test1() { |
| 986 |
constexpr auto x = apply(pop_front(list_t<char, char>{}), |
| 987 |
[]<typename... Us>(type_t<Us>...) { return 42; }); |
| 988 |
static_assert(x == 42); |
| 989 |
} |
| 990 |
// Reduced 1 ======================================== |
| 991 |
consteval bool zero() { return false; } |
| 992 |
|
| 993 |
template <typename F> |
| 994 |
consteval bool foo(bool, F f) { |
| 995 |
return f(); |
| 996 |
} |
| 997 |
|
| 998 |
void test2() { |
| 999 |
constexpr auto x = foo(zero(), []() { return true; }); |
| 1000 |
static_assert(x); |
| 1001 |
} |
| 1002 |
|
| 1003 |
// Reduced 2 ======================================== |
| 1004 |
template <typename F> |
| 1005 |
consteval auto bar(F f) { return f;} |
| 1006 |
|
| 1007 |
void test3() { |
| 1008 |
constexpr auto t1 = bar(bar(bar(bar([]() { return true; }))))(); |
| 1009 |
static_assert(t1); |
| 1010 |
|
| 1011 |
int a = 1; // expected-note {{declared here}} |
| 1012 |
auto t2 = bar(bar(bar(bar([=]() { return a; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}} |
| 1013 |
// expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}} |
| 1014 |
|
| 1015 |
constexpr auto t3 = bar(bar([x=bar(42)]() { return x; }))(); |
| 1016 |
static_assert(t3==42); |
| 1017 |
constexpr auto t4 = bar(bar([x=bar(42)]() consteval { return x; }))(); |
| 1018 |
static_assert(t4==42); |
| 1019 |
} |
| 1020 |
|
| 1021 |
} // namespace GH51695 |
| 1022 |
|
| 1023 |
// https://github.com/llvm/llvm-project/issues/50455 |
| 1024 |
namespace GH50455 { |
| 1025 |
void f() { |
| 1026 |
[]() consteval { int i{}; }(); |
| 1027 |
[]() consteval { int i{}; ++i; }(); |
| 1028 |
} |
| 1029 |
void g() { |
| 1030 |
(void)[](int i) consteval { return i; }(0); |
| 1031 |
(void)[](int i) consteval { return i; }(0); |
| 1032 |
} |
| 1033 |
} // namespace GH50455 |
| 1034 |
|
| 1035 |
namespace GH58302 { |
| 1036 |
struct A { |
| 1037 |
consteval A(){} |
| 1038 |
consteval operator int() { return 1;} |
| 1039 |
}; |
| 1040 |
|
| 1041 |
int f() { |
| 1042 |
int x = A{}; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
namespace GH57682 { |
| 1047 |
void test() { |
| 1048 |
constexpr auto l1 = []() consteval { // expected-error {{cannot take address of consteval call operator of '(lambda at}} \ |
| 1049 |
// expected-note 2{{declared here}} |
| 1050 |
return 3; |
| 1051 |
}; |
| 1052 |
constexpr int (*f1)(void) = l1; // expected-error {{constexpr variable 'f1' must be initialized by a constant expression}} \ |
| 1053 |
// expected-note {{pointer to a consteval declaration is not a constant expression}} |
| 1054 |
|
| 1055 |
|
| 1056 |
constexpr auto lstatic = []() static consteval { // expected-error {{cannot take address of consteval call operator of '(lambda at}} \ |
| 1057 |
// expected-note 2{{declared here}} \ |
| 1058 |
// expected-warning {{extension}} |
| 1059 |
return 3; |
| 1060 |
}; |
| 1061 |
constexpr int (*f2)(void) = lstatic; // expected-error {{constexpr variable 'f2' must be initialized by a constant expression}} \ |
| 1062 |
// expected-note {{pointer to a consteval declaration is not a constant expression}} |
| 1063 |
|
| 1064 |
int (*f3)(void) = []() consteval { return 3; }; // expected-error {{cannot take address of consteval call operator of '(lambda at}} \ |
| 1065 |
// expected-note {{declared here}} |
| 1066 |
} |
| 1067 |
|
| 1068 |
consteval void consteval_test() { |
| 1069 |
constexpr auto l1 = []() consteval { return 3; }; |
| 1070 |
|
| 1071 |
int (*f1)(void) = l1; // ok |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
namespace GH60286 { |
| 1076 |
|
| 1077 |
struct A { |
| 1078 |
int i = 0; |
| 1079 |
|
| 1080 |
consteval A() {} |
| 1081 |
A(const A&) { i = 1; } |
| 1082 |
consteval int f() { return i; } |
| 1083 |
}; |
| 1084 |
|
| 1085 |
constexpr auto B = A{A{}}.f(); |
| 1086 |
static_assert(B == 0); |
| 1087 |
|
| 1088 |
} |
| 1089 |
|
| 1090 |
namespace GH58207 { |
| 1091 |
struct tester { |
| 1092 |
consteval tester(const char* name) noexcept { } |
| 1093 |
}; |
| 1094 |
consteval const char* make_name(const char* name) { return name;} |
| 1095 |
consteval const char* pad(int P) { return "thestring"; } |
| 1096 |
|
| 1097 |
int bad = 10; // expected-note 6{{declared here}} |
| 1098 |
|
| 1099 |
tester glob1(make_name("glob1")); |
| 1100 |
tester glob2(make_name("glob2")); |
| 1101 |
constexpr tester cglob(make_name("cglob")); |
| 1102 |
tester paddedglob(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \ |
| 1103 |
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} |
| 1104 |
|
| 1105 |
constexpr tester glob3 = { make_name("glob3") }; |
| 1106 |
constexpr tester glob4 = { make_name(pad(bad)) }; // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \ |
| 1107 |
// expected-error {{constexpr variable 'glob4' must be initialized by a constant expression}} \ |
| 1108 |
// expected-note 2{{read of non-const variable 'bad' is not allowed in a constant expression}} |
| 1109 |
|
| 1110 |
auto V = make_name(pad(3)); |
| 1111 |
auto V1 = make_name(pad(bad)); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ |
| 1112 |
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} |
| 1113 |
|
| 1114 |
|
| 1115 |
void foo() { |
| 1116 |
static tester loc1(make_name("loc1")); |
| 1117 |
static constexpr tester loc2(make_name("loc2")); |
| 1118 |
static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \ |
| 1119 |
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} |
| 1120 |
} |
| 1121 |
|
| 1122 |
void bar() { |
| 1123 |
static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \ |
| 1124 |
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
namespace GH64949 { |
| 1129 |
struct f { |
| 1130 |
int g; // expected-note 2{{subobject declared here}} |
| 1131 |
constexpr ~f() {} |
| 1132 |
}; |
| 1133 |
class h { |
| 1134 |
|
| 1135 |
public: |
| 1136 |
consteval h(char *) {} |
| 1137 |
consteval operator int() const { return 1; } |
| 1138 |
f i; |
| 1139 |
}; |
| 1140 |
|
| 1141 |
void test() { (int)h{nullptr}; } |
| 1142 |
// expected-error@-1 {{call to consteval function 'GH64949::h::h' is not a constant expression}} |
| 1143 |
// expected-note@-2 {{subobject 'g' is not initialized}} |
| 1144 |
|
| 1145 |
int test2() { return h{nullptr}; } |
| 1146 |
// expected-error@-1 {{call to consteval function 'GH64949::h::h' is not a constant expression}} |
| 1147 |
// expected-note@-2 {{subobject 'g' is not initialized}} |
| 1148 |
|
| 1149 |
|
| 1150 |
} |
| 1151 |
|
| 1152 |
namespace GH65985 { |
| 1153 |
|
| 1154 |
int consteval operator""_foo(unsigned long long V) { |
| 1155 |
return 0; |
| 1156 |
} |
| 1157 |
int consteval operator""_bar(unsigned long long V); // expected-note 4 {{here}} |
| 1158 |
|
| 1159 |
int consteval f() { |
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
int consteval g(); // expected-note 2 {{here}} |
| 1164 |
|
| 1165 |
|
| 1166 |
struct C { |
| 1167 |
static const int a = 1_foo; |
| 1168 |
static constexpr int b = 1_foo; |
| 1169 |
static const int c = 1_bar; // expected-error {{call to consteval function 'GH65985::operator""_bar' is not a constant expression}} \ |
| 1170 |
// expected-note 2 {{undefined function 'operator""_bar' cannot be used in a constant expression}} \ |
| 1171 |
// expected-error {{in-class initializer for static data member is not a constant expression}} |
| 1172 |
|
| 1173 |
// FIXME: remove duplicate diagnostics |
| 1174 |
static constexpr int d = 1_bar; // expected-error {{call to consteval function 'GH65985::operator""_bar' is not a constant expression}} \ |
| 1175 |
// expected-note {{undefined function 'operator""_bar' cannot be used in a constant expression}} \ |
| 1176 |
// expected-error {{constexpr variable 'd' must be initialized by a constant expression}} \ |
| 1177 |
// expected-note {{undefined function 'operator""_bar' cannot be used in a constant expression}} |
| 1178 |
|
| 1179 |
static const int e = f(); |
| 1180 |
static const int f = g(); // expected-error {{call to consteval function 'GH65985::g' is not a constant expression}} \ |
| 1181 |
// expected-error {{in-class initializer for static data member is not a constant expression}} \ |
| 1182 |
// expected-note 2 {{undefined function 'g' cannot be used in a constant expression}} |
| 1183 |
}; |
| 1184 |
|
| 1185 |
} |
| 1186 |
|
| 1187 |
namespace GH66562 { |
| 1188 |
|
| 1189 |
namespace ns |
| 1190 |
{ |
| 1191 |
consteval int foo(int x) { return 1; } // expected-note {{declared here}} \ |
| 1192 |
// expected-note {{passing argument to parameter 'x' here}} |
| 1193 |
} |
| 1194 |
|
| 1195 |
template <class A> |
| 1196 |
struct T { |
| 1197 |
static constexpr auto xx = ns::foo(A{}); // expected-error {{cannot take address of consteval function 'foo' outside of an immediate invocation}} \ |
| 1198 |
// expected-error {{cannot initialize a parameter of type 'int' with an rvalue of type 'char *'}} |
| 1199 |
}; |
| 1200 |
|
| 1201 |
template class T<char*>; // expected-note {{in instantiation}} |
| 1202 |
|
| 1203 |
} |
| 1204 |
|
| 1205 |
namespace GH65520 { |
| 1206 |
|
| 1207 |
consteval int bar (int i) { if (i != 1) return 1/0; return 0; } |
| 1208 |
// expected-note@-1{{division by zero}} |
| 1209 |
|
| 1210 |
void |
| 1211 |
g () |
| 1212 |
{ |
| 1213 |
int a_ok[bar(1)]; |
| 1214 |
int a_err[bar(3)]; // expected-error {{call to consteval function 'GH65520::bar' is not a constant expression}} \ |
| 1215 |
// expected-note {{in call to 'bar(3)'}} |
| 1216 |
} |
| 1217 |
|
| 1218 |
consteval int undefined(); // expected-note {{declared here}} |
| 1219 |
|
| 1220 |
consteval void immediate() { |
| 1221 |
int a [undefined()]; // expected-note {{undefined function 'undefined' cannot be used in a constant expression}} \ |
| 1222 |
// expected-error {{call to consteval function 'GH65520::undefined' is not a constant expression}} \ |
| 1223 |
// expected-error {{variable of non-literal type 'int[undefined()]' cannot be defined in a constexpr function before C++23}} |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
} |
| 1228 |
|
| 1229 |
namespace GH105558 { |
| 1230 |
|
| 1231 |
consteval int* alloc() { return new int(0); } |
| 1232 |
consteval void f(int* p) { delete p; } |
| 1233 |
consteval void g1(int*&& p) { delete p; } |
| 1234 |
consteval void g2(const int* p) { delete p; } |
| 1235 |
consteval void g3(int*const& p) { delete p; } |
| 1236 |
struct X { |
| 1237 |
int* p; |
| 1238 |
explicit(false) constexpr X(int* p) : p(p) {} |
| 1239 |
}; |
| 1240 |
consteval void g4(X x) { delete x.p; } |
| 1241 |
|
| 1242 |
void test() { |
| 1243 |
f(alloc()); |
| 1244 |
g1(alloc()); |
| 1245 |
g2(alloc()); |
| 1246 |
g3(alloc()); |
| 1247 |
g4(alloc()); |
| 1248 |
} |
| 1249 |
|
| 1250 |
} |
| 1251 |
|
| 1252 |
// Test that we don't redundantly instantiate the friend declaration in |
| 1253 |
// RemoveNestedImmediateInvocation(). Otherwise, we would end up with spurious |
| 1254 |
// redefinition errors. |
| 1255 |
namespace GH107175 { |
| 1256 |
|
| 1257 |
consteval void consteval_func() {} |
| 1258 |
|
| 1259 |
template <auto> struct define_f { |
| 1260 |
friend void foo() {} |
| 1261 |
}; |
| 1262 |
|
| 1263 |
template <auto = [] {}> struct A {}; |
| 1264 |
|
| 1265 |
struct B { |
| 1266 |
template <auto T> consteval void func() { (void)define_f<T>{}; } |
| 1267 |
}; |
| 1268 |
|
| 1269 |
int main() { |
| 1270 |
B{}.func<A{}>(); |
| 1271 |
consteval_func(); |
| 1272 |
} |
| 1273 |
|
| 1274 |
} // namespace GH107175 |
| 1275 |
|
| 1276 |
namespace GH137885 { |
| 1277 |
|
| 1278 |
template <typename... P> struct A {}; |
| 1279 |
|
| 1280 |
template <int N> |
| 1281 |
struct B { |
| 1282 |
consteval B() {} |
| 1283 |
|
| 1284 |
template <typename... P> consteval operator A<P...>() const { |
| 1285 |
static_assert(sizeof...(P) == N); |
| 1286 |
return {}; |
| 1287 |
} |
| 1288 |
}; |
| 1289 |
|
| 1290 |
template <typename T> struct type_identity { |
| 1291 |
using type = T; |
| 1292 |
}; |
| 1293 |
|
| 1294 |
template <typename... P> |
| 1295 |
void foo(typename type_identity<A<P...>>::type a, P...) {} |
| 1296 |
|
| 1297 |
void foo() { |
| 1298 |
foo(B<0>()); |
| 1299 |
foo(B<5>(), 1, 2, 3, 4, 5); |
| 1300 |
} |
| 1301 |
|
| 1302 |
} |
| 1303 |
|
| 1304 |
// https://github.com/llvm/llvm-project/issues/139160 |
| 1305 |
namespace GH139160{ |
| 1306 |
// original test case taken from Github |
| 1307 |
struct A {int x[1]; }; |
| 1308 |
A f(); // expected-note {{declared here}} |
| 1309 |
typedef int *t[]; |
| 1310 |
consteval int* f(int* x) { return x; } |
| 1311 |
|
| 1312 |
int ** x = (t){f(f().x)}; // expected-error {{call to consteval function 'GH139160::f' is not a constant expression}} |
| 1313 |
// expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}} |
| 1314 |
// expected-error@-2 {{initializer element is not a compile-time constant}} |
| 1315 |
|
| 1316 |
struct B {int value, value_two;}; |
| 1317 |
B make_struct() {return {10, 20};} // expected-note {{declared here}} |
| 1318 |
consteval int get_value(B container) {return container.value;} |
| 1319 |
B result = (B){10, get_value(make_struct())}; // expected-error {{initializer element is not a compile-time constant}} |
| 1320 |
// expected-error@-1 {{call to consteval function 'GH139160::get_value' is not a constant expression}} |
| 1321 |
// expected-note@-2 {{non-constexpr function 'make_struct' cannot be used in a constant expression}} |
| 1322 |
} // namespace GH139160 |
| 1323 |
|
| 1324 |
namespace GH118187 { |
| 1325 |
|
| 1326 |
template <typename T> int t() { |
| 1327 |
return []<typename U>() consteval { |
| 1328 |
return [](U v) { return v; }(123); |
| 1329 |
}.template operator()<int>(); |
| 1330 |
} |
| 1331 |
|
| 1332 |
int v = t<int>(); |
| 1333 |
} // namespace GH118187 |
| 1334 |
|
| 1335 |
namespace GH156579 { |
| 1336 |
template <class> |
| 1337 |
auto f{[] (auto...) { |
| 1338 |
if constexpr ([] (auto) { return true; }(0)) |
| 1339 |
return 0; |
| 1340 |
}}; |
| 1341 |
|
| 1342 |
void g() { |
| 1343 |
f<int>(); |
| 1344 |
} |
| 1345 |
} // namespace GH156579 |