brintos

brintos / llvm-project-archived public Read only

0
0
Text · 41.5 KiB · 92bfa40 Raw
1346 lines · cpp
1// RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify2 3typedef __SIZE_TYPE__ size_t;4 5namespace basic_sema {6 7consteval int f1(int i) {8  return i;9}10 11consteval constexpr int f2(int i) {12  //expected-error@-1 {{cannot combine}}13  return i;14}15 16constexpr auto l_eval = [](int i) consteval {17// expected-note@-1+ {{declared here}}18 19  return i;20};21 22constexpr consteval int f3(int i) {23  //expected-error@-1 {{cannot combine}}24  return i;25}26 27struct 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 37consteval struct B {}; // expected-error {{struct cannot be marked consteval}}38 39consteval typedef B b; // expected-error {{typedef cannot be consteval}}40 41consteval int redecl() {return 0;} // expected-note {{previous declaration is here}}42constexpr int redecl() {return 0;} // expected-error {{constexpr declaration of 'redecl' follows consteval declaration}}43 44consteval int i = 0; // expected-error {{consteval can only be used in function declarations}}45 46consteval int; // expected-error {{consteval can only be used in function declarations}}47 48consteval int f1() {} // expected-error {{no return statement in consteval function}}49 50struct C {51  C() {}52  ~C() {}53};54 55struct 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 61struct E : C {62  consteval ~E() {} // expected-error {{cannot be declared consteval}}63};64}65 66consteval int main() { // expected-error {{'main' is not allowed to be declared consteval}}67  return 0;68}69 70consteval int f_eval(int i) {71// expected-note@-1+ {{declared here}}72  return i;73}74 75namespace taking_address {76 77using func_type = int(int);78 79func_type* p1 = (&f_eval);80// expected-error@-1 {{take address}}81func_type* p7 = __builtin_addressof(f_eval);82// expected-error@-1 {{take address}}83 84auto p = f_eval;85// expected-error@-1 {{take address}}86 87auto m1 = &basic_sema::A::f1;88// expected-error@-1 {{take address}}89auto l1 = &decltype(basic_sema::l_eval)::operator();90// expected-error@-1 {{take address}}91 92consteval int f(int i) {93// expected-note@-1+ {{declared here}}94  return i;95}96 97auto ptr = &f;98// expected-error@-1 {{take address}}99 100auto f1() {101  return &f;102// expected-error@-1 {{take address}}103}104 105}106 107namespace invalid_function {108 109struct 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 124namespace nested {125consteval int f() {126  return 0;127}128 129consteval int f1(...) {130  return 1;131}132 133enum E {};134 135using T = int(&)();136 137consteval auto operator+ (E, int(*a)()) {138  return 0;139}140 141void d() {142  auto i = f1(E() + &f);143}144 145auto l0 = [](auto) consteval {146  return 0;147};148 149int i0 = l0(&f1);150 151int i1 = f1(l0(4));152 153int i2 = f1(&f1, &f1, &f1, &f1, &f1, &f1, &f1);154 155int i3 = f1(f1(f1(&f1, &f1), f1(&f1, &f1), f1(f1(&f1, &f1), &f1)));156 157}158 159namespace user_defined_literal {160 161consteval int operator""_test(unsigned long long i) {162// expected-note@-1+ {{declared here}}163  return 0;164}165 166int i = 0_test;167 168auto ptr = &operator""_test;169// expected-error@-1 {{take address}}170 171consteval auto operator""_test1(unsigned long long i) {172  return &f_eval;173}174 175auto i1 = 0_test1; // expected-error {{is not a constant expression}}176// expected-note@-1 {{is not a constant expression}}177 178}179 180namespace return_address {181 182consteval int f() {183// expected-note@-1 {{declared here}}184  return 0;185}186 187consteval int(*ret1(int i))() {188  return &f;189}190 191auto ptr = ret1(0);192// expected-error@-1 {{is not a constant expression}}193// expected-note@-2 {{pointer to a consteval}}194 195struct A {196  consteval int f(int) {197    // expected-note@-1+ {{declared here}}198    return 0;199  }200};201 202using mem_ptr_type = int (A::*)(int);203 204template<mem_ptr_type ptr>205struct C {};206 207C<&A::f> c;208// expected-error@-1 {{is not a constant expression}}209// expected-note@-2 {{pointer to a consteval}}210 211consteval mem_ptr_type ret2() {212  return &A::f;213}214 215C<ret2()> c1;216// expected-error@-1 {{is not a constant expression}}217// expected-note@-2 {{pointer to a consteval}}218 219}220 221namespace context {222 223int g_i;224// expected-note@-1 {{declared here}}225 226consteval int f(int) {227  return 0;228}229 230constexpr int c_i = 0;231 232int t1 = f(g_i);233// expected-error@-1 {{is not a constant expression}}234// expected-note@-2 {{read of non-const variable}}235int t3 = f(c_i);236 237constexpr 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 245consteval int f_eval(int i) {246  return f(i);247}248 249auto l0 = [](int i) consteval {250  return f(i);251};252 253auto 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 259int(*test)(int)  = l1;260 261}262 263namespace consteval_lambda_in_template {264struct S {265    int *value;266    constexpr S(int v) : value(new int {v}) {}267    constexpr ~S() { delete value; }268};269consteval S fn() { return S(5); }270 271template <typename T>272void fn2() {273    (void)[]() consteval -> int {274      return *(fn().value);  // OK, immediate context275    };276}277 278void caller() {279    fn2<int>();280}281}282 283namespace std {284 285template <typename T> struct remove_reference { using type = T; };286template <typename T> struct remove_reference<T &> { using type = T; };287template <typename T> struct remove_reference<T &&> { using type = T; };288 289template <typename T>290constexpr 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 296namespace temporaries {297 298struct A {299  consteval int ret_i() const { return 0; }300  consteval A ret_a() const { return A{}; }301  constexpr ~A() { }302};303 304consteval int by_value_a(A a) { return a.ret_i(); }305 306consteval int const_a_ref(const A &a) {307  return a.ret_i();308}309 310consteval int rvalue_ref(const A &&a) {311  return a.ret_i();312}313 314consteval const A &to_lvalue_ref(const A &&a) {315  return a;316}317 318void 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 343namespace alloc {344 345consteval int f() {346  int *A = new int(0);347// expected-note@-1+ {{allocation performed here was not deallocated}}348  return *A;349}350 351int i1 = f(); // expected-error {{is not a constant expression}}352 353struct 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 361consteval int by_value_a(A a) { return a.ret_i(); }362 363consteval int const_a_ref(const A &a) {364  return a.ret_i();365}366 367consteval int rvalue_ref(const A &&a) {368  return a.ret_i();369}370 371consteval const A &to_lvalue_ref(const A &&a) {372  return a;373}374 375void 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 406namespace self_referencing {407 408struct 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 417consteval S f(int i) {418  return S(i);419}420 421void 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 428struct 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 437void 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}445namespace ctor {446 447consteval int f_eval() { // expected-note+ {{declared here}}448  return 0;449}450 451namespace 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 462namespace 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 503namespace 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 522struct A {523  int(*ptr)();524  consteval A(int(*p)() = nullptr) : ptr(p) {}525};526 527struct B {528  int(*ptr)();529  B() : ptr(nullptr) {}530  consteval B(int(*p)(), int) : ptr(p) {}531};532 533void 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 570namespace copy_ctor {571 572consteval int f_eval() { // expected-note+ {{declared here}}573  return 0;574}575 576struct Copy {577  int(*ptr)();578  constexpr Copy(int(*p)() = nullptr) : ptr(p) {}579  consteval Copy(const Copy&) = default;580};581 582constexpr const Copy &to_lvalue_ref(const Copy &&a) {583  return a;584}585 586void 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_ctor621 622namespace unevaluated {623 624template <typename T, typename U> struct is_same { static const bool value = false; };625template <typename T> struct is_same<T, T> { static const bool value = true; };626 627long f(); // expected-note {{declared here}}628auto consteval g(auto a) {629  return a;630}631 632auto 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 635using T = decltype(g(f()));636static_assert(is_same<long, T>::value);637 638} // namespace unevaluated639 640namespace value_dependent {641 642consteval int foo(int x) {643  return x;644}645 646template <int X> constexpr int bar() {647  // Previously this call was rejected as value-dependent constant expressions648  // can't be immediately evaluated. Now we show that we don't immediately649  // evaluate them until they are instantiated.650  return foo(X);651}652 653template <typename T> constexpr int baz() {654  constexpr int t = sizeof(T);655  // Previously this call was rejected as `t` is value-dependent and its value656  // is unknown until the function is instantiated. Now we show that we don't657  // reject such calls.658  return foo(t);659}660 661static_assert(bar<15>() == 15);662static_assert(baz<int>() == sizeof(int));663} // namespace value_dependent664 665// https://github.com/llvm/llvm-project/issues/55601666namespace issue_55601 {667template<typename T>668class 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};678Bar<int> f;679Bar<float> g;680 681struct derp {682  // Can't be used in a constant expression683  derp(int); // expected-note {{declared here}}684  consteval operator int() const { return 5; }685};686Bar<derp> a; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}}687 688struct constantDerp {689  // Can be used in a constant expression.690  consteval constantDerp(int) {}691  consteval operator int() const { return 5; }692};693Bar<constantDerp> b;694 695} // namespace issue_55601696 697namespace 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.701consteval int foo() { return 1; }702consteval int bar(int i = foo()) { return i * i; }703 704struct Test1 {705  Test1(int i = bar(13)) {}706  void v(int i = bar(13) * 2 + bar(15)) {}707};708Test1 t1;709 710struct Test2 {711  constexpr Test2(int i = bar()) {}712  constexpr void v(int i = bar(bar(bar(foo())))) {}713};714Test2 t2;715 716} // namespace default_argument717 718namespace PR50779 {719struct derp {720  int b = 0;721};722 723constexpr derp d;724 725struct 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 731constexpr test a;732 733// We previously rejected both of these overloaded operators as taking the734// address of a consteval function outside of an immediate context, but we735// accepted direct calls to the overloaded operator. Now we show that we accept736// both forms.737constexpr int s = a.operator[](1);738constexpr int t = a[1];739constexpr int u = a.operator->()->b;740constexpr int v = a->b;741// FIXME: I believe this case should work, but we currently reject.742constexpr int w = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}743constexpr int x = a.f();744 745// Show that we reject when not in an immediate context.746int w2 = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}747}748 749namespace PR48235 {750consteval int d() {751  return 1;752}753 754struct 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} // PR48235768 769namespace NamespaceScopeConsteval {770struct S {771  int Val; // expected-note {{subobject declared here}}772  consteval S() {}773};774 775S s1; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \776         expected-note {{subobject 'Val' is not initialized}}777 778template <typename Ty>779struct T {780  Ty Val; // expected-note {{subobject declared here}}781  consteval T() {}782};783 784T<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 NamespaceScopeConsteval788 789namespace Issue54578 {790// We expect the user-defined literal to be resovled entirely at compile time791// despite being instantiated through a template.792inline consteval unsigned char operator""_UC(const unsigned long long n) {793  return static_cast<unsigned char>(n);794}795 796inline constexpr char f1(const auto octet) {797  return 4_UC;798}799 800template <typename Ty>801inline constexpr char f2(const Ty octet) {802  return 4_UC;803}804 805void 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 813namespace defaulted_special_member_template {814template <typename T>815struct 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 820template <typename T>821struct 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 829template <typename T>830struct 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 838struct 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 847void 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_template864 865namespace multiple_default_constructors {866struct Foo {867  Foo() {} // expected-note {{declared here}}868};869struct Bar {870  Bar() = default;871};872struct Baz {873  consteval Baz() {}874};875 876template <typename T, unsigned N>877struct 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 885void 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 899consteval 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_constructors911 912namespace GH50055 {913enum E {e1=0, e2=1};914consteval 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 919int 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 925namespace GH51182 {926// Nested consteval function.927consteval int f(int v) {928  return v;929}930 931template <typename T>932consteval int g(T a) {933  // An immediate function context.934  int n = f(a);935  return n;936}937static_assert(g(100) == 100);938// --------------------------------------939template <typename T>940consteval T max(const T& a, const T& b) {941    return (a > b) ? a : b;942}943template <typename T>944consteval 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}952static_assert(max(1,2)==2);953static_assert(mid(1,2,3)==2);954} // namespace GH51182955 956// https://github.com/llvm/llvm-project/issues/56183957namespace GH56183 {958consteval auto Foo(auto c) { return c; }959consteval auto Bar(auto f) { return f(); }960void test() {961  constexpr auto x = Foo(Bar([] { return 'a'; }));962  static_assert(x == 'a');963}964}  // namespace GH56183965 966// https://github.com/llvm/llvm-project/issues/51695967namespace GH51695 {968// Original ========================================969template <typename T>970struct type_t {};971 972template <typename...>973struct list_t {};974 975template <typename T, typename... Ts>976consteval auto pop_front(list_t<T, Ts...>) -> auto {977  return list_t<Ts...>{};978}979 980template <typename... Ts, typename F>981consteval auto apply(list_t<Ts...>, F fn) -> auto {982  return fn(type_t<Ts>{}...);983}984 985void 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 ========================================991consteval bool zero() { return false; }992 993template <typename F>994consteval bool foo(bool, F f) {995  return f();996}997 998void test2() {999  constexpr auto x = foo(zero(), []() { return true; });1000  static_assert(x);1001}1002 1003// Reduced 2 ========================================1004template <typename F>1005consteval auto bar(F f) { return f;}1006 1007void 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 GH516951022 1023// https://github.com/llvm/llvm-project/issues/504551024namespace GH50455 {1025void f() {1026  []() consteval { int i{}; }();1027  []() consteval { int i{}; ++i; }();1028}1029void g() {1030  (void)[](int i) consteval { return i; }(0);1031  (void)[](int i) consteval { return i; }(0);1032}1033}  // namespace GH504551034 1035namespace GH58302 {1036struct A {1037   consteval A(){}1038   consteval operator int() { return 1;}1039};1040 1041int f() {1042   int x = A{};1043}1044}1045 1046namespace GH57682 {1047void 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 1068consteval void consteval_test() {1069  constexpr auto l1 = []() consteval { return 3; };1070 1071  int (*f1)(void) = l1;  // ok1072}1073}1074 1075namespace GH60286 {1076 1077struct A {1078  int i = 0;1079 1080  consteval A() {}1081  A(const A&) { i = 1; }1082  consteval int f() { return i; }1083};1084 1085constexpr auto B = A{A{}}.f();1086static_assert(B == 0);1087 1088}1089 1090namespace GH58207 {1091struct tester {1092    consteval tester(const char* name) noexcept { }1093};1094consteval const char* make_name(const char* name) { return name;}1095consteval const char* pad(int P) { return "thestring"; }1096 1097int bad = 10; // expected-note 6{{declared here}}1098 1099tester glob1(make_name("glob1"));1100tester glob2(make_name("glob2"));1101constexpr tester cglob(make_name("cglob"));1102tester 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 1105constexpr tester glob3 = { make_name("glob3") };1106constexpr 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 1110auto V = make_name(pad(3));1111auto 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 1115void 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 1122void 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 1128namespace GH64949 {1129struct f {1130  int g; // expected-note 2{{subobject declared here}}1131  constexpr ~f() {}1132};1133class h {1134 1135public:1136  consteval h(char *) {}1137  consteval operator int() const { return 1; }1138  f i;1139};1140 1141void 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 1145int  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 1152namespace GH65985 {1153 1154int consteval operator""_foo(unsigned long long V) {1155    return 0;1156}1157int consteval operator""_bar(unsigned long long V); // expected-note 4 {{here}}1158 1159int consteval f() {1160  return 0;1161}1162 1163int consteval g();  // expected-note 2 {{here}}1164 1165 1166struct 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 diagnostics1174    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 1187namespace GH66562 {1188 1189namespace ns1190{1191    consteval int foo(int x) { return 1; } // expected-note {{declared here}}  \1192                                           // expected-note {{passing argument to parameter 'x' here}}1193}1194 1195template <class A>1196struct 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 1201template class T<char*>; // expected-note {{in instantiation}}1202 1203}1204 1205namespace GH65520 {1206 1207consteval int bar (int i) { if (i != 1) return 1/0; return 0; }1208// expected-note@-1{{division by zero}}1209 1210void1211g ()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 1218consteval int undefined(); // expected-note {{declared here}}1219 1220consteval 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 1229namespace GH105558 {1230 1231consteval int* alloc() { return new int(0); }1232consteval void f(int* p) { delete p; }1233consteval void g1(int*&& p) { delete p; }1234consteval void g2(const int* p) { delete p; }1235consteval void g3(int*const& p) { delete p; }1236struct X {1237  int* p;1238  explicit(false) constexpr X(int* p) : p(p) {}1239};1240consteval void g4(X x) { delete x.p; }1241 1242void 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 in1253// RemoveNestedImmediateInvocation(). Otherwise, we would end up with spurious1254// redefinition errors.1255namespace GH107175 {1256 1257consteval void consteval_func() {}1258 1259template <auto> struct define_f {1260  friend void foo() {}1261};1262 1263template <auto = [] {}> struct A {};1264 1265struct B {1266  template <auto T> consteval void func() { (void)define_f<T>{}; }1267};1268 1269int main() {1270  B{}.func<A{}>();1271  consteval_func();1272}1273 1274} // namespace GH1071751275 1276namespace GH137885 {1277 1278template <typename... P> struct A {};1279 1280template <int N>1281struct 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 1290template <typename T> struct type_identity {1291  using type = T;1292};1293 1294template <typename... P>1295void foo(typename type_identity<A<P...>>::type a, P...) {}1296 1297void 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/1391601305namespace GH139160{1306  // original test case taken from Github1307  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 GH1391601323 1324namespace GH118187 {1325 1326template <typename T> int t() {1327  return []<typename U>() consteval {1328    return [](U v) { return v; }(123);1329  }.template operator()<int>();1330}1331 1332int v = t<int>();1333}  // namespace GH1181871334 1335namespace GH156579 {1336template <class>1337auto f{[] (auto...) {1338    if constexpr ([] (auto) { return true; }(0))1339        return 0;1340}};1341 1342void g() {1343    f<int>();1344}1345}  // namespace GH1565791346