[mlir] Fix typo in test vector transform pass descriptions (#118194)
[llvm-project.git] / clang / test / SemaCXX / cxx2a-consteval.cpp
blobfef4674d178412e72be0aaaa5a632d3b626923fd
1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify
3 typedef __SIZE_TYPE__ size_t;
5 namespace basic_sema {
7 consteval int f1(int i) {
8 return i;
11 consteval constexpr int f2(int i) {
12 //expected-error@-1 {{cannot combine}}
13 return i;
16 constexpr auto l_eval = [](int i) consteval {
17 // expected-note@-1+ {{declared here}}
19 return i;
22 constexpr consteval int f3(int i) {
23 //expected-error@-1 {{cannot combine}}
24 return i;
27 struct A {
28 consteval int f1(int i) const {
29 // expected-note@-1 {{declared here}}
30 return i;
32 consteval A(int i);
33 consteval A() = default;
34 consteval ~A() = default; // expected-error {{destructor cannot be declared consteval}}
37 consteval struct B {}; // expected-error {{struct cannot be marked consteval}}
39 consteval typedef B b; // expected-error {{typedef cannot be consteval}}
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}}
44 consteval int i = 0; // expected-error {{consteval can only be used in function declarations}}
46 consteval int; // expected-error {{consteval can only be used in function declarations}}
48 consteval int f1() {} // expected-error {{no return statement in consteval function}}
50 struct C {
51 C() {}
52 ~C() {}
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}}
61 struct E : C {
62 consteval ~E() {} // expected-error {{cannot be declared consteval}}
66 consteval int main() { // expected-error {{'main' is not allowed to be declared consteval}}
67 return 0;
70 consteval int f_eval(int i) {
71 // expected-note@-1+ {{declared here}}
72 return i;
75 namespace taking_address {
77 using func_type = int(int);
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}}
84 auto p = f_eval;
85 // expected-error@-1 {{take address}}
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}}
92 consteval int f(int i) {
93 // expected-note@-1+ {{declared here}}
94 return i;
97 auto ptr = &f;
98 // expected-error@-1 {{take address}}
100 auto f1() {
101 return &f;
102 // expected-error@-1 {{take address}}
107 namespace invalid_function {
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}}
124 namespace nested {
125 consteval int f() {
126 return 0;
129 consteval int f1(...) {
130 return 1;
133 enum E {};
135 using T = int(&)();
137 consteval auto operator+ (E, int(*a)()) {
138 return 0;
141 void d() {
142 auto i = f1(E() + &f);
145 auto l0 = [](auto) consteval {
146 return 0;
149 int i0 = l0(&f1);
151 int i1 = f1(l0(4));
153 int i2 = f1(&f1, &f1, &f1, &f1, &f1, &f1, &f1);
155 int i3 = f1(f1(f1(&f1, &f1), f1(&f1, &f1), f1(f1(&f1, &f1), &f1)));
159 namespace user_defined_literal {
161 consteval int operator""_test(unsigned long long i) {
162 // expected-note@-1+ {{declared here}}
163 return 0;
166 int i = 0_test;
168 auto ptr = &operator""_test;
169 // expected-error@-1 {{take address}}
171 consteval auto operator""_test1(unsigned long long i) {
172 return &f_eval;
175 auto i1 = 0_test1; // expected-error {{is not a constant expression}}
176 // expected-note@-1 {{is not a constant expression}}
180 namespace return_address {
182 consteval int f() {
183 // expected-note@-1 {{declared here}}
184 return 0;
187 consteval int(*ret1(int i))() {
188 return &f;
191 auto ptr = ret1(0);
192 // expected-error@-1 {{is not a constant expression}}
193 // expected-note@-2 {{pointer to a consteval}}
195 struct A {
196 consteval int f(int) {
197 // expected-note@-1+ {{declared here}}
198 return 0;
202 using mem_ptr_type = int (A::*)(int);
204 template<mem_ptr_type ptr>
205 struct C {};
207 C<&A::f> c;
208 // expected-error@-1 {{is not a constant expression}}
209 // expected-note@-2 {{pointer to a consteval}}
211 consteval mem_ptr_type ret2() {
212 return &A::f;
215 C<ret2()> c1;
216 // expected-error@-1 {{is not a constant expression}}
217 // expected-note@-2 {{pointer to a consteval}}
221 namespace context {
223 int g_i;
224 // expected-note@-1 {{declared here}}
226 consteval int f(int) {
227 return 0;
230 constexpr int c_i = 0;
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);
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);
245 consteval int f_eval(int i) {
246 return f(i);
249 auto l0 = [](int i) consteval {
250 return f(i);
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);
259 int(*test)(int) = l1;
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; }
269 consteval S fn() { return S(5); }
271 template <typename T>
272 void fn2() {
273 (void)[]() consteval -> int {
274 return *(fn().value); // OK, immediate context
278 void caller() {
279 fn2<int>();
283 namespace std {
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; };
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);
296 namespace temporaries {
298 struct A {
299 consteval int ret_i() const { return 0; }
300 consteval A ret_a() const { return A{}; }
301 constexpr ~A() { }
304 consteval int by_value_a(A a) { return a.ret_i(); }
306 consteval int const_a_ref(const A &a) {
307 return a.ret_i();
310 consteval int rvalue_ref(const A &&a) {
311 return a.ret_i();
314 consteval const A &to_lvalue_ref(const A &&a) {
315 return a;
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()); }//
343 namespace alloc {
345 consteval int f() {
346 int *A = new int(0);
347 // expected-note@-1+ {{allocation performed here was not deallocated}}
348 return *A;
351 int i1 = f(); // expected-error {{is not a constant expression}}
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; }
361 consteval int by_value_a(A a) { return a.ret_i(); }
363 consteval int const_a_ref(const A &a) {
364 return a.ret_i();
367 consteval int rvalue_ref(const A &&a) {
368 return a.ret_i();
371 consteval const A &to_lvalue_ref(const A &&a) {
372 return a;
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()); }
406 namespace self_referencing {
408 struct S {
409 S* ptr = nullptr;
410 constexpr S(int i) : ptr(this) {
411 if (this == ptr && i)
412 ptr = nullptr;
414 constexpr ~S() {}
417 consteval S f(int i) {
418 return S(i);
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}}
428 struct S1 {
429 S1* ptr = nullptr;
430 consteval S1(int i) : ptr(this) {
431 if (this == ptr && i)
432 ptr = nullptr;
434 constexpr ~S1() {}
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}}
445 namespace ctor {
447 consteval int f_eval() { // expected-note+ {{declared here}}
448 return 0;
451 namespace std {
452 struct strong_ordering {
453 int n;
454 static const strong_ordering less, equal, greater;
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);
462 namespace override {
463 struct A {
464 virtual consteval void f(); // expected-note {{overridden}}
465 virtual void g(); // expected-note {{overridden}}
467 struct B : A {
468 consteval void f();
469 void g();
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}}
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}}
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}}
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}}
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}}
503 namespace operator_rewrite {
504 struct A {
505 friend consteval int operator<=>(const A&, const A&) { return 0; }
507 const bool k = A() < A();
508 static_assert(!k);
510 A a;
511 bool k2 = A() < a; // OK, does not access 'a'.
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;
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}}
522 struct A {
523 int(*ptr)();
524 consteval A(int(*p)() = nullptr) : ptr(p) {}
527 struct B {
528 int(*ptr)();
529 B() : ptr(nullptr) {}
530 consteval B(int(*p)(), int) : ptr(p) {}
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}}
570 namespace copy_ctor {
572 consteval int f_eval() { // expected-note+ {{declared here}}
573 return 0;
576 struct Copy {
577 int(*ptr)();
578 constexpr Copy(int(*p)() = nullptr) : ptr(p) {}
579 consteval Copy(const Copy&) = default;
582 constexpr const Copy &to_lvalue_ref(const Copy &&a) {
583 return a;
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}}
620 } // namespace special_ctor
622 namespace unevaluated {
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; };
627 long f(); // expected-note {{declared here}}
628 auto consteval g(auto a) {
629 return a;
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}}
635 using T = decltype(g(f()));
636 static_assert(is_same<long, T>::value);
638 } // namespace unevaluated
640 namespace value_dependent {
642 consteval int foo(int x) {
643 return x;
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);
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);
661 static_assert(bar<15>() == 15);
662 static_assert(baz<int>() == sizeof(int));
663 } // namespace value_dependent
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;
678 Bar<int> f;
679 Bar<float> g;
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; }
686 Bar<derp> a; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}}
688 struct constantDerp {
689 // Can be used in a constant expression.
690 consteval constantDerp(int) {}
691 consteval operator int() const { return 5; }
693 Bar<constantDerp> b;
695 } // namespace issue_55601
697 namespace default_argument {
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; }
704 struct Test1 {
705 Test1(int i = bar(13)) {}
706 void v(int i = bar(13) * 2 + bar(15)) {}
708 Test1 t1;
710 struct Test2 {
711 constexpr Test2(int i = bar()) {}
712 constexpr void v(int i = bar(bar(bar(foo())))) {}
714 Test2 t2;
716 } // namespace default_argument
718 namespace PR50779 {
719 struct derp {
720 int b = 0;
723 constexpr derp d;
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}}
731 constexpr test a;
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();
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}}
749 namespace PR48235 {
750 consteval int d() {
751 return 1;
754 struct A {
755 consteval int a() const { return 1; }
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}}
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}}
767 } // PR48235
769 namespace NamespaceScopeConsteval {
770 struct S {
771 int Val; // expected-note {{subobject declared here}}
772 consteval S() {}
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}}
778 template <typename Ty>
779 struct T {
780 Ty Val; // expected-note {{subobject declared here}}
781 consteval T() {}
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}}
787 } // namespace NamespaceScopeConsteval
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);
796 inline constexpr char f1(const auto octet) {
797 return 4_UC;
800 template <typename Ty>
801 inline constexpr char f2(const Ty octet) {
802 return 4_UC;
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);
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}}
820 template <typename T>
821 struct copy {
822 T data;
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;
829 template <typename T>
830 struct move {
831 T data;
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;
838 struct foo {
839 foo() {} // expected-note {{declared here}}
840 foo(const foo &) {} // expected-note {{declared here}}
841 foo(foo &&) {} // expected-note {{declared here}}
843 foo& operator=(const foo &) { return *this; } // expected-note {{declared here}}
844 foo& operator=(foo &&) { return *this; } // expected-note {{declared here}}
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()'}}
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)'}}
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)'}}
863 } // namespace defaulted_special_member_template
865 namespace multiple_default_constructors {
866 struct Foo {
867 Foo() {} // expected-note {{declared here}}
869 struct Bar {
870 Bar() = default;
872 struct Baz {
873 consteval Baz() {}
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}}
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;
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;
903 S<Baz, 2> s4; // expected-note {{non-constexpr constructor 'S' cannot be used in a constant expression}}
905 S<Bar, 3> s2;
906 S<Baz, 3> s3;
907 return 0;
910 } // namespace multiple_default_constructors
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;
919 int test() {
920 return testDefaultArgForParam() + testDefaultArgForParam((E)1);
921 // expected-error@-1 {{call to consteval function 'GH50055::testDefaultArgForParam' is not a constant expression}}
925 namespace GH51182 {
926 // Nested consteval function.
927 consteval int f(int v) {
928 return v;
931 template <typename T>
932 consteval int g(T a) {
933 // An immediate function context.
934 int n = f(a);
935 return n;
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;
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);
952 static_assert(max(1,2)==2);
953 static_assert(mid(1,2,3)==2);
954 } // namespace GH51182
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');
964 } // namespace GH56183
966 // https://github.com/llvm/llvm-project/issues/51695
967 namespace GH51695 {
968 // Original ========================================
969 template <typename T>
970 struct type_t {};
972 template <typename...>
973 struct list_t {};
975 template <typename T, typename... Ts>
976 consteval auto pop_front(list_t<T, Ts...>) -> auto {
977 return list_t<Ts...>{};
980 template <typename... Ts, typename F>
981 consteval auto apply(list_t<Ts...>, F fn) -> auto {
982 return fn(type_t<Ts>{}...);
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);
990 // Reduced 1 ========================================
991 consteval bool zero() { return false; }
993 template <typename F>
994 consteval bool foo(bool, F f) {
995 return f();
998 void test2() {
999 constexpr auto x = foo(zero(), []() { return true; });
1000 static_assert(x);
1003 // Reduced 2 ========================================
1004 template <typename F>
1005 consteval auto bar(F f) { return f;}
1007 void test3() {
1008 constexpr auto t1 = bar(bar(bar(bar([]() { return true; }))))();
1009 static_assert(t1);
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}}
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);
1021 } // namespace GH51695
1023 // https://github.com/llvm/llvm-project/issues/50455
1024 namespace GH50455 {
1025 void f() {
1026 []() consteval { int i{}; }();
1027 []() consteval { int i{}; ++i; }();
1029 void g() {
1030 (void)[](int i) consteval { return i; }(0);
1031 (void)[](int i) consteval { return i; }(0);
1033 } // namespace GH50455
1035 namespace GH58302 {
1036 struct A {
1037 consteval A(){}
1038 consteval operator int() { return 1;}
1041 int f() {
1042 int x = A{};
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;
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}}
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;
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}}
1064 int (*f3)(void) = []() consteval { return 3; }; // expected-error {{cannot take address of consteval call operator of '(lambda at}} \
1065 // expected-note {{declared here}}
1068 consteval void consteval_test() {
1069 constexpr auto l1 = []() consteval { return 3; };
1071 int (*f1)(void) = l1; // ok
1075 namespace GH60286 {
1077 struct A {
1078 int i = 0;
1080 consteval A() {}
1081 A(const A&) { i = 1; }
1082 consteval int f() { return i; }
1085 constexpr auto B = A{A{}}.f();
1086 static_assert(B == 0);
1090 namespace GH58207 {
1091 struct tester {
1092 consteval tester(const char* name) noexcept { }
1094 consteval const char* make_name(const char* name) { return name;}
1095 consteval const char* pad(int P) { return "thestring"; }
1097 int bad = 10; // expected-note 6{{declared here}}
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}}
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}}
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}}
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}}
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}}
1128 namespace GH64949 {
1129 struct f {
1130 int g; // expected-note 2{{subobject declared here}}
1131 constexpr ~f() {}
1133 class h {
1135 public:
1136 consteval h(char *) {}
1137 consteval operator int() const { return 1; }
1138 f i;
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}}
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}}
1152 namespace GH65985 {
1154 int consteval operator""_foo(unsigned long long V) {
1155 return 0;
1157 int consteval operator""_bar(unsigned long long V); // expected-note 3{{here}}
1159 int consteval f() {
1160 return 0;
1163 int consteval g(); // expected-note {{here}}
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 {{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}}
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}}
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 {{undefined function 'g' cannot be used in a constant expression}}
1187 namespace GH66562 {
1189 namespace ns
1191 consteval int foo(int x) { return 1; } // expected-note {{declared here}} \
1192 // expected-note {{passing argument to parameter 'x' here}}
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 *'}}
1201 template class T<char*>; // expected-note {{in instantiation}}
1205 namespace GH65520 {
1207 consteval int bar (int i) { if (i != 1) return 1/0; return 0; }
1208 // expected-note@-1{{division by zero}}
1210 void
1211 g ()
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)'}}
1218 consteval int undefined(); // expected-note {{declared here}}
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}}
1229 namespace GH105558 {
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) {}
1240 consteval void g4(X x) { delete x.p; }
1242 void test() {
1243 f(alloc());
1244 g1(alloc());
1245 g2(alloc());
1246 g3(alloc());
1247 g4(alloc());
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 {
1257 consteval void consteval_func() {}
1259 template <auto> struct define_f {
1260 friend void foo() {}
1263 template <auto = [] {}> struct A {};
1265 struct B {
1266 template <auto T> consteval void func() { (void)define_f<T>{}; }
1269 int main() {
1270 B{}.func<A{}>();
1271 consteval_func();
1274 } // namespace GH107175