1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value %s -verify
2
3 typedef __SIZE_TYPE__ size_t;
4
5 namespace basic_sema {
6
f1(int i)7 consteval int f1(int i) {
8 return i;
9 }
10
f2(int i)11 consteval constexpr int f2(int i) {
12 //expected-error@-1 {{cannot combine}}
13 return i;
14 }
15
__anon616f5e1d0102(int i) 16 constexpr auto l_eval = [](int i) consteval {
17 // expected-note@-1+ {{declared here}}
18
19 return i;
20 };
21
f3(int i)22 constexpr consteval int f3(int i) {
23 //expected-error@-1 {{cannot combine}}
24 return i;
25 }
26
27 struct A {
f1basic_sema::A28 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
redecl()41 consteval int redecl() {return 0;} // expected-note {{previous declaration is here}}
redecl()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
f1()48 consteval int f1() {} // expected-error {{no return statement in consteval function}}
49
50 struct C {
Cbasic_sema::C51 C() {}
~Cbasic_sema::C52 ~C() {}
53 };
54
55 struct D {
56 C c;
57 consteval D() = default; // expected-error {{cannot be consteval}}
58 consteval ~D() = default; // expected-error {{destructor cannot be declared consteval}}
59 };
60
61 struct E : C {
~Ebasic_sema::E62 consteval ~E() {} // expected-error {{cannot be declared consteval}}
63 };
64 }
65
main()66 consteval int main() { // expected-error {{'main' is not allowed to be declared consteval}}
67 return 0;
68 }
69
f_eval(int i)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
f(int i)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
f1()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}}
~Ainvalid_function::A118 consteval ~A() {}
119 // expected-error@-1 {{destructor cannot be declared consteval}}
120 };
121
122 }
123
124 namespace nested {
f()125 consteval int f() {
126 return 0;
127 }
128
f1(...)129 consteval int f1(...) {
130 return 1;
131 }
132
133 enum E {};
134
135 using T = int(&)();
136
operator +(E,int (* a)())137 consteval auto operator+ (E, int(*a)()) {
138 return 0;
139 }
140
d()141 void d() {
142 auto i = f1(E() + &f);
143 }
144
__anon616f5e1d0202(auto) 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
operator ""_test(unsigned long long i)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
operator ""_test1(unsigned long long i)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
f()182 consteval int f() {
183 // expected-note@-1 {{declared here}}
184 return 0;
185 }
186
ret1(int i)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 {
freturn_address::A196 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
ret2()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
f(int)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
f_c(int 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);
243 }
244
f_eval(int i)245 consteval int f_eval(int i) {
246 return f(i);
247 }
248
__anon616f5e1d0302(int i) 249 auto l0 = [](int i) consteval {
250 return f(i);
251 };
252
__anon616f5e1d0402(int 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);
259 };
260
261 }
262
263 namespace std {
264
265 template <typename T> struct remove_reference { using type = T; };
266 template <typename T> struct remove_reference<T &> { using type = T; };
267 template <typename T> struct remove_reference<T &&> { using type = T; };
268
269 template <typename T>
move(T && t)270 constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {
271 return static_cast<typename std::remove_reference<T>::type &&>(t);
272 }
273
274 }
275
276 namespace temporaries {
277
278 struct A {
ret_itemporaries::A279 consteval int ret_i() const { return 0; }
ret_atemporaries::A280 consteval A ret_a() const { return A{}; }
~Atemporaries::A281 constexpr ~A() { }
282 };
283
by_value_a(A a)284 consteval int by_value_a(A a) { return a.ret_i(); }
285
const_a_ref(const A & a)286 consteval int const_a_ref(const A &a) {
287 return a.ret_i();
288 }
289
rvalue_ref(const A && a)290 consteval int rvalue_ref(const A &&a) {
291 return a.ret_i();
292 }
293
to_lvalue_ref(const A && a)294 consteval const A &to_lvalue_ref(const A &&a) {
295 return a;
296 }
297
test()298 void test() {
299 constexpr A a {};
300 { int k = A().ret_i(); }
301 { A k = A().ret_a(); }
302 { A k = to_lvalue_ref(A()); }// expected-error {{is not a constant expression}}
303 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
304 { A k = to_lvalue_ref(A().ret_a()); } // expected-error {{is not a constant expression}}
305 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
306 { int k = A().ret_a().ret_i(); }
307 { int k = by_value_a(A()); }
308 { int k = const_a_ref(A()); }
309 { int k = const_a_ref(a); }
310 { int k = rvalue_ref(A()); }
311 { int k = rvalue_ref(std::move(a)); }
312 { int k = const_a_ref(A().ret_a()); }
313 { int k = const_a_ref(to_lvalue_ref(A().ret_a())); }
314 { int k = const_a_ref(to_lvalue_ref(std::move(a))); }
315 { int k = by_value_a(A().ret_a()); }
316 { int k = by_value_a(to_lvalue_ref(std::move(a))); }
317 { int k = (A().ret_a(), A().ret_i()); }
318 { int k = (const_a_ref(A().ret_a()), A().ret_i()); }//
319 }
320
321 }
322
323 namespace alloc {
324
f()325 consteval int f() {
326 int *A = new int(0);
327 // expected-note@-1+ {{allocation performed here was not deallocated}}
328 return *A;
329 }
330
331 int i1 = f(); // expected-error {{is not a constant expression}}
332
333 struct A {
334 int* p = new int(42);
335 // expected-note@-1+ {{heap allocation performed here}}
ret_ialloc::A336 consteval int ret_i() const { return p ? *p : 0; }
ret_aalloc::A337 consteval A ret_a() const { return A{}; }
~Aalloc::A338 constexpr ~A() { delete p; }
339 };
340
by_value_a(A a)341 consteval int by_value_a(A a) { return a.ret_i(); }
342
const_a_ref(const A & a)343 consteval int const_a_ref(const A &a) {
344 return a.ret_i();
345 }
346
rvalue_ref(const A && a)347 consteval int rvalue_ref(const A &&a) {
348 return a.ret_i();
349 }
350
to_lvalue_ref(const A && a)351 consteval const A &to_lvalue_ref(const A &&a) {
352 return a;
353 }
354
test()355 void test() {
356 constexpr A a{ nullptr };
357 { int k = A().ret_i(); }
358 { A k = A().ret_a(); } // expected-error {{is not a constant expression}}
359 // expected-note@-1 {{is not a constant expression}}
360 { A k = to_lvalue_ref(A()); } // expected-error {{is not a constant expression}}
361 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
362 { A k = to_lvalue_ref(A().ret_a()); }
363 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
364 // expected-note@-2 {{heap-allocated object is not a constant expression}}
365 // expected-error@-3 {{'alloc::to_lvalue_ref' is not a constant expression}}
366 // expected-note@-4 {{reference to temporary is not a constant expression}}
367 // expected-note@-5 {{temporary created here}}
368 { int k = A().ret_a().ret_i(); }
369 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
370 // expected-note@-2 {{heap-allocated object is not a constant expression}}
371 { int k = by_value_a(A()); }
372 { int k = const_a_ref(A()); }
373 { int k = const_a_ref(a); }
374 { int k = rvalue_ref(A()); }
375 { int k = rvalue_ref(std::move(a)); }
376 { int k = const_a_ref(A().ret_a()); }
377 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
378 // expected-note@-2 {{is not a constant expression}}
379 { int k = const_a_ref(to_lvalue_ref(A().ret_a())); }
380 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
381 // expected-note@-2 {{is not a constant expression}}
382 { int k = const_a_ref(to_lvalue_ref(std::move(a))); }
383 { int k = by_value_a(A().ret_a()); }
384 { int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); }
385 { int k = (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}}
386 // expected-note@-1 {{is not a constant expression}}
387 { int k = (const_a_ref(A().ret_a()), A().ret_i()); }
388 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
389 // expected-note@-2 {{is not a constant expression}}
390 }
391
392 }
393
394 namespace self_referencing {
395
396 struct S {
397 S* ptr = nullptr;
Sself_referencing::S398 constexpr S(int i) : ptr(this) {
399 if (this == ptr && i)
400 ptr = nullptr;
401 }
~Sself_referencing::S402 constexpr ~S() {}
403 };
404
f(int i)405 consteval S f(int i) {
406 return S(i);
407 }
408
test()409 void test() {
410 S s(1);
411 s = f(1);
412 s = f(0); // expected-error {{is not a constant expression}}
413 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
414 }
415
416 struct S1 {
417 S1* ptr = nullptr;
S1self_referencing::S1418 consteval S1(int i) : ptr(this) {
419 if (this == ptr && i)
420 ptr = nullptr;
421 }
~S1self_referencing::S1422 constexpr ~S1() {}
423 };
424
test1()425 void test1() {
426 S1 s(1);
427 s = S1(1);
428 s = S1(0); // expected-error {{is not a constant expression}}
429 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
430 }
431
432 }
433 namespace ctor {
434
f_eval()435 consteval int f_eval() { // expected-note+ {{declared here}}
436 return 0;
437 }
438
439 namespace std {
440 struct strong_ordering {
441 int n;
442 static const strong_ordering less, equal, greater;
443 };
444 constexpr strong_ordering strong_ordering::less = {-1};
445 constexpr strong_ordering strong_ordering::equal = {0};
446 constexpr strong_ordering strong_ordering::greater = {1};
447 constexpr bool operator!=(strong_ordering, int);
448 }
449
450 namespace override {
451 struct A {
452 virtual consteval void f(); // expected-note {{overridden}}
453 virtual void g(); // expected-note {{overridden}}
454 };
455 struct B : A {
456 consteval void f();
457 void g();
458 };
459 struct C : A {
460 void f(); // expected-error {{non-consteval function 'f' cannot override a consteval function}}
461 consteval void g(); // expected-error {{consteval function 'g' cannot override a non-consteval function}}
462 };
463
464 namespace implicit_equals_1 {
465 struct Y;
466 struct X {
467 std::strong_ordering operator<=>(const X&) const;
468 constexpr bool operator==(const X&) const;
469 virtual consteval bool operator==(const Y&) const; // expected-note {{here}}
470 };
471 struct Y : X {
472 std::strong_ordering operator<=>(const Y&) const = default;
473 // expected-error@-1 {{non-consteval function 'operator==' cannot override a consteval function}}
474 };
475 }
476
477 namespace implicit_equals_2 {
478 struct Y;
479 struct X {
480 constexpr std::strong_ordering operator<=>(const X&) const;
481 constexpr bool operator==(const X&) const;
482 virtual bool operator==(const Y&) const; // expected-note {{here}}
483 };
484 struct Y : X {
485 consteval std::strong_ordering operator<=>(const Y&) const = default;
486 // expected-error@-1 {{consteval function 'operator==' cannot override a non-consteval function}}
487 };
488 }
489 }
490
491 namespace operator_rewrite {
492 struct A {
operator <=>(const A &,const A &)493 friend consteval int operator<=>(const A&, const A&) { return 0; }
494 };
495 const bool k = A() < A();
496 static_assert(!k);
497
498 A a;
499 bool k2 = A() < a; // OK, does not access 'a'.
500
501 struct B {
operator <=>(const B & l,const B & r)502 friend consteval int operator<=>(const B &l, const B &r) { return r.n - l.n; } // expected-note {{read of }}
503 int n;
504 };
505 static_assert(B() >= B());
506 B b; // expected-note {{here}}
507 bool k3 = B() < b; // expected-error-re {{call to consteval function '{{.*}}::operator<=>' is not a constant expression}} expected-note {{in call}}
508 }
509
510 struct A {
511 int(*ptr)();
Actor::A512 consteval A(int(*p)() = nullptr) : ptr(p) {}
513 };
514
515 struct B {
516 int(*ptr)();
Bctor::B517 B() : ptr(nullptr) {}
Bctor::B518 consteval B(int(*p)(), int) : ptr(p) {}
519 };
520
test()521 void test() {
522 { A a; }
523 { A a(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
524 { B b(nullptr, 0); }
525 { B b(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
526 { A a{}; }
527 { A a{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
528 { B b{nullptr, 0}; }
529 { B b{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
530 { A a = A(); }
531 { A a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
532 { B b = B(nullptr, 0); }
533 { B b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
534 { A a = A{}; }
535 { A a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
536 { B b = B{nullptr, 0}; }
537 { B b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
538 { A a; a = A(); }
539 { A a; a = A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
540 { B b; b = B(nullptr, 0); }
541 { B b; b = B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
542 { A a; a = A{}; }
543 { A a; a = A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
544 { B b; b = B{nullptr, 0}; }
545 { B b; b = B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
546 { A* a; a = new A(); }
547 { A* a; a = new A(&f_eval); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
548 { B* b; b = new B(nullptr, 0); }
549 { B* b; b = new B(&f_eval, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
550 { A* a; a = new A{}; }
551 { A* a; a = new A{&f_eval}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
552 { B* b; b = new B{nullptr, 0}; }
553 { B* b; b = new B{&f_eval, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
554 }
555
556 }
557
558 namespace copy_ctor {
559
f_eval()560 consteval int f_eval() { // expected-note+ {{declared here}}
561 return 0;
562 }
563
564 struct Copy {
565 int(*ptr)();
Copycopy_ctor::Copy566 constexpr Copy(int(*p)() = nullptr) : ptr(p) {}
567 consteval Copy(const Copy&) = default;
568 };
569
to_lvalue_ref(const Copy && a)570 constexpr const Copy &to_lvalue_ref(const Copy &&a) {
571 return a;
572 }
573
test()574 void test() {
575 constexpr const Copy C;
576 // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision.
577 // so we need to test with both prvalue and xvalues.
578 { Copy c(C); }
579 { Copy c((Copy(&f_eval))); }// expected-error {{cannot take address of consteval}}
580 { Copy c(std::move(C)); }
581 { Copy c(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
582 { Copy c(to_lvalue_ref((Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
583 { Copy c(to_lvalue_ref(std::move(C))); }
584 { Copy c(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
585 { Copy c = Copy(C); }
586 { Copy c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}}
587 { Copy c = Copy(std::move(C)); }
588 { Copy c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
589 { Copy c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
590 { Copy c = Copy(to_lvalue_ref(std::move(C))); }
591 { Copy c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
592 { Copy c; c = Copy(C); }
593 { Copy c; c = Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}}
594 { Copy c; c = Copy(std::move(C)); }
595 { Copy c; c = Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
596 { Copy c; c = Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
597 { Copy c; c = Copy(to_lvalue_ref(std::move(C))); }
598 { Copy c; c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
599 { Copy* c; c = new Copy(C); }
600 { Copy* c; c = new Copy(Copy(&f_eval)); }// expected-error {{cannot take address of consteval}}
601 { Copy* c; c = new Copy(std::move(C)); }
602 { Copy* c; c = new Copy(std::move(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
603 { Copy* c; c = new Copy(to_lvalue_ref(Copy(&f_eval))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
604 { Copy* c; c = new Copy(to_lvalue_ref(std::move(C))); }
605 { Copy* c; c = new Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
606 }
607
608 } // namespace special_ctor
609
610 namespace unevaluated {
611
612 template <typename T, typename U> struct is_same { static const bool value = false; };
613 template <typename T> struct is_same<T, T> { static const bool value = true; };
614
615 long f(); // expected-note {{declared here}}
g(auto a)616 auto consteval g(auto a) {
617 return a;
618 }
619
620 auto e = g(f()); // expected-error {{is not a constant expression}}
621 // expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}}
622
623 using T = decltype(g(f()));
624 static_assert(is_same<long, T>::value);
625
626 } // namespace unevaluated
627
628 namespace value_dependent {
629
foo(int x)630 consteval int foo(int x) {
631 return x;
632 }
633
bar()634 template <int X> constexpr int bar() {
635 // Previously this call was rejected as value-dependent constant expressions
636 // can't be immediately evaluated. Now we show that we don't immediately
637 // evaluate them until they are instantiated.
638 return foo(X);
639 }
640
baz()641 template <typename T> constexpr int baz() {
642 constexpr int t = sizeof(T);
643 // Previously this call was rejected as `t` is value-dependent and its value
644 // is unknown until the function is instantiated. Now we show that we don't
645 // reject such calls.
646 return foo(t);
647 }
648
649 static_assert(bar<15>() == 15);
650 static_assert(baz<int>() == sizeof(int));
651
652 } // namespace value_dependent
653
654 namespace default_argument {
655
656 // Previously calls of consteval functions in default arguments were rejected.
657 // Now we show that we don't reject such calls.
foo()658 consteval int foo() { return 1; }
bar(int i=foo ())659 consteval int bar(int i = foo()) { return i * i; }
660
661 struct Test1 {
Test1default_argument::Test1662 Test1(int i = bar(13)) {}
vdefault_argument::Test1663 void v(int i = bar(13) * 2 + bar(15)) {}
664 };
665 Test1 t1;
666
667 struct Test2 {
Test2default_argument::Test2668 constexpr Test2(int i = bar()) {}
vdefault_argument::Test2669 constexpr void v(int i = bar(bar(bar(foo())))) {}
670 };
671 Test2 t2;
672
673 } // namespace default_argument
674
675 namespace PR50779 {
676 struct derp {
677 int b = 0;
678 };
679
680 constexpr derp d;
681
682 struct test {
operator []PR50779::test683 consteval int operator[](int i) const { return {}; }
operator ->PR50779::test684 consteval const derp * operator->() const { return &d; }
fPR50779::test685 consteval int f() const { return 12; } // expected-note 2{{declared here}}
686 };
687
688 constexpr test a;
689
690 // We previously rejected both of these overloaded operators as taking the
691 // address of a consteval function outside of an immediate context, but we
692 // accepted direct calls to the overloaded operator. Now we show that we accept
693 // both forms.
694 constexpr int s = a.operator[](1);
695 constexpr int t = a[1];
696 constexpr int u = a.operator->()->b;
697 constexpr int v = a->b;
698 // FIXME: I believe this case should work, but we currently reject.
699 constexpr int w = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
700 constexpr int x = a.f();
701
702 // Show that we reject when not in an immediate context.
703 int w2 = (a.*&test::f)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
704 }
705
706 namespace PR48235 {
d()707 consteval int d() {
708 return 1;
709 }
710
711 struct A {
aPR48235::A712 consteval int a() const { return 1; }
713
bPR48235::A714 void b() {
715 this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
716 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
717 }
718
cPR48235::A719 void c() {
720 a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
721 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
722 }
723 };
724 } // PR48235
725
726 namespace NamespaceScopeConsteval {
727 struct S {
728 int Val; // expected-note {{subobject declared here}}
SNamespaceScopeConsteval::S729 consteval S() {}
730 };
731
732 S s1; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \
733 expected-note {{subobject of type 'int' is not initialized}}
734
735 template <typename Ty>
736 struct T {
737 Ty Val; // expected-note {{subobject declared here}}
TNamespaceScopeConsteval::T738 consteval T() {}
739 };
740
741 T<int> t; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \
742 expected-note {{subobject of type 'int' is not initialized}}
743
744 } // namespace NamespaceScopeConsteval
745
746 namespace Issue54578 {
747 // We expect the user-defined literal to be resovled entirely at compile time
748 // despite being instantiated through a template.
operator ""_UC(const unsigned long long n)749 inline consteval unsigned char operator""_UC(const unsigned long long n) {
750 return static_cast<unsigned char>(n);
751 }
752
f1(const auto octet)753 inline constexpr char f1(const auto octet) {
754 return 4_UC;
755 }
756
757 template <typename Ty>
f2(const Ty octet)758 inline constexpr char f2(const Ty octet) {
759 return 4_UC;
760 }
761
test()762 void test() {
763 static_assert(f1('a') == 4);
764 static_assert(f2('a') == 4);
765 constexpr int c = f1('a') + f2('a');
766 static_assert(c == 8);
767 }
768 }
769
770 // https://github.com/llvm/llvm-project/issues/51695
771 namespace GH51695 {
772 // Original ========================================
773 template <typename T>
774 struct type_t {};
775
776 template <typename...>
777 struct list_t {};
778
779 template <typename T, typename... Ts>
pop_front(list_t<T,Ts...>)780 consteval auto pop_front(list_t<T, Ts...>) -> auto {
781 return list_t<Ts...>{};
782 }
783
784 template <typename... Ts, typename F>
apply(list_t<Ts...>,F fn)785 consteval auto apply(list_t<Ts...>, F fn) -> auto {
786 return fn(type_t<Ts>{}...);
787 }
788
test1()789 void test1() {
790 constexpr auto x = apply(pop_front(list_t<char, char>{}),
791 []<typename... Us>(type_t<Us>...) { return 42; });
792 static_assert(x == 42);
793 }
794 // Reduced 1 ========================================
zero()795 consteval bool zero() { return false; }
796
797 template <typename F>
foo(bool,F f)798 consteval bool foo(bool, F f) {
799 return f();
800 }
801
test2()802 void test2() {
803 constexpr auto x = foo(zero(), []() { return true; });
804 static_assert(x);
805 }
806
807 // Reduced 2 ========================================
808 template <typename F>
bar(F f)809 consteval auto bar(F f) { return f;}
810
test3()811 void test3() {
812 constexpr auto t1 = bar(bar(bar(bar([]() { return true; }))))();
813 static_assert(t1);
814
815 int a = 1; // expected-note {{declared here}}
816 auto t2 = bar(bar(bar(bar([=]() { return a; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}}
817 // expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}}
818
819 constexpr auto t3 = bar(bar([x=bar(42)]() { return x; }))();
820 static_assert(t3==42);
821 constexpr auto t4 = bar(bar([x=bar(42)]() consteval { return x; }))();
822 static_assert(t4==42);
823 }
824
825 } // namespace GH51695
826
827 // https://github.com/llvm/llvm-project/issues/50455
828 namespace GH50455 {
f()829 void f() {
830 []() consteval { int i{}; }();
831 []() consteval { int i{}; ++i; }();
832 }
g()833 void g() {
834 (void)[](int i) consteval { return i; }(0);
835 (void)[](int i) consteval { return i; }(0);
836 }
837 } // namespace GH50455
838