Debian Codegraph @

clang/test/AST/ByteCode/records.cpp

raw
1 // RUN: %clang_cc1 -std=c++14 -verify=expected,both %s -fexperimental-new-constant-interpreter
2 // RUN: %clang_cc1 -std=c++17 -verify=expected,both %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -std=c++17 -verify=expected,both -triple i686 %s -fexperimental-new-constant-interpreter
4 // RUN: %clang_cc1 -std=c++20 -verify=expected,both %s -fexperimental-new-constant-interpreter
5 // RUN: %clang_cc1 -std=c++14 -verify=ref,both %s
6 // RUN: %clang_cc1 -std=c++17 -verify=ref,both %s
7 // RUN: %clang_cc1 -std=c++17 -verify=ref,both -triple i686 %s
8 // RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
9
10 /// Used to crash.
11 struct Empty {};
12 constexpr Empty e = {Empty()};
13
14 struct BoolPair {
15 bool first;
16 bool second;
17 };
18
19 struct Ints {
20 int a = 20;
21 int b = 30;
22 bool c = true;
23 BoolPair bp = {true, false};
24 int numbers[3] = {1,2,3};
25
26 static const int five = 5;
27 static constexpr int getFive() {
28 return five;
29 }
30
31 constexpr int getTen() const {
32 return 10;
33 }
34 };
35
36 static_assert(Ints::getFive() == 5, "");
37
38 constexpr Ints ints;
39 static_assert(ints.a == 20, "");
40 static_assert(ints.b == 30, "");
41 static_assert(ints.c, "");
42 static_assert(ints.getTen() == 10, "");
43 static_assert(ints.numbers[0] == 1, "");
44 static_assert(ints.numbers[1] == 2, "");
45 static_assert(ints.numbers[2] == 3, "");
46
47 constexpr const BoolPair &BP = ints.bp;
48 static_assert(BP.first, "");
49 static_assert(!BP.second, "");
50 static_assert(ints.bp.first, "");
51 static_assert(!ints.bp.second, "");
52
53
54 constexpr Ints ints2{-20, -30, false};
55 static_assert(ints2.a == -20, "");
56 static_assert(ints2.b == -30, "");
57 static_assert(!ints2.c, "");
58
59 constexpr Ints getInts() {
60 return {64, 128, true};
61 }
62 constexpr Ints ints3 = getInts();
63 static_assert(ints3.a == 64, "");
64 static_assert(ints3.b == 128, "");
65 static_assert(ints3.c, "");
66
67 constexpr Ints ints4 = {
68 .a = 40 * 50,
69 .b = 0,
70 .c = (ints.a > 0),
71
72 };
73 static_assert(ints4.a == (40 * 50), "");
74 static_assert(ints4.b == 0, "");
75 static_assert(ints4.c, "");
76 static_assert(ints4.numbers[0] == 1, "");
77 static_assert(ints4.numbers[1] == 2, "");
78 static_assert(ints4.numbers[2] == 3, "");
79
80 constexpr Ints ints5 = ints4;
81 static_assert(ints5.a == (40 * 50), "");
82 static_assert(ints5.b == 0, "");
83 static_assert(ints5.c, "");
84 static_assert(ints5.numbers[0] == 1, "");
85 static_assert(ints5.numbers[1] == 2, "");
86 static_assert(ints5.numbers[2] == 3, "");
87
88
89 struct Ints2 {
90 int a = 10;
91 int b;
92 };
93 constexpr Ints2 ints22; // both-error {{without a user-provided default constructor}}
94
95 constexpr Ints2 I2 = Ints2{12, 25};
96 static_assert(I2.a == 12, "");
97 static_assert(I2.b == 25, "");
98
99 class C {
100 public:
101 int a;
102 int b;
103
104 constexpr C() : a(100), b(200) {}
105
106 constexpr C get() const {
107 return *this;
108 }
109 };
110
111 constexpr C c;
112 static_assert(c.a == 100, "");
113 static_assert(c.b == 200, "");
114
115 constexpr C c2 = C().get();
116 static_assert(c2.a == 100, "");
117 static_assert(c2.b == 200, "");
118
119
120 /// A global, composite temporary variable.
121 constexpr const C &c3 = C().get();
122
123 /// Same, but with a bitfield.
124 class D {
125 public:
126 unsigned a : 4;
127 constexpr D() : a(15) {}
128 constexpr D get() const {
129 return *this;
130 }
131 };
132 constexpr const D &d4 = D().get();
133
134 constexpr int getB() {
135 C c;
136 int &j = c.b;
137
138 j = j * 2;
139
140 return c.b;
141 }
142 static_assert(getB() == 400, "");
143
144 constexpr int getA(const C &c) {
145 return c.a;
146 }
147 static_assert(getA(c) == 100, "");
148
149 constexpr const C* getPointer() {
150 return &c;
151 }
152 static_assert(getPointer()->a == 100, "");
153
154 constexpr C RVOAndParams(const C *c) {
155 return C();
156 }
157 constexpr C RVOAndParamsResult = RVOAndParams(&c);
158
159 /// Parameter and return value have different types.
160 constexpr C RVOAndParams(int a) {
161 return C();
162 }
163 constexpr C RVOAndParamsResult2 = RVOAndParams(12);
164
165 class Bar { // both-note {{definition of 'Bar' is not complete}}
166 public:
167 constexpr Bar(){}
168 constexpr Bar b; // both-error {{cannot be constexpr}} \
169 // both-error {{has incomplete type 'const Bar'}}
170 };
171 constexpr Bar B; // both-error {{must be initialized by a constant expression}}
172 constexpr Bar *pb = nullptr;
173
174 constexpr int locals() {
175 C c;
176 c.a = 10;
177
178 // Assignment, not an initializer.
179 c = C();
180 c.a = 10;
181
182
183 // Assignment, not an initializer.
184 c = RVOAndParams(&c);
185
186 return c.a;
187 }
188 static_assert(locals() == 100, "");
189
190 namespace thisPointer {
191 struct S {
192 constexpr int get12() { return 12; }
193 };
194
195 constexpr int foo() { // both-error {{never produces a constant expression}}
196 S *s = nullptr;
197 return s->get12(); // both-note 2{{member call on dereferenced null pointer}}
198
199 }
200 static_assert(foo() == 12, ""); // both-error {{not an integral constant expression}} \
201 // both-note {{in call to 'foo()'}}
202 };
203
204 struct FourBoolPairs {
205 BoolPair v[4] = {
206 {false, false},
207 {false, true},
208 {true, false},
209 {true, true },
210 };
211 };
212 // Init
213 constexpr FourBoolPairs LT;
214 // Copy ctor
215 constexpr FourBoolPairs LT2 = LT;
216 static_assert(LT2.v[0].first == false, "");
217 static_assert(LT2.v[0].second == false, "");
218 static_assert(LT2.v[2].first == true, "");
219 static_assert(LT2.v[2].second == false, "");
220
221 class Base {
222 public:
223 int i;
224 constexpr Base() : i(10) {}
225 constexpr Base(int i) : i(i) {}
226 };
227
228 class A : public Base {
229 public:
230 constexpr A() : Base(100) {}
231 constexpr A(int a) : Base(a) {}
232 };
233 constexpr A a{};
234 static_assert(a.i == 100, "");
235 constexpr A a2{12};
236 static_assert(a2.i == 12, "");
237 static_assert(a2.i == 200, ""); // both-error {{static assertion failed}} \
238 // both-note {{evaluates to '12 == 200'}}
239
240
241 struct S {
242 int a = 0;
243 constexpr int get5() const { return 5; }
244 constexpr void fo() const {
245 this; // both-warning {{expression result unused}}
246 this->a; // both-warning {{expression result unused}}
247 get5();
248 getInts();
249 }
250
251 constexpr int m() const {
252 fo();
253 return 1;
254 }
255 };
256 constexpr S s;
257 static_assert(s.m() == 1, "");
258
259 namespace InitializerTemporaries {
260 class Bar {
261 private:
262 int a;
263
264 public:
265 constexpr Bar() : a(10) {}
266 constexpr int getA() const { return a; }
267 };
268
269 class Foo {
270 public:
271 int a;
272
273 constexpr Foo() : a(Bar().getA()) {}
274 };
275 constexpr Foo F;
276 static_assert(F.a == 10, "");
277
278
279 /// Needs constexpr destructors.
280 #if __cplusplus >= 202002L
281 /// Does
282 /// Arr[Pos] = Value;
283 /// ++Pos;
284 /// in its destructor.
285 class BitSetter {
286 private:
287 int *Arr;
288 int &Pos;
289 int Value;
290
291 public:
292 constexpr BitSetter(int *Arr, int &Pos, int Value) :
293 Arr(Arr), Pos(Pos), Value(Value) {}
294
295 constexpr int getValue() const { return 0; }
296 constexpr ~BitSetter() {
297 Arr[Pos] = Value;
298 ++Pos;
299 }
300 };
301
302 class Test {
303 int a, b, c;
304 public:
305 constexpr Test(int *Arr, int &Pos) :
306 a(BitSetter(Arr, Pos, 1).getValue()),
307 b(BitSetter(Arr, Pos, 2).getValue()),
308 c(BitSetter(Arr, Pos, 3).getValue())
309 {}
310 };
311
312
313 constexpr int T(int Index) {
314 int Arr[] = {0, 0, 0};
315 int Pos = 0;
316
317 {
318 Test(Arr, Pos);
319 // End of scope, should destroy Test.
320 }
321
322 return Arr[Index];
323 }
324 static_assert(T(0) == 1);
325 static_assert(T(1) == 2);
326 static_assert(T(2) == 3);
327
328 // Invalid destructor.
329 struct S {
330 constexpr S() {}
331 constexpr ~S() noexcept(false) { throw 12; } // both-error {{cannot use 'throw'}} \
332 // both-error {{never produces a constant expression}} \
333 // both-note 2{{subexpression not valid}}
334 };
335
336 constexpr int f() {
337 S{}; // both-note {{in call to 'S{}.~S()'}}
338 return 12;
339 }
340 static_assert(f() == 12); // both-error {{not an integral constant expression}} \
341 // both-note {{in call to 'f()'}}
342
343
344 #endif
345 }
346
347 #if __cplusplus >= 201703L
348 namespace BaseInit {
349 class _A {public: int a;};
350 class _B : public _A {};
351 class _C : public _B {};
352
353 constexpr _C c{12};
354 constexpr const _B &b = c;
355 static_assert(b.a == 12);
356
357 class A {public: int a;};
358 class B : public A {};
359 class C : public A {};
360 class D : public B, public C {};
361
362 // This initializes D::B::A::a and not D::C::A::a.
363 constexpr D d{12};
364 static_assert(d.B::a == 12);
365 static_assert(d.C::a == 0);
366 };
367 #endif
368
369 namespace MI {
370 class A {
371 public:
372 int a;
373 constexpr A(int a) : a(a) {}
374 };
375
376 class B {
377 public:
378 int b;
379 constexpr B(int b) : b(b) {}
380 };
381
382 class C : public A, public B {
383 public:
384 constexpr C() : A(10), B(20) {}
385 };
386 constexpr C c = {};
387 static_assert(c.a == 10, "");
388 static_assert(c.b == 20, "");
389
390 constexpr const A *aPointer = &c;
391 constexpr const B *bPointer = &c;
392
393 class D : private A, private B {
394 public:
395 constexpr D() : A(20), B(30) {}
396 constexpr int getA() const { return a; }
397 constexpr int getB() const { return b; }
398 };
399 constexpr D d = {};
400 static_assert(d.getA() == 20, "");
401 static_assert(d.getB() == 30, "");
402 };
403
404 namespace DeriveFailures {
405 #if __cplusplus < 202002L
406 struct Base { // both-note {{declared here}} \
407 // ref-note {{declared here}}
408 int Val;
409 };
410
411 struct Derived : Base {
412 int OtherVal;
413
414 constexpr Derived(int i) : OtherVal(i) {} // ref-error {{never produces a constant expression}} \
415 // both-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}} \
416 // ref-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}}
417 };
418
419 constexpr Derived D(12); // both-error {{must be initialized by a constant expression}} \
420 // both-note {{in call to 'Derived(12)'}} \
421 // both-note {{declared here}}
422
423 static_assert(D.Val == 0, ""); // both-error {{not an integral constant expression}} \
424 // both-note {{initializer of 'D' is not a constant expression}}
425 #endif
426
427 struct AnotherBase {
428 int Val;
429 constexpr AnotherBase(int i) : Val(12 / i) {} // both-note {{division by zero}}
430 };
431
432 struct AnotherDerived : AnotherBase {
433 constexpr AnotherDerived(int i) : AnotherBase(i) {}
434 };
435 constexpr AnotherBase Derp(0); // both-error {{must be initialized by a constant expression}} \
436 // both-note {{in call to 'AnotherBase(0)'}}
437
438 struct YetAnotherBase {
439 int Val;
440 constexpr YetAnotherBase(int i) : Val(i) {}
441 };
442
443 struct YetAnotherDerived : YetAnotherBase {
444 using YetAnotherBase::YetAnotherBase; // both-note {{declared here}}
445 int OtherVal;
446
447 constexpr bool doit() const { return Val == OtherVal; }
448 };
449
450 constexpr YetAnotherDerived Oops(0); // both-error {{must be initialized by a constant expression}} \
451 // both-note {{constructor inherited from base class 'YetAnotherBase' cannot be used in a constant expression}}
452 };
453
454 namespace EmptyCtor {
455 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
456 constexpr piecewise_construct_t piecewise_construct =
457 piecewise_construct_t();
458 };
459
460 namespace ConditionalInit {
461 struct S { int a; };
462
463 constexpr S getS(bool b) {
464 return b ? S{12} : S{13};
465 }
466
467 static_assert(getS(true).a == 12, "");
468 static_assert(getS(false).a == 13, "");
469 };
470 namespace DeclRefs {
471 struct A{ int m; const int &f = m; };
472
473 constexpr A a{10};
474 static_assert(a.m == 10, "");
475 static_assert(a.f == 10, "");
476
477 class Foo {
478 public:
479 int z = 1337;
480 constexpr int a() const {
481 A b{this->z};
482
483 return b.f;
484 }
485 };
486 constexpr Foo f;
487 static_assert(f.a() == 1337, "");
488
489
490 struct B {
491 A a = A{100};
492 };
493 constexpr B b;
494 static_assert(b.a.m == 100, "");
495 static_assert(b.a.f == 100, "");
496
497 constexpr B b2{};
498 static_assert(b2.a.m == 100, "");
499 static_assert(b2.a.f == 100, "");
500 static_assert(b2.a.f == 101, ""); // both-error {{failed}} \
501 // both-note {{evaluates to '100 == 101'}}
502 }
503
504 namespace PointerArith {
505 struct A {};
506 struct B : A { int n; };
507
508 B b = {};
509 constexpr A *a1 = &b;
510 constexpr B *b1 = &b + 1;
511 constexpr B *b2 = &b + 0;
512
513 constexpr A *a2 = &b + 1; // both-error {{must be initialized by a constant expression}} \
514 // both-note {{cannot access base class of pointer past the end of object}}
515 constexpr const int *pn = &(&b + 1)->n; // both-error {{must be initialized by a constant expression}} \
516 // both-note {{cannot access field of pointer past the end of object}}
517 }
518
519 #if __cplusplus >= 202002L
520 namespace VirtualCalls {
521 namespace Obvious {
522
523 class A {
524 public:
525 constexpr A(){}
526 constexpr virtual int foo() {
527 return 3;
528 }
529 };
530 class B : public A {
531 public:
532 constexpr int foo() override {
533 return 6;
534 }
535 };
536
537 constexpr int getFooB(bool b) {
538 A *a;
539 A myA;
540 B myB;
541
542 if (b)
543 a = &myA;
544 else
545 a = &myB;
546
547 return a->foo();
548 }
549 static_assert(getFooB(true) == 3, "");
550 static_assert(getFooB(false) == 6, "");
551 }
552
553 namespace MultipleBases {
554 class A {
555 public:
556 constexpr virtual int getInt() const { return 10; }
557 };
558 class B {
559 public:
560 };
561 class C : public A, public B {
562 public:
563 constexpr int getInt() const override { return 20; }
564 };
565
566 constexpr int callGetInt(const A& a) { return a.getInt(); }
567 static_assert(callGetInt(C()) == 20, "");
568 static_assert(callGetInt(A()) == 10, "");
569 }
570
571 namespace Destructors {
572 class Base {
573 public:
574 int i;
575 constexpr Base(int &i) : i(i) {i++;}
576 constexpr virtual ~Base() {i--;}
577 };
578
579 class Derived : public Base {
580 public:
581 constexpr Derived(int &i) : Base(i) {}
582 constexpr virtual ~Derived() {i--;}
583 };
584
585 constexpr int test() {
586 int i = 0;
587 Derived d(i);
588 return i;
589 }
590 static_assert(test() == 1);
591
592 struct S {
593 constexpr S() {}
594 constexpr ~S() { // both-error {{never produces a constant expression}}
595 int i = 1 / 0; // both-warning {{division by zero}} \
596 // both-note 2{{division by zero}}
597 }
598 };
599 constexpr int testS() {
600 S{}; // both-note {{in call to 'S{}.~S()'}}
601 return 1;
602 }
603 static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
604 // both-note {{in call to 'testS()'}}
605 }
606
607 namespace BaseToDerived {
608 namespace A {
609 struct A {};
610 struct B : A { int n; };
611 struct C : B {};
612 C c = {};
613 constexpr C *pb = (C*)((A*)&c + 1); // both-error {{must be initialized by a constant expression}} \
614 // both-note {{cannot access derived class of pointer past the end of object}}
615 }
616 namespace B {
617 struct A {};
618 struct Z {};
619 struct B : Z, A {
620 int n;
621 constexpr B() : n(10) {}
622 };
623 struct C : B {
624 constexpr C() : B() {}
625 };
626
627 constexpr C c = {};
628 constexpr const A *pa = &c;
629 constexpr const C *cp = (C*)pa;
630 constexpr const B *cb = (B*)cp;
631
632 static_assert(cb->n == 10);
633 static_assert(cp->n == 10);
634 }
635
636 namespace C {
637 struct Base { int *a; };
638 struct Base2 : Base { int f[12]; };
639
640 struct Middle1 { int b[3]; };
641 struct Middle2 : Base2 { char c; };
642 struct Middle3 : Middle2 { char g[3]; };
643 struct Middle4 { int f[3]; };
644 struct Middle5 : Middle4, Middle3 { char g2[3]; };
645
646 struct NotQuiteDerived : Middle1, Middle5 { bool d; };
647 struct Derived : NotQuiteDerived { int e; };
648
649 constexpr NotQuiteDerived NQD1 = {};
650
651 constexpr Middle5 *M4 = (Middle5*)((Base2*)&NQD1);
652 static_assert(M4->a == nullptr);
653 static_assert(M4->g2[0] == 0);
654 }
655 }
656
657
658 namespace VirtualDtors {
659 class A {
660 public:
661 unsigned &v;
662 constexpr A(unsigned &v) : v(v) {}
663 constexpr virtual ~A() {
664 v |= (1 << 0);
665 }
666 };
667 class B : public A {
668 public:
669 constexpr B(unsigned &v) : A(v) {}
670 constexpr virtual ~B() {
671 v |= (1 << 1);
672 }
673 };
674 class C : public B {
675 public:
676 constexpr C(unsigned &v) : B(v) {}
677 constexpr virtual ~C() {
678 v |= (1 << 2);
679 }
680 };
681
682 constexpr bool foo() {
683 unsigned a = 0;
684 {
685 C c(a);
686 }
687 return ((a & (1 << 0)) && (a & (1 << 1)) && (a & (1 << 2)));
688 }
689
690 static_assert(foo());
691 };
692
693 namespace QualifiedCalls {
694 class A {
695 public:
696 constexpr virtual int foo() const {
697 return 5;
698 }
699 };
700 class B : public A {};
701 class C : public B {
702 public:
703 constexpr int foo() const override {
704 return B::foo(); // B doesn't have a foo(), so this should call A::foo().
705 }
706 constexpr int foo2() const {
707 return this->A::foo();
708 }
709 };
710 constexpr C c;
711 static_assert(c.foo() == 5);
712 static_assert(c.foo2() == 5);
713
714
715 struct S {
716 int _c = 0;
717 virtual constexpr int foo() const { return 1; }
718 };
719
720 struct SS : S {
721 int a;
722 constexpr SS() {
723 a = S::foo();
724 }
725 constexpr int foo() const override {
726 return S::foo();
727 }
728 };
729
730 constexpr SS ss;
731 static_assert(ss.a == 1);
732 }
733
734 namespace CtorDtor {
735 struct Base {
736 int i = 0;
737 int j = 0;
738
739 constexpr Base() : i(func()) {
740 j = func();
741 }
742 constexpr Base(int i) : i(i), j(i) {}
743
744 constexpr virtual int func() const { return 1; }
745 };
746
747 struct Derived : Base {
748 constexpr Derived() {}
749 constexpr Derived(int i) : Base(i) {}
750 constexpr int func() const override { return 2; }
751 };
752
753 struct Derived2 : Derived {
754 constexpr Derived2() : Derived(func()) {} // ref-note {{subexpression not valid in a constant expression}}
755 constexpr int func() const override { return 3; }
756 };
757
758 constexpr Base B;
759 static_assert(B.i == 1 && B.j == 1, "");
760
761 constexpr Derived D;
762 static_assert(D.i == 1, "");
763 static_assert(D.j == 1, "");
764
765 constexpr Derived2 D2; // ref-error {{must be initialized by a constant expression}} \
766 // ref-note {{in call to 'Derived2()'}} \
767 // ref-note 2{{declared here}}
768 static_assert(D2.i == 3, ""); // ref-error {{not an integral constant expression}} \
769 // ref-note {{initializer of 'D2' is not a constant expression}}
770 static_assert(D2.j == 3, ""); // ref-error {{not an integral constant expression}} \
771 // ref-note {{initializer of 'D2' is not a constant expression}}
772
773 }
774
775 namespace VirtualFunctionPointers {
776 struct S {
777 virtual constexpr int func() const { return 1; }
778 };
779
780 struct Middle : S {
781 constexpr Middle(int i) : i(i) {}
782 int i;
783 };
784
785 struct Other {
786 constexpr Other(int k) : k(k) {}
787 int k;
788 };
789
790 struct S2 : Middle, Other {
791 int j;
792 constexpr S2(int i, int j, int k) : Middle(i), Other(k), j(j) {}
793 virtual constexpr int func() const { return i + j + k + S::func(); }
794 };
795
796 constexpr S s;
797 constexpr decltype(&S::func) foo = &S::func;
798 constexpr int value = (s.*foo)();
799 static_assert(value == 1);
800
801
802 constexpr S2 s2(1, 2, 3);
803 static_assert(s2.i == 1);
804 static_assert(s2.j == 2);
805 static_assert(s2.k == 3);
806
807 constexpr int value2 = s2.func();
808 constexpr int value3 = (s2.*foo)();
809 static_assert(value3 == 7);
810
811 constexpr int dynamicDispatch(const S &s) {
812 constexpr decltype(&S::func) SFunc = &S::func;
813
814 return (s.*SFunc)();
815 }
816
817 static_assert(dynamicDispatch(s) == 1);
818 static_assert(dynamicDispatch(s2) == 7);
819 };
820
821 };
822 #endif
823
824 #if __cplusplus < 202002L
825 namespace VirtualFromBase {
826 struct S1 {
827 virtual int f() const;
828 };
829 struct S2 {
830 virtual int f();
831 };
832 template <typename T> struct X : T {
833 constexpr X() {}
834 double d = 0.0;
835 constexpr int f() { return sizeof(T); }
836 };
837
838 // Non-virtual f(), OK.
839 constexpr X<X<S1>> xxs1;
840 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
841 static_assert(p->f() == sizeof(S1), "");
842
843 // Virtual f(), not OK.
844 constexpr X<X<S2>> xxs2;
845 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
846 static_assert(q->f() == sizeof(X<S2>), ""); // both-error {{not an integral constant expression}} \
847 // both-note {{cannot evaluate call to virtual function}}
848 }
849 #endif
850
851 namespace CompositeDefaultArgs {
852 struct Foo {
853 int a;
854 int b;
855 constexpr Foo() : a(12), b(13) {}
856 };
857
858 class Bar {
859 public:
860 bool B = false;
861
862 constexpr int someFunc(Foo F = Foo()) {
863 this->B = true;
864 return 5;
865 }
866 };
867
868 constexpr bool testMe() {
869 Bar B;
870 B.someFunc();
871 return B.B;
872 }
873 static_assert(testMe(), "");
874 }
875
876 constexpr bool BPand(BoolPair bp) {
877 return bp.first && bp.second;
878 }
879 static_assert(BPand(BoolPair{true, false}) == false, "");
880
881 namespace TemporaryObjectExpr {
882 struct F {
883 int a;
884 constexpr F() : a(12) {}
885 };
886 constexpr int foo(F f) {
887 return 0;
888 }
889 static_assert(foo(F()) == 0, "");
890 }
891
892 namespace ZeroInit {
893 struct F {
894 int a;
895 };
896
897 namespace Simple {
898 struct A {
899 char a;
900 bool b;
901 int c[4];
902 float d;
903 };
904 constexpr int foo(A x) {
905 return x.a + static_cast<int>(x.b) + x.c[0] + x.c[3] + static_cast<int>(x.d);
906 }
907 static_assert(foo(A()) == 0, "");
908 }
909
910 namespace Inheritance {
911 struct F2 : F {
912 float f;
913 };
914
915 constexpr int foo(F2 f) {
916 return (int)f.f + f.a;
917 }
918 static_assert(foo(F2()) == 0, "");
919 }
920
921 namespace BitFields {
922 struct F {
923 unsigned a : 6;
924 };
925 constexpr int foo(F f) {
926 return f.a;
927 }
928 static_assert(foo(F()) == 0, "");
929 }
930
931 namespace Nested {
932 struct F2 {
933 float f;
934 char c;
935 };
936
937 struct F {
938 F2 f2;
939 int i;
940 };
941
942 constexpr int foo(F f) {
943 return f.i + f.f2.f + f.f2.c;
944 }
945 static_assert(foo(F()) == 0, "");
946 }
947
948 namespace CompositeArrays {
949 struct F2 {
950 float f;
951 char c;
952 };
953
954 struct F {
955 F2 f2[2];
956 int i;
957 };
958
959 constexpr int foo(F f) {
960 return f.i + f.f2[0].f + f.f2[0].c + f.f2[1].f + f.f2[1].c;
961 }
962 static_assert(foo(F()) == 0, "");
963 }
964
965 #if __cplusplus > 201402L
966 namespace Unions {
967 struct F {
968 union {
969 int a;
970 char c[4];
971 float f;
972 } U;
973 int i;
974 };
975
976 constexpr int foo(F f) {
977 return f.i + f.U.f; // both-note {{read of member 'f' of union with active member 'a'}}
978 }
979 static_assert(foo(F()) == 0, ""); // both-error {{not an integral constant expression}} \
980 // both-note {{in call to}}
981 }
982 #endif
983
984 #if __cplusplus >= 202002L
985 namespace Failure {
986 struct S {
987 int a;
988 F f{12};
989 };
990 constexpr int foo(S x) {
991 return x.a;
992 }
993 static_assert(foo(S()) == 0, "");
994 };
995 #endif
996 }
997
998 #if __cplusplus >= 202002L
999 namespace ParenInit {
1000 struct A {
1001 int a;
1002 };
1003
1004 struct B : A {
1005 int b;
1006 };
1007
1008 constexpr B b(A(1),2);
1009
1010
1011 struct O {
1012 int &&j;
1013 };
1014
1015 /// Not constexpr!
1016 O o1(0); // both-warning {{temporary whose address is used as value of}}
1017 // FIXME: the secondary warning message is bogus, would be nice to suppress it.
1018 constinit O o2(0); // both-error {{variable does not have a constant initializer}} \
1019 // both-note {{required by 'constinit' specifier}} \
1020 // both-note {{reference to temporary is not a constant expression}} \
1021 // both-note {{temporary created here}} \
1022 // both-warning {{temporary whose address is used as value}}
1023
1024
1025 /// Initializing an array.
1026 constexpr void bar(int i, int j) {
1027 int arr[4](i, j);
1028 }
1029 }
1030 #endif
1031
1032 namespace DelegatingConstructors {
1033 struct S {
1034 int a;
1035 constexpr S() : S(10) {}
1036 constexpr S(int a) : a(a) {}
1037 };
1038 constexpr S s = {};
1039 static_assert(s.a == 10, "");
1040
1041 struct B {
1042 int a;
1043 int b;
1044
1045 constexpr B(int a) : a(a), b(a + 2) {}
1046 };
1047 struct A : B {
1048 constexpr A() : B(10) {};
1049 };
1050 constexpr A d4 = {};
1051 static_assert(d4.a == 10, "");
1052 static_assert(d4.b == 12, "");
1053 }
1054
1055 namespace AccessOnNullptr {
1056 struct F {
1057 int a;
1058 };
1059
1060 constexpr int a() { // both-error {{never produces a constant expression}}
1061 F *f = nullptr;
1062
1063 f->a = 0; // both-note 2{{cannot access field of null pointer}}
1064 return f->a;
1065 }
1066 static_assert(a() == 0, ""); // both-error {{not an integral constant expression}} \
1067 // both-note {{in call to 'a()'}}
1068
1069 constexpr int a2() { // both-error {{never produces a constant expression}}
1070 F *f = nullptr;
1071
1072
1073 const int *a = &(f->a); // both-note 2{{cannot access field of null pointer}}
1074 return f->a;
1075 }
1076 static_assert(a2() == 0, ""); // both-error {{not an integral constant expression}} \
1077 // both-note {{in call to 'a2()'}}
1078 }
1079
1080 namespace IndirectFieldInit {
1081 #if __cplusplus >= 202002L
1082 /// Primitive.
1083 struct Nested1 {
1084 struct {
1085 int first;
1086 };
1087 int x;
1088 constexpr Nested1(int x) : first(12), x() { x = 4; }
1089 constexpr Nested1() : Nested1(42) {}
1090 };
1091 constexpr Nested1 N1{};
1092 static_assert(N1.first == 12, "");
1093
1094 /// Composite.
1095 struct Nested2 {
1096 struct First { int x = 42; };
1097 struct {
1098 First first;
1099 };
1100 int x;
1101 constexpr Nested2(int x) : first(12), x() { x = 4; }
1102 constexpr Nested2() : Nested2(42) {}
1103 };
1104 constexpr Nested2 N2{};
1105 static_assert(N2.first.x == 12, "");
1106
1107 /// Bitfield.
1108 struct Nested3 {
1109 struct {
1110 unsigned first : 2;
1111 };
1112 int x;
1113 constexpr Nested3(int x) : first(3), x() { x = 4; }
1114 constexpr Nested3() : Nested3(42) {}
1115 };
1116
1117 constexpr Nested3 N3{};
1118 static_assert(N3.first == 3, "");
1119
1120 /// Test that we get the offset right if the
1121 /// record has a base.
1122 struct Nested4Base {
1123 int a;
1124 int b;
1125 char c;
1126 };
1127 struct Nested4 : Nested4Base{
1128 struct {
1129 int first;
1130 };
1131 int x;
1132 constexpr Nested4(int x) : first(123), x() { a = 1; b = 2; c = 3; x = 4; }
1133 constexpr Nested4() : Nested4(42) {}
1134 };
1135 constexpr Nested4 N4{};
1136 static_assert(N4.first == 123, "");
1137
1138 struct S {
1139 struct {
1140 int x, y;
1141 };
1142
1143 constexpr S(int x_, int y_) : x(x_), y(y_) {}
1144 };
1145
1146 constexpr S s(1, 2);
1147 static_assert(s.x == 1 && s.y == 2);
1148
1149 struct S2 {
1150 int a;
1151 struct {
1152 int b;
1153 struct {
1154 int x, y;
1155 };
1156 };
1157
1158 constexpr S2(int x_, int y_) : a(3), b(4), x(x_), y(y_) {}
1159 };
1160
1161 constexpr S2 s2(1, 2);
1162 static_assert(s2.x == 1 && s2.y == 2 && s2.a == 3 && s2.b == 4);
1163
1164 #endif
1165
1166
1167 struct B {
1168 struct {
1169 union {
1170 int x = 3;
1171 };
1172 int y = this->x;
1173 };
1174
1175 constexpr B() {}
1176 };
1177 static_assert(B().y == 3, "");
1178 }
1179
1180 namespace InheritedConstructor {
1181 namespace PR47555 {
1182 struct A {
1183 int c;
1184 int d;
1185 constexpr A(int c, int d) : c(c), d(d){}
1186 };
1187 struct B : A { using A::A; };
1188
1189 constexpr B b = {13, 1};
1190 static_assert(b.c == 13, "");
1191 static_assert(b.d == 1, "");
1192 }
1193
1194 namespace PR47555_2 {
1195 struct A {
1196 int c;
1197 int d;
1198 double e;
1199 constexpr A(int c, int &d, double e) : c(c), d(++d), e(e){}
1200 };
1201 struct B : A { using A::A; };
1202
1203 constexpr int f() {
1204 int a = 10;
1205 B b = {10, a, 40.0};
1206 return a;
1207 }
1208 static_assert(f() == 11, "");
1209 }
1210
1211 namespace AaronsTest {
1212 struct T {
1213 constexpr T(float) {}
1214 };
1215
1216 struct Base {
1217 constexpr Base(T t = 1.0f) {}
1218 constexpr Base(float) {}
1219 };
1220
1221 struct FirstMiddle : Base {
1222 using Base::Base;
1223 constexpr FirstMiddle() : Base(2.0f) {}
1224 };
1225
1226 struct SecondMiddle : Base {
1227 constexpr SecondMiddle() : Base(3.0f) {}
1228 constexpr SecondMiddle(T t) : Base(t) {}
1229 };
1230
1231 struct S : FirstMiddle, SecondMiddle {
1232 using FirstMiddle::FirstMiddle;
1233 constexpr S(int i) : S(4.0f) {}
1234 };
1235
1236 constexpr S s(1);
1237 }
1238 }
1239
1240 namespace InvalidCtorInitializer {
1241 struct X {
1242 int Y;
1243 constexpr X()
1244 : Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
1245 };
1246 // no crash on evaluating the constexpr ctor.
1247 constexpr int Z = X().Y; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}}
1248 }
1249
1250 extern int f(); // both-note {{here}}
1251 struct HasNonConstExprMemInit {
1252 int x = f(); // both-note {{non-constexpr function}}
1253 constexpr HasNonConstExprMemInit() {} // both-error {{never produces a constant expression}}
1254 };
1255
1256 namespace {
1257 template <class Tp, Tp v>
1258 struct integral_constant {
1259 static const Tp value = v;
1260 };
1261
1262 template <class Tp, Tp v>
1263 const Tp integral_constant<Tp, v>::value;
1264
1265 typedef integral_constant<bool, true> true_type;
1266 typedef integral_constant<bool, false> false_type;
1267
1268 /// This might look innocent, but we get an evaluateAsInitializer call for the
1269 /// static bool member before evaluating the first static_assert, but we do NOT
1270 /// get such a call for the second one. So the second one needs to lazily visit
1271 /// the data member itself.
1272 static_assert(true_type::value, "");
1273 static_assert(true_type::value, "");
1274 }
1275
1276 #if __cplusplus >= 202002L
1277 namespace {
1278 /// Used to crash because the CXXDefaultInitExpr is of compound type.
1279 struct A {
1280 int &x;
1281 constexpr ~A() { --x; }
1282 };
1283 struct B {
1284 int &x;
1285 const A &a = A{x};
1286 };
1287 constexpr int a() {
1288 int x = 1;
1289 int f = B{x}.x;
1290 B{x}; // both-warning {{expression result unused}}
1291
1292 return 1;
1293 }
1294 }
1295 #endif
1296
1297 namespace pr18633 {
1298 struct A1 {
1299 static const int sz;
1300 static const int sz2;
1301 };
1302 const int A1::sz2 = 11;
1303 template<typename T>
1304 void func () {
1305 int arr[A1::sz];
1306 // both-warning@-1 {{variable length arrays in C++ are a Clang extension}}
1307 // both-note@-2 {{initializer of 'sz' is unknown}}
1308 // both-note@-9 {{declared here}}
1309 }
1310 template<typename T>
1311 void func2 () {
1312 int arr[A1::sz2];
1313 }
1314 const int A1::sz = 12;
1315 void func2() {
1316 func<int>();
1317 func2<int>();
1318 }
1319 }
1320
1321 namespace {
1322 struct F {
1323 static constexpr int Z = 12;
1324 };
1325 F f;
1326 static_assert(f.Z == 12, "");
1327 }
1328
1329 namespace UnnamedBitFields {
1330 struct A {
1331 int : 1;
1332 double f;
1333 int : 1;
1334 char c;
1335 };
1336
1337 constexpr A a = (A){1.0, 'a'};
1338 static_assert(a.f == 1.0, "");
1339 static_assert(a.c == 'a', "");
1340 }
1341
1342 namespace VirtualBases {
1343 /// This used to crash.
1344 namespace One {
1345 class A {
1346 protected:
1347 int x;
1348 };
1349 class B : public virtual A {
1350 public:
1351 int getX() { return x; } // both-note {{declared here}}
1352 };
1353
1354 class DV : virtual public B{};
1355
1356 void foo() {
1357 DV b;
1358 int a[b.getX()]; // both-warning {{variable length arrays}} \
1359 // both-note {{non-constexpr function 'getX' cannot be used}}
1360 }
1361 }
1362
1363 namespace Two {
1364 struct U { int n; };
1365 struct A : virtual U { int n; };
1366 struct B : A {};
1367 B a;
1368 static_assert((U*)(A*)(&a) == (U*)(&a), "");
1369
1370 struct C : virtual A {};
1371 struct D : B, C {};
1372 D d;
1373 constexpr B *p = &d;
1374 constexpr C *q = &d;
1375 static_assert((A*)p == (A*)q, ""); // both-error {{failed}}
1376 }
1377
1378 namespace Three {
1379 struct U { int n; };
1380 struct V : U { int n; };
1381 struct A : virtual V { int n; };
1382 struct Aa { int n; };
1383 struct B : virtual A, Aa {};
1384
1385 struct C : virtual A, Aa {};
1386
1387 struct D : B, C {};
1388
1389 D d;
1390
1391 constexpr B *p = &d;
1392 constexpr C *q = &d;
1393
1394 static_assert((void*)p != (void*)q, "");
1395 static_assert((A*)p == (A*)q, "");
1396 static_assert((Aa*)p != (Aa*)q, "");
1397
1398 constexpr V *v = p;
1399 constexpr V *w = q;
1400 constexpr V *x = (A*)p;
1401 static_assert(v == w, "");
1402 static_assert(v == x, "");
1403
1404 static_assert((U*)&d == p, "");
1405 static_assert((U*)&d == q, "");
1406 static_assert((U*)&d == v, "");
1407 static_assert((U*)&d == w, "");
1408 static_assert((U*)&d == x, "");
1409
1410 struct X {};
1411 struct Y1 : virtual X {};
1412 struct Y2 : X {};
1413 struct Z : Y1, Y2 {};
1414 Z z;
1415 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
1416 }
1417 }
1418
1419 namespace ZeroInit {
1420 struct S3 {
1421 S3() = default;
1422 S3(const S3&) = default;
1423 S3(S3&&) = default;
1424 constexpr S3(int n) : n(n) {}
1425 int n;
1426 };
1427 constexpr S3 s3d; // both-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
1428 static_assert(s3d.n == 0, "");
1429
1430 struct P {
1431 int a = 10;
1432 };
1433 static_assert(P().a == 10, "");
1434 }
1435
1436 namespace {
1437 #if __cplusplus >= 202002L
1438 struct C {
1439 template <unsigned N> constexpr C(const char (&)[N]) : n(N) {}
1440 unsigned n;
1441 };
1442 template <C c>
1443 constexpr auto operator""_c() { return c.n; }
1444
1445 constexpr auto waldo = "abc"_c;
1446 static_assert(waldo == 4, "");
1447 #endif
1448 }
1449
1450
1451 namespace TemporaryWithInvalidDestructor {
1452 #if __cplusplus >= 202002L
1453 struct A {
1454 bool a = true;
1455 constexpr ~A() noexcept(false) { // both-error {{never produces a constant expression}}
1456 throw; // both-note 2{{not valid in a constant expression}} \
1457 // both-error {{cannot use 'throw' with exceptions disabled}}
1458 }
1459 };
1460 static_assert(A().a, ""); // both-error {{not an integral constant expression}} \
1461 // both-note {{in call to}}
1462 #endif
1463 }
1464
1465 namespace IgnoredCtorWithZeroInit {
1466 struct S {
1467 int a;
1468 };
1469
1470 bool get_status() {
1471 return (S(), true);
1472 }
1473 }
1474
1475 #if __cplusplus >= 202002L
1476 namespace VirtOperator {
1477 /// This used to crash because it's a virtual CXXOperatorCallExpr.
1478 struct B {
1479 virtual constexpr bool operator==(const B&) const { return true; }
1480 };
1481 struct D : B {
1482 constexpr bool operator==(const B&) const override{ return false; } // both-note {{operator}}
1483 };
1484 constexpr bool cmp_base_derived = D() == D(); // both-warning {{ambiguous}}
1485 }
1486
1487 namespace FloatAPValue {
1488 struct ClassTemplateArg {
1489 int a;
1490 float f;
1491 };
1492 template<ClassTemplateArg A> struct ClassTemplateArgTemplate {
1493 static constexpr const ClassTemplateArg &Arg = A;
1494 };
1495 ClassTemplateArgTemplate<ClassTemplateArg{1, 2.0f}> ClassTemplateArgObj;
1496 template<const ClassTemplateArg&> struct ClassTemplateArgRefTemplate {};
1497 ClassTemplateArgRefTemplate<ClassTemplateArgObj.Arg> ClassTemplateArgRefObj;
1498 }
1499 #endif
1500
1501 namespace LocalWithThisPtrInit {
1502 struct S {
1503 int i;
1504 int *p = &i;
1505 };
1506 constexpr int foo() {
1507 S s{2};
1508 return *s.p;
1509 }
1510 static_assert(foo() == 2, "");
1511 }
1512
1513 namespace OnePastEndAndBack {
1514 struct Base {
1515 constexpr Base() {}
1516 int n = 0;
1517 };
1518
1519 constexpr Base a;
1520 constexpr const Base *c = &a + 1;
1521 constexpr const Base *d = c - 1;
1522 static_assert(d == &a, "");
1523 }
1524
1525 namespace BitSet {
1526 class Bitset {
1527 unsigned Bit = 0;
1528
1529 public:
1530 constexpr Bitset() {
1531 int Init[2] = {1,2};
1532 for (auto I : Init)
1533 set(I);
1534 }
1535 constexpr void set(unsigned I) {
1536 this->Bit++;
1537 this->Bit = 1u << 1;
1538 }
1539 };
1540
1541 struct ArchInfo {
1542 Bitset DefaultExts;
1543 };
1544
1545 constexpr ArchInfo ARMV8A = {
1546 Bitset()
1547 };
1548 }
1549
1550 namespace ArrayInitChain {
1551 struct StringLiteral {
1552 const char *S;
1553 };
1554
1555 struct CustomOperandVal {
1556 StringLiteral Str;
1557 unsigned Width;
1558 unsigned Mask = Width + 1;
1559 };
1560
1561 constexpr CustomOperandVal A[] = {
1562 {},
1563 {{"depctr_hold_cnt"}, 12, 13},
1564 };
1565 static_assert(A[0].Str.S == nullptr, "");
1566 static_assert(A[0].Width == 0, "");
1567 static_assert(A[0].Mask == 1, "");
1568
1569 static_assert(A[1].Width == 12, "");
1570 static_assert(A[1].Mask == 13, "");
1571 }
1572
1573 #if __cplusplus >= 202002L
1574 namespace ctorOverrider {
1575 // Ensure that we pick the right final overrider during construction.
1576 struct A {
1577 virtual constexpr char f() const { return 'A'; }
1578 char a = f();
1579 };
1580
1581 struct Covariant1 {
1582 A d;
1583 };
1584
1585 constexpr Covariant1 cb;
1586 }
1587 #endif
1588
1589 #if __cplusplus >= 202002L
1590 namespace VirtDtor {
1591 struct X { char *p; constexpr ~X() { *p++ = 'X'; } };
1592 struct Y : X { int y; virtual constexpr ~Y() { *p++ = 'Y'; } };
1593 struct Z : Y { int z; constexpr ~Z() override { *p++ = 'Z'; } };
1594
1595 union VU {
1596 constexpr VU() : z() {}
1597 constexpr ~VU() {}
1598 Z z;
1599 };
1600
1601 constexpr char virt_dtor(int mode, const char *expected) {
1602 char buff[4] = {};
1603 VU vu;
1604 vu.z.p = buff;
1605
1606 ((Y&)vu.z).~Y();
1607 return true;
1608 }
1609 static_assert(virt_dtor(0, "ZYX"));
1610 }
1611
1612 namespace DtorDestroysFieldsAfterSelf {
1613 struct S {
1614 int a = 10;
1615 constexpr ~S() {
1616 a = 0;
1617 }
1618
1619 };
1620 struct F {
1621 S s;
1622 int a;
1623 int &b;
1624 constexpr F(int a, int &b) : a(a), b(b) {}
1625 constexpr ~F() {
1626 b += s.a;
1627 }
1628 };
1629
1630 constexpr int foo() {
1631 int a = 10;
1632 int b = 5;
1633 {
1634 F f(a, b);
1635 }
1636
1637 return b;
1638 }
1639
1640 static_assert(foo() == 15);
1641 }
1642 #endif
1643
1644 namespace ExprWithCleanups {
1645 struct A { A(); ~A(); int get(); };
1646 constexpr int get() {return false ? A().get() : 1;}
1647 static_assert(get() == 1, "");
1648
1649
1650 struct S {
1651 int V;
1652 constexpr S(int V) : V(V) {}
1653 constexpr int get() {
1654 return V;
1655 }
1656 };
1657 constexpr int get(bool b) {
1658 S a = b ? S(1) : S(2);
1659
1660 return a.get();
1661 }
1662 static_assert(get(true) == 1, "");
1663 static_assert(get(false) == 2, "");
1664
1665
1666 constexpr auto F = true ? 1i : 2i;
1667 static_assert(F == 1i, "");
1668 }
1669
1670 namespace NullptrCast {
1671 struct A {};
1672 struct B : A { int n; };
1673 constexpr A *na = nullptr;
1674 constexpr B *nb = nullptr;
1675 constexpr A &ra = *nb; // both-error {{constant expression}} \
1676 // both-note {{dereferencing a null pointer}}
1677 constexpr B &rb = (B&)*na; // both-error {{constant expression}} \
1678 // both-note {{dereferencing a null pointer}}
1679 constexpr bool test() {
1680 auto a = (A*)(B*)nullptr;
1681
1682 return a == nullptr;
1683 }
1684 static_assert(test(), "");
1685
1686 constexpr bool test2() {
1687 auto a = (B*)(A*)nullptr;
1688
1689 return a == nullptr;
1690 }
1691 static_assert(test2(), "");
1692 }
1693
1694 namespace NonConst {
1695 template <int I>
1696 struct S {
1697 static constexpr int Size = I;
1698 constexpr int getSize() const { return I; }
1699 explicit S(int a) {}
1700 };
1701
1702 void func() {
1703 int a,b ;
1704 const S<10> s{a};
1705 static_assert(s.getSize() == 10, "");
1706 }
1707 }
1708
1709 namespace ExplicitThisInTemporary {
1710 struct B { B *p = this; };
1711 constexpr bool g(B b) { return &b == b.p; }
1712 static_assert(g({}), "");
1713 }
1714
1715 namespace IgnoredMemberExpr {
1716 class A {
1717 public:
1718 int a;
1719 };
1720 class B : public A {
1721 public:
1722 constexpr int foo() {
1723 a; // both-warning {{expression result unused}}
1724 return 0;
1725 }
1726 };
1727 static_assert(B{}.foo() == 0, "");
1728 }
1729
1730 #if __cplusplus >= 202002L
1731 namespace DeadUpcast {
1732 struct A {};
1733 struct B : A{};
1734 constexpr bool foo() {
1735
1736 B *pb;
1737 {
1738 B b;
1739 pb = &b;
1740 }
1741 A *pa = pb;
1742
1743 return true;
1744 }
1745 static_assert(foo(), "");
1746 }
1747 #endif
1748
1749 namespace CtorOfInvalidClass {
1750 constexpr struct { Unknown U; } InvalidCtor; // both-error {{unknown type name 'Unknown'}} \
1751 // both-error {{must be initialized by a constant expression}}
1752
1753 #if __cplusplus >= 202002L
1754 template <typename T, auto Q>
1755 concept ReferenceOf = Q;
1756 /// This calls a valid and constexpr copy constructor of InvalidCtor,
1757 /// but should still be rejected.
1758 template<ReferenceOf<InvalidCtor> auto R, typename Rep> int F; // both-error {{non-type template argument is not a constant expression}}
1759 #endif
1760 }
1761
1762 namespace IncompleteTypes {
1763 struct Incomplete;
1764
1765 constexpr bool foo() {
1766 extern Incomplete bounded[10];
1767 extern Incomplete unbounded[];
1768 extern Incomplete IT;
1769 return true;
1770 }
1771 static_assert(foo(), "");
1772 }
1773
1774 namespace RedeclaredCtor {
1775
1776 struct __sp_mut {
1777 void *__lx_;
1778 constexpr __sp_mut(void *) noexcept;
1779 };
1780 int mut_back[1];
1781
1782 constexpr __sp_mut::__sp_mut(void *p) noexcept : __lx_(p) {}
1783 constexpr __sp_mut muts = &mut_back[0];
1784 }
1785
1786 namespace IntegralBaseCast {
1787 class A {};
1788 class B : public A {};
1789 struct S {
1790 B *a;
1791 };
1792
1793 constexpr int f() {
1794 S s{};
1795 A *a = s.a;
1796 return 0;
1797 }
1798
1799 static_assert(f() == 0, "");
1800 }
1801
1802 namespace AccessMismatch {
1803 struct A {
1804 public:
1805 constexpr A() : a(0), b(0) {}
1806 int a;
1807 constexpr bool cmp() const { return &a < &b; } // both-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
1808 private:
1809 int b;
1810 };
1811 static_assert(A().cmp(), ""); // both-error {{constant expression}} \
1812 // both-note {{in call}}
1813
1814 class B {
1815 public:
1816 A a;
1817 constexpr bool cmp() const { return &a.a < &b.a; } // both-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
1818 protected:
1819 A b;
1820 };
1821 static_assert(B().cmp(), ""); // both-error {{constant expression}} \
1822 // both-note {{in call}}
1823 }
1824
1825 namespace GlobalDtor {
1826 struct A {
1827 };
1828 constexpr A a = {};
1829 constexpr void destroy1() { // both-error {{constexpr}}
1830 a.~A(); // both-note {{cannot modify an object that is visible outside}}
1831 }
1832 }
1833
1834 namespace NullDtor {
1835 struct S {};
1836 constexpr int foo() { // both-error {{never produces a constant expression}}
1837 S *s = nullptr;
1838 s->~S(); // both-note 2{{destruction of dereferenced null pointer is not allowed in a constant expression}}
1839 return 10;
1840 }
1841 static_assert(foo() == 10, ""); // both-error {{not an integral constant expression}} \
1842 // both-note {{in call to}}
1843 }
1844
1845 namespace DiamondDowncast {
1846 struct Top {};
1847 struct Middle1 : Top {};
1848 struct Middle2 : Top {};
1849 struct Bottom : Middle1, Middle2 {};
1850
1851 constexpr Bottom bottom;
1852 constexpr Top &top1 = (Middle1&)bottom;
1853 constexpr Middle2 &fail = (Middle2&)top1; // both-error {{must be initialized by a constant expression}} \
1854 // both-note {{cannot cast object of dynamic type 'const Bottom' to type 'Middle2'}}
1855 }
1856
1857 namespace PrimitiveInitializedByInitList {
1858 constexpr struct {
1859 int a;
1860 int b{this->a};
1861 } c{ 17 };
1862 static_assert(c.b == 17, "");
1863 }
1864
1865 namespace MethodWillHaveBody {
1866 class A {
1867 public:
1868 static constexpr int get_value2() { return 1 + get_value(); }
1869 static constexpr int get_value() { return 1; }
1870 };
1871 static_assert(A::get_value2() == 2, "");
1872
1873 template<typename T> constexpr T f(T);
1874 template<typename T> constexpr T g(T t) {
1875 typedef int arr[f(T())]; // both-warning {{variable length array}} \
1876 // both-note {{undefined function 'f<int>'}}
1877 return t;
1878 }
1879 template<typename T> constexpr T f(T t) { // both-note {{declared here}}
1880 typedef int arr[g(T())]; // both-note {{instantiation of}}
1881 return t;
1882 }
1883 int n = f(0); // both-note {{instantiation of}}
1884 }
1885
1886 namespace StaticRedecl {
1887 struct T {
1888 static T tt;
1889 constexpr T() : p(&tt) {}
1890 T *p;
1891 };
1892 T T::tt;
1893 constexpr T t;
1894 static_assert(t.p == &T::tt, "");
1895 }