1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value %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 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 { 254 // expected-note@-1 {{declared here}} 255 int t = f(i); 256 // expected-error@-1 {{is not a constant expression}} 257 // expected-note@-2 {{function parameter}} 258 return f(0); 259 }; 260 261 } 262 263 namespace std { 264 265 template <typename T> struct remove_reference { using type = T; }; 266 template <typename T> struct remove_reference<T &> { using type = T; }; 267 template <typename T> struct remove_reference<T &&> { using type = T; }; 268 269 template <typename T> 270 constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept { 271 return static_cast<typename std::remove_reference<T>::type &&>(t); 272 } 273 274 } 275 276 namespace temporaries { 277 278 struct A { 279 consteval int ret_i() const { return 0; } 280 consteval A ret_a() const { return A{}; } 281 constexpr ~A() { } 282 }; 283 284 consteval int by_value_a(A a) { return a.ret_i(); } 285 286 consteval int const_a_ref(const A &a) { 287 return a.ret_i(); 288 } 289 290 consteval int rvalue_ref(const A &&a) { 291 return a.ret_i(); 292 } 293 294 consteval const A &to_lvalue_ref(const A &&a) { 295 return a; 296 } 297 298 void test() { 299 constexpr A a {}; 300 { int k = A().ret_i(); } 301 { A k = A().ret_a(); } 302 { A k = to_lvalue_ref(A()); }// expected-error {{is not a constant expression}} 303 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} 304 { A k = to_lvalue_ref(A().ret_a()); } // expected-error {{is not a constant expression}} 305 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} 306 { int k = A().ret_a().ret_i(); } 307 { int k = by_value_a(A()); } 308 { int k = const_a_ref(A()); } 309 { int k = const_a_ref(a); } 310 { int k = rvalue_ref(A()); } 311 { int k = rvalue_ref(std::move(a)); } 312 { int k = const_a_ref(A().ret_a()); } 313 { int k = const_a_ref(to_lvalue_ref(A().ret_a())); } 314 { int k = const_a_ref(to_lvalue_ref(std::move(a))); } 315 { int k = by_value_a(A().ret_a()); } 316 { int k = by_value_a(to_lvalue_ref(std::move(a))); } 317 { int k = (A().ret_a(), A().ret_i()); } 318 { int k = (const_a_ref(A().ret_a()), A().ret_i()); }// 319 } 320 321 } 322 323 namespace alloc { 324 325 consteval int f() { 326 int *A = new int(0); 327 // expected-note@-1+ {{allocation performed here was not deallocated}} 328 return *A; 329 } 330 331 int i1 = f(); // expected-error {{is not a constant expression}} 332 333 struct A { 334 int* p = new int(42); 335 // expected-note@-1+ {{heap allocation performed here}} 336 consteval int ret_i() const { return p ? *p : 0; } 337 consteval A ret_a() const { return A{}; } 338 constexpr ~A() { delete p; } 339 }; 340 341 consteval int by_value_a(A a) { return a.ret_i(); } 342 343 consteval int const_a_ref(const A &a) { 344 return a.ret_i(); 345 } 346 347 consteval int rvalue_ref(const A &&a) { 348 return a.ret_i(); 349 } 350 351 consteval const A &to_lvalue_ref(const A &&a) { 352 return a; 353 } 354 355 void test() { 356 constexpr A a{ nullptr }; 357 { int k = A().ret_i(); } 358 { A k = A().ret_a(); } // expected-error {{is not a constant expression}} 359 // expected-note@-1 {{is not a constant expression}} 360 { A k = to_lvalue_ref(A()); } // expected-error {{is not a constant expression}} 361 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} 362 { A k = to_lvalue_ref(A().ret_a()); } 363 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} 364 // expected-note@-2 {{heap-allocated object is not a constant expression}} 365 // expected-error@-3 {{'alloc::to_lvalue_ref' is not a constant expression}} 366 // expected-note@-4 {{reference to temporary is not a constant expression}} 367 // expected-note@-5 {{temporary created here}} 368 { int k = A().ret_a().ret_i(); } 369 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} 370 // expected-note@-2 {{heap-allocated object is not a constant expression}} 371 { int k = by_value_a(A()); } 372 { int k = const_a_ref(A()); } 373 { int k = const_a_ref(a); } 374 { int k = rvalue_ref(A()); } 375 { int k = rvalue_ref(std::move(a)); } 376 { int k = const_a_ref(A().ret_a()); } 377 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} 378 // expected-note@-2 {{is not a constant expression}} 379 { int k = const_a_ref(to_lvalue_ref(A().ret_a())); } 380 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} 381 // expected-note@-2 {{is not a constant expression}} 382 { int k = const_a_ref(to_lvalue_ref(std::move(a))); } 383 { int k = by_value_a(A().ret_a()); } 384 { int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); } 385 { int k = (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}} 386 // expected-note@-1 {{is not a constant expression}} 387 { int k = (const_a_ref(A().ret_a()), A().ret_i()); } 388 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}} 389 // expected-note@-2 {{is not a constant expression}} 390 } 391 392 } 393 394 namespace self_referencing { 395 396 struct S { 397 S* ptr = nullptr; 398 constexpr S(int i) : ptr(this) { 399 if (this == ptr && i) 400 ptr = nullptr; 401 } 402 constexpr ~S() {} 403 }; 404 405 consteval S f(int i) { 406 return S(i); 407 } 408 409 void test() { 410 S s(1); 411 s = f(1); 412 s = f(0); // expected-error {{is not a constant expression}} 413 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} 414 } 415 416 struct S1 { 417 S1* ptr = nullptr; 418 consteval S1(int i) : ptr(this) { 419 if (this == ptr && i) 420 ptr = nullptr; 421 } 422 constexpr ~S1() {} 423 }; 424 425 void test1() { 426 S1 s(1); 427 s = S1(1); 428 s = S1(0); // expected-error {{is not a constant expression}} 429 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}} 430 } 431 432 } 433 namespace ctor { 434 435 consteval int f_eval() { // expected-note+ {{declared here}} 436 return 0; 437 } 438 439 namespace std { 440 struct strong_ordering { 441 int n; 442 static const strong_ordering less, equal, greater; 443 }; 444 constexpr strong_ordering strong_ordering::less = {-1}; 445 constexpr strong_ordering strong_ordering::equal = {0}; 446 constexpr strong_ordering strong_ordering::greater = {1}; 447 constexpr bool operator!=(strong_ordering, int); 448 } 449 450 namespace override { 451 struct A { 452 virtual consteval void f(); // expected-note {{overridden}} 453 virtual void g(); // expected-note {{overridden}} 454 }; 455 struct B : A { 456 consteval void f(); 457 void g(); 458 }; 459 struct C : A { 460 void f(); // expected-error {{non-consteval function 'f' cannot override a consteval function}} 461 consteval void g(); // expected-error {{consteval function 'g' cannot override a non-consteval function}} 462 }; 463 464 namespace implicit_equals_1 { 465 struct Y; 466 struct X { 467 std::strong_ordering operator<=>(const X&) const; 468 constexpr bool operator==(const X&) const; 469 virtual consteval bool operator==(const Y&) const; // expected-note {{here}} 470 }; 471 struct Y : X { 472 std::strong_ordering operator<=>(const Y&) const = default; 473 // expected-error@-1 {{non-consteval function 'operator==' cannot override a consteval function}} 474 }; 475 } 476 477 namespace implicit_equals_2 { 478 struct Y; 479 struct X { 480 constexpr std::strong_ordering operator<=>(const X&) const; 481 constexpr bool operator==(const X&) const; 482 virtual bool operator==(const Y&) const; // expected-note {{here}} 483 }; 484 struct Y : X { 485 consteval std::strong_ordering operator<=>(const Y&) const = default; 486 // expected-error@-1 {{consteval function 'operator==' cannot override a non-consteval function}} 487 }; 488 } 489 } 490 491 namespace operator_rewrite { 492 struct A { 493 friend consteval int operator<=>(const A&, const A&) { return 0; } 494 }; 495 const bool k = A() < A(); 496 static_assert(!k); 497 498 A a; 499 bool k2 = A() < a; // OK, does not access 'a'. 500 501 struct B { 502 friend consteval int operator<=>(const B &l, const B &r) { return r.n - l.n; } // expected-note {{read of }} 503 int n; 504 }; 505 static_assert(B() >= B()); 506 B b; // expected-note {{here}} 507 bool k3 = B() < b; // expected-error-re {{call to consteval function '{{.*}}::operator<=>' is not a constant expression}} expected-note {{in call}} 508 } 509 510 struct A { 511 int(*ptr)(); 512 consteval A(int(*p)() = nullptr) : ptr(p) {} 513 }; 514 515 struct B { 516 int(*ptr)(); 517 B() : ptr(nullptr) {} 518 consteval B(int(*p)(), int) : ptr(p) {} 519 }; 520 521 void test() { 522 { A a; } 523 { A a(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 524 { B b(nullptr, 0); } 525 { B b(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 526 { A a{}; } 527 { A a{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 528 { B b{nullptr, 0}; } 529 { B b{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 530 { A a = A(); } 531 { A a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 532 { B b = B(nullptr, 0); } 533 { B b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 534 { A a = A{}; } 535 { A a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 536 { B b = B{nullptr, 0}; } 537 { B b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 538 { A a; a = A(); } 539 { A a; a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 540 { B b; b = B(nullptr, 0); } 541 { B b; b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 542 { A a; a = A{}; } 543 { A a; a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 544 { B b; b = B{nullptr, 0}; } 545 { B b; b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 546 { A* a; a = new A(); } 547 { A* a; a = new A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 548 { B* b; b = new B(nullptr, 0); } 549 { B* b; b = new B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 550 { A* a; a = new A{}; } 551 { A* a; a = new A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 552 { B* b; b = new B{nullptr, 0}; } 553 { B* b; b = new B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}} 554 } 555 556 } 557 558 namespace copy_ctor { 559 560 consteval int f_eval() { // expected-note+ {{declared here}} 561 return 0; 562 } 563 564 struct Copy { 565 int(*ptr)(); 566 constexpr Copy(int(*p)() = nullptr) : ptr(p) {} 567 consteval Copy(const Copy&) = default; 568 }; 569 570 constexpr const Copy &to_lvalue_ref(const Copy &&a) { 571 return a; 572 } 573 574 void test() { 575 constexpr const Copy C; 576 // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision. 577 // so we need to test with both prvalue and xvalues. 578 { Copy c(C); } 579 { Copy c((Copy(&f_eval))); }// expected-error {{cannot take address of consteval}} 580 { Copy c(std::move(C)); } 581 { Copy c(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 582 { Copy c(to_lvalue_ref((Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 583 { Copy c(to_lvalue_ref(std::move(C))); } 584 { Copy c(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 585 { Copy c = Copy(C); } 586 { Copy c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} 587 { Copy c = Copy(std::move(C)); } 588 { Copy c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 589 { Copy c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 590 { Copy c = Copy(to_lvalue_ref(std::move(C))); } 591 { Copy c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 592 { Copy c; c = Copy(C); } 593 { Copy c; c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} 594 { Copy c; c = Copy(std::move(C)); } 595 { Copy c; c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 596 { Copy c; c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 597 { Copy c; c = Copy(to_lvalue_ref(std::move(C))); } 598 { Copy c; c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 599 { Copy* c; c = new Copy(C); } 600 { Copy* c; c = new Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}} 601 { Copy* c; c = new Copy(std::move(C)); } 602 { Copy* c; c = new Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 603 { Copy* c; c = new Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}} 604 { Copy* c; c = new Copy(to_lvalue_ref(std::move(C))); } 605 { 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}} 606 } 607 608 } // namespace special_ctor 609 610 namespace unevaluated { 611 612 template <typename T, typename U> struct is_same { static const bool value = false; }; 613 template <typename T> struct is_same<T, T> { static const bool value = true; }; 614 615 long f(); // expected-note {{declared here}} 616 auto consteval g(auto a) { 617 return a; 618 } 619 620 auto e = g(f()); // expected-error {{is not a constant expression}} 621 // expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}} 622 623 using T = decltype(g(f())); 624 static_assert(is_same<long, T>::value); 625 626 } // namespace unevaluated 627 628 namespace value_dependent { 629 630 consteval int foo(int x) { 631 return x; 632 } 633 634 template <int X> constexpr int bar() { 635 // Previously this call was rejected as value-dependent constant expressions 636 // can't be immediately evaluated. Now we show that we don't immediately 637 // evaluate them until they are instantiated. 638 return foo(X); 639 } 640 641 template <typename T> constexpr int baz() { 642 constexpr int t = sizeof(T); 643 // Previously this call was rejected as `t` is value-dependent and its value 644 // is unknown until the function is instantiated. Now we show that we don't 645 // reject such calls. 646 return foo(t); 647 } 648 649 static_assert(bar<15>() == 15); 650 static_assert(baz<int>() == sizeof(int)); 651 652 } // namespace value_dependent 653 654 namespace PR50779 { 655 struct derp { 656 int b = 0; 657 }; 658 659 constexpr derp d; 660 661 struct test { 662 consteval int operator[](int i) const { return {}; } 663 consteval const derp * operator->() const { return &d; } 664 consteval int f() const { return 12; } // expected-note 2{{declared here}} 665 }; 666 667 constexpr test a; 668 669 // We previously rejected both of these overloaded operators as taking the 670 // address of a consteval function outside of an immediate context, but we 671 // accepted direct calls to the overloaded operator. Now we show that we accept 672 // both forms. 673 constexpr int s = a.operator[](1); 674 constexpr int t = a[1]; 675 constexpr int u = a.operator->()->b; 676 constexpr int v = a->b; 677 // FIXME: I believe this case should work, but we currently reject. 678 constexpr int w = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}} 679 constexpr int x = a.f(); 680 681 // Show that we reject when not in an immediate context. 682 int w2 = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}} 683 } 684 685 namespace PR48235 { 686 consteval int d() { 687 return 1; 688 } 689 690 struct A { 691 consteval int a() const { return 1; } 692 693 void b() { 694 this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \ 695 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} 696 } 697 698 void c() { 699 a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \ 700 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} 701 } 702 }; 703 } // PR48235 704 705 namespace NamespaceScopeConsteval { 706 struct S { 707 int Val; // expected-note {{subobject declared here}} 708 consteval S() {} 709 }; 710 711 S s1; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \ 712 expected-note {{subobject of type 'int' is not initialized}} 713 714 template <typename Ty> 715 struct T { 716 Ty Val; // expected-note {{subobject declared here}} 717 consteval T() {} 718 }; 719 720 T<int> t; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \ 721 expected-note {{subobject of type 'int' is not initialized}} 722 723 } // namespace NamespaceScopeConsteval 724 725 namespace Issue54578 { 726 // We expect the user-defined literal to be resovled entirely at compile time 727 // despite being instantiated through a template. 728 inline consteval unsigned char operator""_UC(const unsigned long long n) { 729 return static_cast<unsigned char>(n); 730 } 731 732 inline constexpr char f1(const auto octet) { 733 return 4_UC; 734 } 735 736 template <typename Ty> 737 inline constexpr char f2(const Ty octet) { 738 return 4_UC; 739 } 740 741 void test() { 742 static_assert(f1('a') == 4); 743 static_assert(f2('a') == 4); 744 constexpr int c = f1('a') + f2('a'); 745 static_assert(c == 8); 746 } 747 } 748