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}}
16 A a
= {1, 2}; // cxx20-error {{no matching constructor}}
22 B b1
= B(); // trigger declaration of implicit ctors
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
; };
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}}
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}}
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}}
51 .x
= 1, // override-note {{previous}}
52 .x
= 1 // override-error {{overrides prior initialization}}
53 }; // wmissing-warning {{missing field 'y' initializer}}
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; };
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
{
86 struct derived
: base
{
89 derived d
= {.x
= 1, .y
= 2}; // expected-error {{'x' does not refer to any field}}
93 union U
{ int a
, b
; };
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
; };
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
; };
112 void g2() { int k
= f({.x
= 1, 2, .y
= 3}); (void)k
; } // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{here}}
115 void h(A a
); // expected-note {{candidate}}
116 void h(C c
); // expected-note {{candidate}}
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}}
127 j({.x
= 1, .y
= 2}); // expected-error {{ambiguous}}
132 void l(E e
); // expected-note {{candidate}}
133 int &l(F f
); // expected-note {{candidate}}
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}) = {});
152 h
<A
, C
>(); // ok, selects C overload by SFINAE
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}}}));
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");
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}}
185 // OK, does not instantiate X<void&> (!).
203 struct : public A
, public B
{
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'}}
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}}