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-designated -Wmissing-field-initializers -Wno-c99-designator -Wno-reorder-init-list -Wno-initializer-overrides
8 // RUN: %clang_cc1 -std=c++20 %s -verify=cxx20,expected,wmissing -Wmissing-field-initializers -Wno-missing-designated-field-initializers -Wno-c99-designator -Wno-reorder-init-list -Wno-initializer-overrides
11 namespace class_with_ctor
{
12 struct A
{ // cxx20-note 6{{candidate}}
13 A() = default; // cxx20-note 3{{candidate}}
17 A a
= {1, 2}; // cxx20-error {{no matching constructor}}
23 B b1
= B(); // trigger declaration of implicit ctors
29 C c1
= {{}, {}}; // ok, call default ctor twice
30 C c2
= {{1, 2}, {3, 4}}; // cxx20-error 2{{no matching constructor}}
33 namespace designator
{
34 struct A
{ int x
, y
; };
38 .y
= 1, // reorder-note {{previous initialization for field 'y' is here}}
39 .x
= 2 // reorder-error {{ISO C++ requires field designators to be specified in declaration order; field 'y' will be initialized after field 'x'}}
41 int arr
[3] = {[1] = 5}; // pedantic-error {{array designators are a C99 extension}}
42 B b
= {.a
.x
= 0}; // pedantic-error {{nested designators are a C99 extension}}
44 .x
= 1, // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
45 2 // pedantic-note {{first non-designated initializer is here}}
48 1, // pedantic-note {{first non-designated initializer is here}}
49 .y
= 2 // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
52 .x
= 1, // override-note {{previous}}
53 .x
= 1 // override-error {{overrides prior initialization}}
54 }; // wmissing-designated-warning {{missing field 'y' initializer}}
56 .y
= 1, // override-note {{previous}}
57 .y
= 1 // override-error {{overrides prior initialization}}
58 }; // wmissing-designated-warning {{missing field 'x' initializer}}
59 B b2
= {.a
= 1}; // pedantic-error {{brace elision for designated initializer is a C99 extension}}
60 // wmissing-warning@-1 {{missing field 'y' initializer}}
61 B b3
= {.a
= 1, 2}; // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{first non-designated}} pedantic-error {{brace elision}}
62 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}}
63 B b5
= {.a
= nullptr}; // expected-error {{cannot initialize}}
64 // wmissing-warning@-1 {{missing field 'y' initializer}}
65 struct C
{ int :0, x
, :0, y
, :0; };
67 .x
= 1, // override-note {{previous}}
68 .x
= 1, // override-error {{overrides prior initialization}} override-note {{previous}}
69 .y
= 1, // override-note {{previous}}
70 .y
= 1, // override-error {{overrides prior initialization}} // reorder-note {{previous initialization for field 'y' is here}}
71 .x
= 1, // reorder-error {{declaration order}} override-error {{overrides prior initialization}} override-note {{previous}}
72 .x
= 1, // override-error {{overrides prior initialization}}
75 struct Foo
{ int a
, b
; };
77 struct Foo foo0
= { 1 }; // wmissing-warning {{missing field 'b' initializer}}
78 struct Foo foo1
= { .a
= 1 }; // wmissing-designated-warning {{missing field 'b' initializer}}
79 struct Foo foo2
= { .b
= 1 }; // wmissing-designated-warning {{missing field 'a' initializer}}
83 namespace base_class
{
87 struct derived
: base
{
90 derived d
= {.x
= 1, .y
= 2}; // expected-error {{'x' does not refer to any field}}
94 union U
{ int a
, b
; };
96 .a
= 1, // override-note {{here}}
97 .b
= 2, // override-error {{overrides prior}}
101 namespace overload_resolution
{
102 struct A
{ int x
, y
; };
103 union B
{ int x
, y
; };
106 void f(B b
) = delete;
107 void g() { f({.x
= 1, .y
= 2}); } // ok, calls non-union overload
109 // As an extension of the union case, overload resolution won't pick any
110 // candidate where a field initializer would be overridden.
111 struct A2
{ int x
, other
, y
; };
113 void g2() { int k
= f({.x
= 1, 2, .y
= 3}); (void)k
; } // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{here}}
116 void h(A a
); // expected-note {{candidate}}
117 void h(C c
); // expected-note {{candidate}}
120 h({.y
= 1, .x
= 2}); // reorder-error {{declaration order}} reorder-note {{previous}}
121 h({.x
= 1}); // expected-error {{ambiguous}}
124 struct D
{ int y
, x
; };
125 void j(A a
); // expected-note {{candidate}}
126 void j(D d
); // expected-note {{candidate}}
128 j({.x
= 1, .y
= 2}); // expected-error {{ambiguous}}
133 void l(E e
); // expected-note {{candidate}}
134 int &l(F f
); // expected-note {{candidate}}
136 int &r
= l({.a
= 0}); // ok, l(E) is not viable
137 int &s
= l({.a
= {0}}); // expected-error {{ambiguous}}
141 namespace deduction
{
142 struct A
{ int x
, y
; };
143 union B
{ int x
, y
; };
145 template<typename T
, typename U
> void f(decltype(T
{.x
= 1, .y
= 2}) = {});
146 template<typename T
, typename U
> void f(decltype(U
{.x
= 1, .y
= 2}) = {}) = delete;
147 void g() { f
<A
, B
>(); } // ok, calls non-union overload
149 struct C
{ int y
, x
; };
150 template<typename T
, typename U
> void h(decltype(T
{.y
= 1, .x
= 2}) = {}) = delete;
151 template<typename T
, typename U
> void h(decltype(U
{.y
= 1, .x
= 2}) = {});
153 h
<A
, C
>(); // ok, selects C overload by SFINAE
158 template<typename T
, typename U
> void j1(decltype(T
{.n
= 0}));
159 template<typename T
, typename U
> void j1(decltype(U
{.n
= 0})) = delete;
160 template<typename T
, typename U
> void j2(decltype(T
{.n
= {0}})); // expected-note {{candidate}}
161 template<typename T
, typename U
> void j2(decltype(U
{.n
= {0}})); // expected-note {{candidate}}
162 template<typename T
, typename U
> void j3(decltype(T
{.n
= {{0}}})) = delete;
163 template<typename T
, typename U
> void j3(decltype(U
{.n
= {{0}}}));
165 j1
<D
, E
>({}); // ok, selects D overload by SFINAE (too few braces for E)
166 j2
<D
, E
>({}); // expected-error {{ambiguous}}
167 j3
<D
, E
>({}); // ok, selects E overload by SFINAE (too many braces for D)
171 namespace no_unwrap
{
172 template<typename T
> struct X
{
173 static_assert(false, "should not be instantiated");
176 template<typename T
, typename U
= typename X
<T
>::type
> Q(T
&&);
179 // Ensure that we do not try to call 'Q::Q(.a = 1)' here.
180 void g() { Q q
= {.a
= 1}; } // expected-error {{initialization of non-aggregate type 'Q' with a designated initializer list}}
186 // OK, does not instantiate X<void&> (!).
204 struct : public A
, public B
{
215 // pedantic-note@-1 {{first non-designated initializer is here}}
216 // reorder-note@-2 {{previous initialization for field 'z' is here}}
217 .y
=1, // reorder-error {{field 'z' will be initialized after field 'y'}}
218 // reorder-note@-1 {{previous initialization for field 'y' is here}}
219 .x
=2}, // reorder-error {{field 'y' will be initialized after field 'x'}}
220 {.b
=3, // reorder-note {{previous initialization for field 'b' is here}}
221 .a
=4}, // reorder-error {{field 'b' will be initialized after field 'a'}}
222 .e
= 1, // reorder-note {{previous initialization for field 'e' is here}}
223 // pedantic-error@-1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
224 .d
= 1, // reorder-error {{field 'e' will be initialized after field 'd'}}
225 // reorder-note@-1 {{previous initialization for field 'd' is here}}
226 .c
= 1, // reorder-error {{field 'd' will be initialized after field 'c'}} // reorder-note {{previous initialization for field 'c' is here}}
227 .b
= 1, // reorder-error {{field 'c' will be initialized after field 'b'}} // reorder-note {{previous initialization for field 'b' is here}}
228 .a
= 1, // reorder-error {{field 'b' will be initialized after field 'a'}}
243 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 {{.*}})'}}
244 // reorder-note@-1 {{previous initialization for field 'x' is here}}
246 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'}}
247 // reorder-note-re@-1 {{previous initialization for field 'GH63759::C::(anonymous union at {{.*}})' is here}}