xref: /minix3/external/bsd/llvm/dist/clang/test/CXX/drs/dr14xx.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5 
6 #if __cplusplus < 201103L
7 // expected-no-diagnostics
8 #endif
9 
10 namespace dr1460 { // dr1460: 3.5
11 #if __cplusplus >= 201103L
12   namespace DRExample {
13     union A {
14       union {};
15       union {};
A()16       constexpr A() {}
17     };
18     constexpr A a = A();
19 
20     union B {
21       union {};
22       union {};
23       constexpr B() = default;
24     };
25     constexpr B b = B();
26 
27     union C {
28       union {};
29       union {};
30     };
31     constexpr C c = C();
32 #if __cplusplus > 201103L
f()33     constexpr void f() { C c; }
34     static_assert((f(), true), "");
35 #endif
36   }
37 
38   union A {};
39   union B { int n; }; // expected-note +{{here}}
40   union C { int n = 0; };
41   struct D { union {}; };
42   struct E { union { int n; }; }; // expected-note +{{here}}
43   struct F { union { int n = 0; }; };
44 
45   struct X {
46     friend constexpr A::A() noexcept;
47     friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}}
48     friend constexpr C::C() noexcept;
49     friend constexpr D::D() noexcept;
50     friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}}
51     friend constexpr F::F() noexcept;
52   };
53 
54   // These are OK, because value-initialization doesn't actually invoke the
55   // constructor.
56   constexpr A a = A();
57   constexpr B b = B();
58   constexpr C c = C();
59   constexpr D d = D();
60   constexpr E e = E();
61   constexpr F f = F();
62 
63   namespace Defaulted {
64     union A { constexpr A() = default; };
65     union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}}
66     union C { int n = 0; constexpr C() = default; };
67     struct D { union {}; constexpr D() = default; };
68     struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}}
69     struct F { union { int n = 0; }; constexpr F() = default; };
70 
71     struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}}
72     struct H {
73       union {
74         int n = 0;
75       };
76       union { // expected-note 2{{member not initialized}}
77         int m;
78       };
Hdr1460::Defaulted::H79       constexpr H() {} // expected-error {{must initialize all members}}
Hdr1460::Defaulted::H80       constexpr H(bool) : m(1) {}
Hdr1460::Defaulted::H81       constexpr H(char) : n(1) {} // expected-error {{must initialize all members}}
Hdr1460::Defaulted::H82       constexpr H(double) : m(1), n(1) {}
83     };
84   }
85 
86 #if __cplusplus > 201103L
check()87   template<typename T> constexpr bool check() {
88     T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}}
89     return true;
90   }
91   static_assert(check<A>(), "");
92   static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}}
93   static_assert(check<C>(), "");
94   static_assert(check<D>(), "");
95   static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}}
96   static_assert(check<F>(), "");
97 #endif
98 
99   union G {
100     int a = 0; // expected-note {{previous initialization is here}}
101     int b = 0; // expected-error {{initializing multiple members of union}}
102   };
103   union H {
104     union {
105       int a = 0; // expected-note {{previous initialization is here}}
106     };
107     union {
108       int b = 0; // expected-error {{initializing multiple members of union}}
109     };
110   };
111   struct I {
112     union {
113       int a = 0; // expected-note {{previous initialization is here}}
114       int b = 0; // expected-error {{initializing multiple members of union}}
115     };
116   };
117   struct J {
118     union { int a = 0; };
119     union { int b = 0; };
120   };
121 
122   namespace Overriding {
123     struct A {
124       int a = 1, b, c = 3;
Adr1460::Overriding::A125       constexpr A() : b(2) {}
126     };
127     static_assert(A().a == 1 && A().b == 2 && A().c == 3, "");
128 
129     union B {
130       int a, b = 2, c;
B()131       constexpr B() : a(1) {}
B(char)132       constexpr B(char) : b(4) {}
B(int)133       constexpr B(int) : c(3) {}
B(const char *)134       constexpr B(const char*) {}
135     };
136     static_assert(B().a == 1, "");
137     static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
138     static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}}
139     static_assert(B('x').b == 4, "");
140     static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
141     static_assert(B(123).c == 3, "");
142     static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
143     static_assert(B("").b == 2, "");
144     static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
145 
146     struct C {
147       union { int a, b = 2, c; };
148       union { int d, e = 5, f; };
Cdr1460::Overriding::C149       constexpr C() : a(1) {}
Cdr1460::Overriding::C150       constexpr C(char) : c(3) {}
Cdr1460::Overriding::C151       constexpr C(int) : d(4) {}
Cdr1460::Overriding::C152       constexpr C(float) : f(6) {}
Cdr1460::Overriding::C153       constexpr C(const char*) {}
154     };
155 
156     static_assert(C().a == 1, "");
157     static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
158     static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
159     static_assert(C().e == 5, "");
160 
161     static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
162     static_assert(C('x').c == 3, "");
163     static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
164     static_assert(C('x').e == 5, "");
165 
166     static_assert(C(1).b == 2, "");
167     static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
168     static_assert(C(1).d == 4, "");
169     static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
170 
171     static_assert(C(1.f).b == 2, "");
172     static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
173     static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
174     static_assert(C(1.f).f == 6, "");
175 
176     static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
177     static_assert(C("").b == 2, "");
178     static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
179     static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
180     static_assert(C("").e == 5, "");
181     static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}}
182 
183     struct D;
184     extern const D d;
185     struct D {
186       int a;
187       union {
188         int b = const_cast<D&>(d).a = 1; // not evaluated
189         int c;
190       };
Ddr1460::Overriding::D191       constexpr D() : a(0), c(0) {}
192     };
193     constexpr D d {};
194     static_assert(d.a == 0, "");
195   }
196 #endif
197 }
198