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 
8 
9 namespace class_with_ctor {
10   struct A { // cxx20-note 6{{candidate}}
11     A() = default; // cxx20-note 3{{candidate}}
12     int x;
13     int y;
14   };
15   A a = {1, 2}; // cxx20-error {{no matching constructor}}
16 
17   struct B {
18     int x;
19     int y;
20   };
21   B b1 = B(); // trigger declaration of implicit ctors
22   B b2 = {1, 2}; // ok
23 
24   struct C : A {
25     A a;
26   };
27   C c1 = {{}, {}}; // ok, call default ctor twice
28   C c2 = {{1, 2}, {3, 4}}; // cxx20-error 2{{no matching constructor}}
29 }
30 
31 namespace designator {
32 struct A { int x, y; };
33 struct B { A a; };
34 
35 A a1 = {
36   .y = 1, // reorder-note {{previous initialization for field 'y' is here}}
37   .x = 2 // reorder-error {{ISO C++ requires field designators to be specified in declaration order; field 'y' will be initialized after field 'x'}}
38 };
39 int arr[3] = {[1] = 5}; // pedantic-error {{array designators are a C99 extension}}
40 B b = {.a.x = 0}; // pedantic-error {{nested designators are a C99 extension}}
41 A a2 = {
42   .x = 1, // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
43   2 // pedantic-note {{first non-designated initializer is here}}
44 };
45 A a3 = {
46   1, // pedantic-note {{first non-designated initializer is here}}
47   .y = 2 // pedantic-error {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
48 };
49 A a4 = {
50   .x = 1, // override-note {{previous}}
51   .x = 1 // override-error {{overrides prior initialization}}
52 };
53 A a5 = {
54   .y = 1, // override-note {{previous}}
55   .y = 1 // override-error {{overrides prior initialization}}
56 };
57 B b2 = {.a = 1}; // pedantic-error {{brace elision for designated initializer is a C99 extension}}
58 B b3 = {.a = 1, 2}; // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{first non-designated}} pedantic-error {{brace elision}}
59 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}}
60 B b5 = {.a = nullptr}; // expected-error {{cannot initialize}}
61 struct C { int :0, x, :0, y, :0; };
62 C c = {
63   .x = 1, // override-note {{previous}}
64   .x = 1, // override-error {{overrides prior initialization}} override-note {{previous}}
65   .y = 1, // override-note {{previous}}
66   .y = 1, // override-error {{overrides prior initialization}}
67   .x = 1, // reorder-error {{declaration order}} override-error {{overrides prior initialization}} override-note {{previous}}
68   .x = 1, // override-error {{overrides prior initialization}}
69 };
70 }
71 
72 namespace base_class {
73   struct base {
74     int x;
75   };
76   struct derived : base {
77     int y;
78   };
79   derived d = {.x = 1, .y = 2}; // expected-error {{'x' does not refer to any field}}
80 }
81 
82 namespace union_ {
83   union U { int a, b; };
84   U u = {
85     .a = 1, // override-note {{here}}
86     .b = 2, // override-error {{overrides prior}}
87   };
88 }
89 
90 namespace overload_resolution {
91   struct A { int x, y; };
92   union B { int x, y; };
93 
94   void f(A a);
95   void f(B b) = delete;
g()96   void g() { f({.x = 1, .y = 2}); } // ok, calls non-union overload
97 
98   // As an extension of the union case, overload resolution won't pick any
99   // candidate where a field initializer would be overridden.
100   struct A2 { int x, other, y; };
101   int f(A2);
g2()102   void g2() { int k = f({.x = 1, 2, .y = 3}); (void)k; } // pedantic-error {{mixture of designated and non-designated}} pedantic-note {{here}}
103 
104   struct C { int x; };
105   void h(A a); // expected-note {{candidate}}
106   void h(C c); // expected-note {{candidate}}
i()107   void i() {
108     h({.x = 1, .y = 2});
109     h({.y = 1, .x = 2}); // reorder-error {{declaration order}} reorder-note {{previous}}
110     h({.x = 1}); // expected-error {{ambiguous}}
111   }
112 
113   struct D { int y, x; };
114   void j(A a); // expected-note {{candidate}}
115   void j(D d); // expected-note {{candidate}}
k()116   void k() {
117     j({.x = 1, .y = 2}); // expected-error {{ambiguous}}
118   }
119 
120   struct E { A a; };
121   struct F { int a; };
122   void l(E e); // expected-note {{candidate}}
123   int &l(F f); // expected-note {{candidate}}
m()124   void m() {
125     int &r = l({.a = 0}); // ok, l(E) is not viable
126     int &s = l({.a = {0}}); // expected-error {{ambiguous}}
127   }
128 }
129 
130 namespace deduction {
131   struct A { int x, y; };
132   union B { int x, y; };
133 
134   template<typename T, typename U> void f(decltype(T{.x = 1, .y = 2}) = {});
135   template<typename T, typename U> void f(decltype(U{.x = 1, .y = 2}) = {}) = delete;
g()136   void g() { f<A, B>(); } // ok, calls non-union overload
137 
138   struct C { int y, x; };
139   template<typename T, typename U> void h(decltype(T{.y = 1, .x = 2}) = {}) = delete;
140   template<typename T, typename U> void h(decltype(U{.y = 1, .x = 2}) = {});
i()141   void i() {
142     h<A, C>(); // ok, selects C overload by SFINAE
143   }
144 
145   struct D { int n; };
146   struct E { D n; };
147   template<typename T, typename U> void j1(decltype(T{.n = 0}));
148   template<typename T, typename U> void j1(decltype(U{.n = 0})) = delete;
149   template<typename T, typename U> void j2(decltype(T{.n = {0}})); // expected-note {{candidate}}
150   template<typename T, typename U> void j2(decltype(U{.n = {0}})); // expected-note {{candidate}}
151   template<typename T, typename U> void j3(decltype(T{.n = {{0}}})) = delete;
152   template<typename T, typename U> void j3(decltype(U{.n = {{0}}}));
k()153   void k() {
154     j1<D, E>({}); // ok, selects D overload by SFINAE (too few braces for E)
155     j2<D, E>({}); // expected-error {{ambiguous}}
156     j3<D, E>({}); // ok, selects E overload by SFINAE (too many braces for D)
157   }
158 }
159