Debian Codegraph @

clang/test/SemaCXX/cxx2a-consteval.cpp

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