[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaCXX / cxx2a-consteval.cpp
blob6ae2664046d712daee143a3bb2301b9e23ab7903
1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value %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 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 {
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);
263 namespace std {
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; };
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);
276 namespace temporaries {
278 struct A {
279 consteval int ret_i() const { return 0; }
280 consteval A ret_a() const { return A{}; }
281 constexpr ~A() { }
284 consteval int by_value_a(A a) { return a.ret_i(); }
286 consteval int const_a_ref(const A &a) {
287 return a.ret_i();
290 consteval int rvalue_ref(const A &&a) {
291 return a.ret_i();
294 consteval const A &to_lvalue_ref(const A &&a) {
295 return a;
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()); }//
323 namespace alloc {
325 consteval int f() {
326 int *A = new int(0);
327 // expected-note@-1+ {{allocation performed here was not deallocated}}
328 return *A;
331 int i1 = f(); // expected-error {{is not a constant expression}}
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; }
341 consteval int by_value_a(A a) { return a.ret_i(); }
343 consteval int const_a_ref(const A &a) {
344 return a.ret_i();
347 consteval int rvalue_ref(const A &&a) {
348 return a.ret_i();
351 consteval const A &to_lvalue_ref(const A &&a) {
352 return a;
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}}
394 namespace self_referencing {
396 struct S {
397 S* ptr = nullptr;
398 constexpr S(int i) : ptr(this) {
399 if (this == ptr && i)
400 ptr = nullptr;
402 constexpr ~S() {}
405 consteval S f(int i) {
406 return S(i);
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}}
416 struct S1 {
417 S1* ptr = nullptr;
418 consteval S1(int i) : ptr(this) {
419 if (this == ptr && i)
420 ptr = nullptr;
422 constexpr ~S1() {}
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}}
433 namespace ctor {
435 consteval int f_eval() { // expected-note+ {{declared here}}
436 return 0;
439 namespace std {
440 struct strong_ordering {
441 int n;
442 static const strong_ordering less, equal, greater;
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);
450 namespace override {
451 struct A {
452 virtual consteval void f(); // expected-note {{overridden}}
453 virtual void g(); // expected-note {{overridden}}
455 struct B : A {
456 consteval void f();
457 void g();
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}}
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}}
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}}
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}}
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}}
491 namespace operator_rewrite {
492 struct A {
493 friend consteval int operator<=>(const A&, const A&) { return 0; }
495 const bool k = A() < A();
496 static_assert(!k);
498 A a;
499 bool k2 = A() < a; // OK, does not access 'a'.
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;
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}}
510 struct A {
511 int(*ptr)();
512 consteval A(int(*p)() = nullptr) : ptr(p) {}
515 struct B {
516 int(*ptr)();
517 B() : ptr(nullptr) {}
518 consteval B(int(*p)(), int) : ptr(p) {}
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}}
558 namespace copy_ctor {
560 consteval int f_eval() { // expected-note+ {{declared here}}
561 return 0;
564 struct Copy {
565 int(*ptr)();
566 constexpr Copy(int(*p)() = nullptr) : ptr(p) {}
567 consteval Copy(const Copy&) = default;
570 constexpr const Copy &to_lvalue_ref(const Copy &&a) {
571 return a;
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}}
608 } // namespace special_ctor
610 namespace unevaluated {
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; };
615 long f(); // expected-note {{declared here}}
616 auto consteval g(auto a) {
617 return a;
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}}
623 using T = decltype(g(f()));
624 static_assert(is_same<long, T>::value);
626 } // namespace unevaluated
628 namespace value_dependent {
630 consteval int foo(int x) {
631 return x;
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);
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);
649 static_assert(bar<15>() == 15);
650 static_assert(baz<int>() == sizeof(int));
651 } // namespace value_dependent
653 // https://github.com/llvm/llvm-project/issues/55601
654 namespace issue_55601 {
655 template<typename T>
656 class Bar {
657 consteval static T x() { return 5; } // expected-note {{non-constexpr constructor 'derp' cannot be used in a constant expression}}
658 public:
659 Bar() : a(x()) {} // expected-error {{call to consteval function 'issue_55601::Bar<issue_55601::derp>::x' is not a constant expression}}
660 // expected-error@-1 {{call to consteval function 'issue_55601::derp::operator int' is not a constant expression}}
661 // expected-note@-2 {{in call to 'x()'}}
662 // expected-note@-3 {{non-literal type 'issue_55601::derp' cannot be used in a constant expression}}
663 private:
664 int a;
666 Bar<int> f;
667 Bar<float> g;
669 struct derp {
670 // Can't be used in a constant expression
671 derp(int); // expected-note {{declared here}}
672 consteval operator int() const { return 5; }
674 Bar<derp> a; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}}
676 struct constantDerp {
677 // Can be used in a constant expression.
678 consteval constantDerp(int) {}
679 consteval operator int() const { return 5; }
681 Bar<constantDerp> b;
683 } // namespace issue_55601
685 namespace default_argument {
687 // Previously calls of consteval functions in default arguments were rejected.
688 // Now we show that we don't reject such calls.
689 consteval int foo() { return 1; }
690 consteval int bar(int i = foo()) { return i * i; }
692 struct Test1 {
693 Test1(int i = bar(13)) {}
694 void v(int i = bar(13) * 2 + bar(15)) {}
696 Test1 t1;
698 struct Test2 {
699 constexpr Test2(int i = bar()) {}
700 constexpr void v(int i = bar(bar(bar(foo())))) {}
702 Test2 t2;
704 } // namespace default_argument
706 namespace PR50779 {
707 struct derp {
708 int b = 0;
711 constexpr derp d;
713 struct test {
714 consteval int operator[](int i) const { return {}; }
715 consteval const derp * operator->() const { return &d; }
716 consteval int f() const { return 12; } // expected-note 2{{declared here}}
719 constexpr test a;
721 // We previously rejected both of these overloaded operators as taking the
722 // address of a consteval function outside of an immediate context, but we
723 // accepted direct calls to the overloaded operator. Now we show that we accept
724 // both forms.
725 constexpr int s = a.operator[](1);
726 constexpr int t = a[1];
727 constexpr int u = a.operator->()->b;
728 constexpr int v = a->b;
729 // FIXME: I believe this case should work, but we currently reject.
730 constexpr int w = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
731 constexpr int x = a.f();
733 // Show that we reject when not in an immediate context.
734 int w2 = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
737 namespace PR48235 {
738 consteval int d() {
739 return 1;
742 struct A {
743 consteval int a() const { return 1; }
745 void b() {
746 this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
747 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
750 void c() {
751 a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
752 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
755 } // PR48235
757 namespace NamespaceScopeConsteval {
758 struct S {
759 int Val; // expected-note {{subobject declared here}}
760 consteval S() {}
763 S s1; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \
764 expected-note {{subobject of type 'int' is not initialized}}
766 template <typename Ty>
767 struct T {
768 Ty Val; // expected-note {{subobject declared here}}
769 consteval T() {}
772 T<int> t; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \
773 expected-note {{subobject of type 'int' is not initialized}}
775 } // namespace NamespaceScopeConsteval
777 namespace Issue54578 {
778 // We expect the user-defined literal to be resovled entirely at compile time
779 // despite being instantiated through a template.
780 inline consteval unsigned char operator""_UC(const unsigned long long n) {
781 return static_cast<unsigned char>(n);
784 inline constexpr char f1(const auto octet) {
785 return 4_UC;
788 template <typename Ty>
789 inline constexpr char f2(const Ty octet) {
790 return 4_UC;
793 void test() {
794 static_assert(f1('a') == 4);
795 static_assert(f2('a') == 4);
796 constexpr int c = f1('a') + f2('a');
797 static_assert(c == 8);
801 namespace defaulted_special_member_template {
802 template <typename T>
803 struct default_ctor {
804 T data;
805 consteval default_ctor() = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
808 template <typename T>
809 struct copy {
810 T data;
812 consteval copy(const copy &) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
813 consteval copy &operator=(const copy &) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
814 copy() = default;
817 template <typename T>
818 struct move {
819 T data;
821 consteval move(move &&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
822 consteval move &operator=(move &&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
823 move() = default;
826 struct foo {
827 foo() {} // expected-note {{declared here}}
828 foo(const foo &) {} // expected-note {{declared here}}
829 foo(foo &&) {} // expected-note {{declared here}}
831 foo& operator=(const foo &) { return *this; } // expected-note {{declared here}}
832 foo& operator=(foo &&) { return *this; } // expected-note {{declared here}}
835 void func() {
836 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}} \
837 expected-note {{in call to 'default_ctor()'}}
839 copy<foo> good0;
840 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}} \
841 expected-note {{in call to 'copy(good0)'}}
842 fail1 = good0; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \
843 expected-note {{in call to '&fail1->operator=(good0)'}}
845 move<foo> good1;
846 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}} \
847 expected-note {{in call to 'move(good1)'}}
848 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}} \
849 expected-note {{in call to '&fail2->operator=(good1)'}}
851 } // namespace defaulted_special_member_template
853 namespace multiple_default_constructors {
854 struct Foo {
855 Foo() {} // expected-note {{declared here}}
857 struct Bar {
858 Bar() = default;
860 struct Baz {
861 consteval Baz() {}
864 template <typename T, unsigned N>
865 struct S {
866 T data;
867 S() requires (N==1) = default;
868 // This cannot be used in constexpr context.
869 S() requires (N==2) {} // expected-note {{declared here}}
870 consteval S() requires (N==3) = default; // expected-note {{non-constexpr constructor 'Foo' cannot be used in a constant expression}}
873 void func() {
874 // Explictly defaulted constructor.
875 S<Foo, 1> s1;
876 S<Bar, 1> s2;
877 // User provided constructor.
878 S<Foo, 2> s3;
879 S<Bar, 2> s4;
880 // Consteval explictly defaulted constructor.
881 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}} \
882 expected-note {{in call to 'S()'}}
883 S<Bar, 3> s6;
884 S<Baz, 3> s7;
887 consteval int aConstevalFunction() { // expected-error {{consteval function never produces a constant expression}}
888 // Defaulted default constructors are implicitly consteval.
889 S<Bar, 1> s1;
891 S<Baz, 2> s4; // expected-note {{non-constexpr constructor 'S' cannot be used in a constant expression}}
893 S<Bar, 3> s2;
894 S<Baz, 3> s3;
895 return 0;
898 } // namespace multiple_default_constructors
900 namespace GH50055 {
901 enum E {e1=0, e2=1};
902 consteval int testDefaultArgForParam(E eParam = (E)-1) {
903 // expected-error@-1 {{integer value -1 is outside the valid range of values [0, 1] for this enumeration type}}
904 return (int)eParam;
907 int test() {
908 return testDefaultArgForParam() + testDefaultArgForParam((E)1);
912 namespace GH51182 {
913 // Nested consteval function.
914 consteval int f(int v) {
915 return v;
918 template <typename T>
919 consteval int g(T a) {
920 // An immediate function context.
921 int n = f(a);
922 return n;
924 static_assert(g(100) == 100);
925 // --------------------------------------
926 template <typename T>
927 consteval T max(const T& a, const T& b) {
928 return (a > b) ? a : b;
930 template <typename T>
931 consteval T mid(const T& a, const T& b, const T& c) {
932 T m = max(max(a, b), c);
933 if (m == a)
934 return max(b, c);
935 if (m == b)
936 return max(a, c);
937 return max(a, b);
939 static_assert(max(1,2)==2);
940 static_assert(mid(1,2,3)==2);
941 } // namespace GH51182
943 // https://github.com/llvm/llvm-project/issues/56183
944 namespace GH56183 {
945 consteval auto Foo(auto c) { return c; }
946 consteval auto Bar(auto f) { return f(); }
947 void test() {
948 constexpr auto x = Foo(Bar([] { return 'a'; }));
949 static_assert(x == 'a');
951 } // namespace GH56183
953 // https://github.com/llvm/llvm-project/issues/51695
954 namespace GH51695 {
955 // Original ========================================
956 template <typename T>
957 struct type_t {};
959 template <typename...>
960 struct list_t {};
962 template <typename T, typename... Ts>
963 consteval auto pop_front(list_t<T, Ts...>) -> auto {
964 return list_t<Ts...>{};
967 template <typename... Ts, typename F>
968 consteval auto apply(list_t<Ts...>, F fn) -> auto {
969 return fn(type_t<Ts>{}...);
972 void test1() {
973 constexpr auto x = apply(pop_front(list_t<char, char>{}),
974 []<typename... Us>(type_t<Us>...) { return 42; });
975 static_assert(x == 42);
977 // Reduced 1 ========================================
978 consteval bool zero() { return false; }
980 template <typename F>
981 consteval bool foo(bool, F f) {
982 return f();
985 void test2() {
986 constexpr auto x = foo(zero(), []() { return true; });
987 static_assert(x);
990 // Reduced 2 ========================================
991 template <typename F>
992 consteval auto bar(F f) { return f;}
994 void test3() {
995 constexpr auto t1 = bar(bar(bar(bar([]() { return true; }))))();
996 static_assert(t1);
998 int a = 1; // expected-note {{declared here}}
999 auto t2 = bar(bar(bar(bar([=]() { return a; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}}
1000 // expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}}
1002 constexpr auto t3 = bar(bar([x=bar(42)]() { return x; }))();
1003 static_assert(t3==42);
1004 constexpr auto t4 = bar(bar([x=bar(42)]() consteval { return x; }))();
1005 static_assert(t4==42);
1008 } // namespace GH51695
1010 // https://github.com/llvm/llvm-project/issues/50455
1011 namespace GH50455 {
1012 void f() {
1013 []() consteval { int i{}; }();
1014 []() consteval { int i{}; ++i; }();
1016 void g() {
1017 (void)[](int i) consteval { return i; }(0);
1018 (void)[](int i) consteval { return i; }(0);
1020 } // namespace GH50455
1022 namespace GH58302 {
1023 struct A {
1024 consteval A(){}
1025 consteval operator int() { return 1;}
1028 int f() {
1029 int x = A{};