Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / cxx2a-initializer-aggregates.cpp
blob510ace58c35a6aaaf5a308013f121d532f495156
1 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,pedantic,override,reorder -pedantic-errors
2 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,pedantic,override,reorder -Wno-c++20-designator -pedantic-errors
3 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,pedantic -Werror=c99-designator -Wno-reorder-init-list -Wno-initializer-overrides
4 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,reorder -Wno-c99-designator -Werror=reorder-init-list -Wno-initializer-overrides
5 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,override -Wno-c99-designator -Wno-reorder-init-list -Werror=initializer-overrides
6 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected -Wno-c99-designator -Wno-reorder-init-list -Wno-initializer-overrides
7 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,wmissing -Wmissing-field-initializers -Wno-c99-designator -Wno-reorder-init-list -Wno-initializer-overrides
10 namespace class_with_ctor {
11 struct A { // cxx20-note 6{{candidate}}
12 A() = default; // cxx20-note 3{{candidate}}
13 int x;
14 int y;
16 A a = {1, 2}; // cxx20-error {{no matching constructor}}
18 struct B {
19 int x;
20 int y;
22 B b1 = B(); // trigger declaration of implicit ctors
23 B b2 = {1, 2}; // ok
25 struct C : A {
26 A a;
28 C c1 = {{}, {}}; // ok, call default ctor twice
29 C c2 = {{1, 2}, {3, 4}}; // cxx20-error 2{{no matching constructor}}
32 namespace designator {
33 struct A { int x, y; };
34 struct B { A a; };
36 A a1 = {
37 .y = 1, // reorder-note {{previous initialization for field 'y' is here}}
38 .x = 2 // reorder-error {{ISO C++ requires field designators to be specified in declaration order; field 'y' will be initialized after field 'x'}}
40 int arr[3] = {[1] = 5}; // pedantic-error {{array designators are a C99 extension}}
41 B b = {.a.x = 0}; // pedantic-error {{nested designators are a C99 extension}}
42 A a2 = {
43 .x = 1, // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
44 2 // pedantic-note {{first non-designated initializer is here}}
46 A a3 = {
47 1, // pedantic-note {{first non-designated initializer is here}}
48 .y = 2 // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
50 A a4 = {
51 .x = 1, // override-note {{previous}}
52 .x = 1 // override-error {{overrides prior initialization}}
53 }; // wmissing-warning {{missing field 'y' initializer}}
54 A a5 = {
55 .y = 1, // override-note {{previous}}
56 .y = 1 // override-error {{overrides prior initialization}}
57 }; // wmissing-warning {{missing field 'x' initializer}}
58 B b2 = {.a = 1}; // pedantic-error {{brace elision for designated initializer is a C99 extension}}
59 // wmissing-warning@-1 {{missing field 'y' initializer}}
60 B b3 = {.a = 1, 2}; // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{first non-designated}} pedantic-error {{brace elision}}
61 B b4 = {.a = 1, 2, 3}; // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{first non-designated}} pedantic-error {{brace elision}} expected-error {{excess elements}}
62 B b5 = {.a = nullptr}; // expected-error {{cannot initialize}}
63 // wmissing-warning@-1 {{missing field 'y' initializer}}
64 struct C { int :0, x, :0, y, :0; };
65 C c = {
66 .x = 1, // override-note {{previous}}
67 .x = 1, // override-error {{overrides prior initialization}} override-note {{previous}}
68 .y = 1, // override-note {{previous}}
69 .y = 1, // override-error {{overrides prior initialization}} // reorder-note {{previous initialization for field 'y' is here}}
70 .x = 1, // reorder-error {{declaration order}} override-error {{overrides prior initialization}} override-note {{previous}}
71 .x = 1, // override-error {{overrides prior initialization}}
74 struct Foo { int a, b; };
76 struct Foo foo0 = { 1 }; // wmissing-warning {{missing field 'b' initializer}}
77 struct Foo foo1 = { .a = 1 }; // wmissing-warning {{missing field 'b' initializer}}
78 struct Foo foo2 = { .b = 1 }; // wmissing-warning {{missing field 'a' initializer}}
82 namespace base_class {
83 struct base {
84 int x;
86 struct derived : base {
87 int y;
89 derived d = {.x = 1, .y = 2}; // expected-error {{'x' does not refer to any field}}
92 namespace union_ {
93 union U { int a, b; };
94 U u = {
95 .a = 1, // override-note {{here}}
96 .b = 2, // override-error {{overrides prior}}
100 namespace overload_resolution {
101 struct A { int x, y; };
102 union B { int x, y; };
104 void f(A a);
105 void f(B b) = delete;
106 void g() { f({.x = 1, .y = 2}); } // ok, calls non-union overload
108 // As an extension of the union case, overload resolution won't pick any
109 // candidate where a field initializer would be overridden.
110 struct A2 { int x, other, y; };
111 int f(A2);
112 void g2() { int k = f({.x = 1, 2, .y = 3}); (void)k; } // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{here}}
114 struct C { int x; };
115 void h(A a); // expected-note {{candidate}}
116 void h(C c); // expected-note {{candidate}}
117 void i() {
118 h({.x = 1, .y = 2});
119 h({.y = 1, .x = 2}); // reorder-error {{declaration order}} reorder-note {{previous}}
120 h({.x = 1}); // expected-error {{ambiguous}}
123 struct D { int y, x; };
124 void j(A a); // expected-note {{candidate}}
125 void j(D d); // expected-note {{candidate}}
126 void k() {
127 j({.x = 1, .y = 2}); // expected-error {{ambiguous}}
130 struct E { A a; };
131 struct F { int a; };
132 void l(E e); // expected-note {{candidate}}
133 int &l(F f); // expected-note {{candidate}}
134 void m() {
135 int &r = l({.a = 0}); // ok, l(E) is not viable
136 int &s = l({.a = {0}}); // expected-error {{ambiguous}}
140 namespace deduction {
141 struct A { int x, y; };
142 union B { int x, y; };
144 template<typename T, typename U> void f(decltype(T{.x = 1, .y = 2}) = {});
145 template<typename T, typename U> void f(decltype(U{.x = 1, .y = 2}) = {}) = delete;
146 void g() { f<A, B>(); } // ok, calls non-union overload
148 struct C { int y, x; };
149 template<typename T, typename U> void h(decltype(T{.y = 1, .x = 2}) = {}) = delete;
150 template<typename T, typename U> void h(decltype(U{.y = 1, .x = 2}) = {});
151 void i() {
152 h<A, C>(); // ok, selects C overload by SFINAE
155 struct D { int n; };
156 struct E { D n; };
157 template<typename T, typename U> void j1(decltype(T{.n = 0}));
158 template<typename T, typename U> void j1(decltype(U{.n = 0})) = delete;
159 template<typename T, typename U> void j2(decltype(T{.n = {0}})); // expected-note {{candidate}}
160 template<typename T, typename U> void j2(decltype(U{.n = {0}})); // expected-note {{candidate}}
161 template<typename T, typename U> void j3(decltype(T{.n = {{0}}})) = delete;
162 template<typename T, typename U> void j3(decltype(U{.n = {{0}}}));
163 void k() {
164 j1<D, E>({}); // ok, selects D overload by SFINAE (too few braces for E)
165 j2<D, E>({}); // expected-error {{ambiguous}}
166 j3<D, E>({}); // ok, selects E overload by SFINAE (too many braces for D)
170 namespace no_unwrap {
171 template<typename T> struct X {
172 static_assert(false, "should not be instantiated");
174 struct Q {
175 template<typename T, typename U = typename X<T>::type> Q(T&&);
178 // Ensure that we do not try to call 'Q::Q(.a = 1)' here.
179 void g() { Q q = {.a = 1}; } // expected-error {{initialization of non-aggregate type 'Q' with a designated initializer list}}
181 struct S { int a; };
182 void h(Q q);
183 void h(S s);
185 // OK, does not instantiate X<void&> (!).
186 void i() {
187 h({.a = 1});
191 namespace GH63605 {
192 struct A {
193 unsigned x;
194 unsigned y;
195 unsigned z;
198 struct B {
199 unsigned a;
200 unsigned b;
203 struct : public A, public B {
204 unsigned : 2;
205 unsigned a : 6;
206 unsigned : 1;
207 unsigned b : 6;
208 unsigned : 2;
209 unsigned c : 6;
210 unsigned d : 1;
211 unsigned e : 2;
212 } data = {
213 {.z=0,
214 // pedantic-note@-1 {{first non-designated initializer is here}}
215 // reorder-note@-2 {{previous initialization for field 'z' is here}}
216 .y=1, // reorder-error {{field 'z' will be initialized after field 'y'}}
217 // reorder-note@-1 {{previous initialization for field 'y' is here}}
218 .x=2}, // reorder-error {{field 'y' will be initialized after field 'x'}}
219 {.b=3, // reorder-note {{previous initialization for field 'b' is here}}
220 .a=4}, // reorder-error {{field 'b' will be initialized after field 'a'}}
221 .e = 1, // reorder-note {{previous initialization for field 'e' is here}}
222 // pedantic-error@-1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
223 .d = 1, // reorder-error {{field 'e' will be initialized after field 'd'}}
224 // reorder-note@-1 {{previous initialization for field 'd' is here}}
225 .c = 1, // reorder-error {{field 'd' will be initialized after field 'c'}} // reorder-note {{previous initialization for field 'c' is here}}
226 .b = 1, // reorder-error {{field 'c' will be initialized after field 'b'}} // reorder-note {{previous initialization for field 'b' is here}}
227 .a = 1, // reorder-error {{field 'b' will be initialized after field 'a'}}
231 namespace GH63759 {
232 struct C {
233 int y = 1;
234 union {
235 int a;
236 short b;
238 int x = 1;
241 void foo() {
242 C c1 = {.x = 3, .a = 1}; // reorder-error-re {{ISO C++ requires field designators to be specified in declaration order; field 'x' will be initialized after field 'GH63759::C::(anonymous union at {{.*}})'}}
243 // reorder-note@-1 {{previous initialization for field 'x' is here}}
245 C c2 = {.a = 3, .y = 1}; // reorder-error-re {{ISO C++ requires field designators to be specified in declaration order; field 'GH63759::C::(anonymous union at {{.*}})' will be initialized after field 'y'}}
246 // reorder-note-re@-1 {{previous initialization for field 'GH63759::C::(anonymous union at {{.*}})' is here}}