Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / constexpr-value-init.cpp
blob3314174a0ea17d65948c06f621e84c011e2245fd
1 // RUN: %clang_cc1 %s -Wno-uninitialized -std=c++17 -fsyntax-only -verify
3 struct A {
4 constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
5 int a;
6 int b;
7 };
8 struct B { // expected-note {{in call to 'A()'}}
9 A a;
12 constexpr A a1; // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
13 constexpr A a2 = A(); // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
14 void f() {
15 constexpr A a; // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
18 constexpr B b1; // expected-error {{constant expression}} expected-note {{in call to 'B()'}}
19 constexpr B b2 = B(); // ok
20 static_assert(b2.a.a == 1, "");
21 static_assert(b2.a.b == 2, "");
23 struct C {
24 int c;
26 struct D : C { int d; };
27 constexpr C c1; // expected-error {{without a user-provided default constructor}}
28 constexpr C c2 = C(); // ok
29 constexpr D d1; // expected-error {{without a user-provided default constructor}}
30 constexpr D d2 = D(); // ok with DR1452
31 static_assert(D().c == 0, "");
32 static_assert(D().d == 0, "");
34 struct V : virtual C {};
35 template<typename T> struct Z : T {
36 constexpr Z() : V() {}
38 constexpr int n = Z<V>().c; // expected-error {{constant expression}} expected-note {{non-literal type 'Z<V>'}}
40 struct E { // expected-note {{in call to 'A()'}}
41 A a[2];
43 constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
44 constexpr E e2 = E();
45 static_assert(e2.a[0].a == 1, "");
46 static_assert(e2.a[0].b == 2, "");
47 static_assert(e2.a[1].a == 1, "");
48 static_assert(e2.a[1].b == 2, "");
50 namespace InvalidDeclInsideConstExpr {
51 template <int a> struct i; // expected-note {{template is declared here}}
52 template <> struct i<0> {};
54 template <int x> constexpr auto c() {
55 // i<x> is valid, but it might be incomplete. g would be invalid in that case.
56 i<x> g; // expected-error {{implicit instantiation of undefined template 'InvalidDeclInsideConstExpr::i<1>'}}
57 return 0;
60 auto y = c<1>(); // expected-note {{in instantiation of function template specialization 'InvalidDeclInsideConstExpr::c<1>' requested here}}
61 auto x = c<0>(); // this is valid.