1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*67e74705SXin Li
3*67e74705SXin Li // This must obviously come before the definition of std::initializer_list.
missing_initializerlist()4*67e74705SXin Li void missing_initializerlist() {
5*67e74705SXin Li auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}
6*67e74705SXin Li }
7*67e74705SXin Li
8*67e74705SXin Li namespace std {
9*67e74705SXin Li typedef decltype(sizeof(int)) size_t;
10*67e74705SXin Li
11*67e74705SXin Li // libc++'s implementation
12*67e74705SXin Li template <class _E>
13*67e74705SXin Li class initializer_list
14*67e74705SXin Li {
15*67e74705SXin Li const _E* __begin_;
16*67e74705SXin Li size_t __size_;
17*67e74705SXin Li
initializer_list(const _E * __b,size_t __s)18*67e74705SXin Li initializer_list(const _E* __b, size_t __s)
19*67e74705SXin Li : __begin_(__b),
20*67e74705SXin Li __size_(__s)
21*67e74705SXin Li {}
22*67e74705SXin Li
23*67e74705SXin Li public:
24*67e74705SXin Li typedef _E value_type;
25*67e74705SXin Li typedef const _E& reference;
26*67e74705SXin Li typedef const _E& const_reference;
27*67e74705SXin Li typedef size_t size_type;
28*67e74705SXin Li
29*67e74705SXin Li typedef const _E* iterator;
30*67e74705SXin Li typedef const _E* const_iterator;
31*67e74705SXin Li
initializer_list()32*67e74705SXin Li initializer_list() : __begin_(nullptr), __size_(0) {}
33*67e74705SXin Li
size() const34*67e74705SXin Li size_t size() const {return __size_;}
begin() const35*67e74705SXin Li const _E* begin() const {return __begin_;}
end() const36*67e74705SXin Li const _E* end() const {return __begin_ + __size_;}
37*67e74705SXin Li };
38*67e74705SXin Li }
39*67e74705SXin Li
40*67e74705SXin Li template <typename T, typename U>
41*67e74705SXin Li struct same_type { static const bool value = false; };
42*67e74705SXin Li template <typename T>
43*67e74705SXin Li struct same_type<T, T> { static const bool value = true; };
44*67e74705SXin Li
45*67e74705SXin Li struct one { char c[1]; };
46*67e74705SXin Li struct two { char c[2]; };
47*67e74705SXin Li
48*67e74705SXin Li struct A {
49*67e74705SXin Li int a, b;
50*67e74705SXin Li };
51*67e74705SXin Li
52*67e74705SXin Li struct B {
53*67e74705SXin Li B();
54*67e74705SXin Li B(int, int);
55*67e74705SXin Li };
56*67e74705SXin Li
simple_list()57*67e74705SXin Li void simple_list() {
58*67e74705SXin Li std::initializer_list<int> il = { 1, 2, 3 };
59*67e74705SXin Li std::initializer_list<double> dl = { 1.0, 2.0, 3 };
60*67e74705SXin Li std::initializer_list<A> al = { {1, 2}, {2, 3}, {3, 4} };
61*67e74705SXin Li std::initializer_list<B> bl = { {1, 2}, {2, 3}, {} };
62*67e74705SXin Li }
63*67e74705SXin Li
function_call()64*67e74705SXin Li void function_call() {
65*67e74705SXin Li void f(std::initializer_list<int>);
66*67e74705SXin Li f({1, 2, 3});
67*67e74705SXin Li
68*67e74705SXin Li void g(std::initializer_list<B>);
69*67e74705SXin Li g({ {1, 2}, {2, 3}, {} });
70*67e74705SXin Li }
71*67e74705SXin Li
72*67e74705SXin Li struct C {
73*67e74705SXin Li C(int);
74*67e74705SXin Li };
75*67e74705SXin Li
76*67e74705SXin Li struct D {
77*67e74705SXin Li D();
78*67e74705SXin Li operator int();
79*67e74705SXin Li operator C();
80*67e74705SXin Li };
81*67e74705SXin Li
overloaded_call()82*67e74705SXin Li void overloaded_call() {
83*67e74705SXin Li one overloaded(std::initializer_list<int>);
84*67e74705SXin Li two overloaded(std::initializer_list<B>);
85*67e74705SXin Li
86*67e74705SXin Li static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one), "bad overload");
87*67e74705SXin Li static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two), "bad overload");
88*67e74705SXin Li
89*67e74705SXin Li void ambiguous(std::initializer_list<A>); // expected-note {{candidate}}
90*67e74705SXin Li void ambiguous(std::initializer_list<B>); // expected-note {{candidate}}
91*67e74705SXin Li ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}}
92*67e74705SXin Li
93*67e74705SXin Li one ov2(std::initializer_list<int>); // expected-note {{candidate}}
94*67e74705SXin Li two ov2(std::initializer_list<C>); // expected-note {{candidate}}
95*67e74705SXin Li // Worst sequence to int is identity, whereas to C it's user-defined.
96*67e74705SXin Li static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one), "bad overload");
97*67e74705SXin Li // But here, user-defined is worst in both cases.
98*67e74705SXin Li ov2({1, 2, D()}); // expected-error {{ambiguous}}
99*67e74705SXin Li }
100*67e74705SXin Li
101*67e74705SXin Li template <typename T>
102*67e74705SXin Li T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
103*67e74705SXin Li template <typename T>
104*67e74705SXin Li T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
105*67e74705SXin Li
argument_deduction()106*67e74705SXin Li void argument_deduction() {
107*67e74705SXin Li static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction");
108*67e74705SXin Li static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction");
109*67e74705SXin Li
110*67e74705SXin Li deduce({1, 2.0}); // expected-error {{no matching function}}
111*67e74705SXin Li
112*67e74705SXin Li static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction");
113*67e74705SXin Li static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction");
114*67e74705SXin Li
115*67e74705SXin Li deduce_ref({1, 2.0}); // expected-error {{no matching function}}
116*67e74705SXin Li }
117*67e74705SXin Li
auto_deduction()118*67e74705SXin Li void auto_deduction() {
119*67e74705SXin Li auto l = {1, 2, 3, 4};
120*67e74705SXin Li auto l2 {1, 2, 3, 4}; // expected-error {{initializer for variable 'l2' with type 'auto' contains multiple expressions}}
121*67e74705SXin Li auto l3 {1};
122*67e74705SXin Li static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
123*67e74705SXin Li static_assert(same_type<decltype(l3), int>::value, "");
124*67e74705SXin Li auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
125*67e74705SXin Li
126*67e74705SXin Li for (int i : {1, 2, 3, 4}) {}
127*67e74705SXin Li }
128*67e74705SXin Li
dangle()129*67e74705SXin Li void dangle() {
130*67e74705SXin Li new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}}
131*67e74705SXin Li new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
132*67e74705SXin Li }
133*67e74705SXin Li
134*67e74705SXin Li struct haslist1 {
135*67e74705SXin Li std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}}
136*67e74705SXin Li std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}}
137*67e74705SXin Li haslist1();
138*67e74705SXin Li };
139*67e74705SXin Li
haslist1()140*67e74705SXin Li haslist1::haslist1()
141*67e74705SXin Li : il{1, 2, 3} // expected-warning{{at the end of the constructor}}
142*67e74705SXin Li {}
143*67e74705SXin Li
144*67e74705SXin Li namespace PR12119 {
145*67e74705SXin Li // Deduction with nested initializer lists.
146*67e74705SXin Li template<typename T> void f(std::initializer_list<T>);
147*67e74705SXin Li template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
148*67e74705SXin Li
foo()149*67e74705SXin Li void foo() {
150*67e74705SXin Li f({0, {1}}); // expected-warning{{braces around scalar initializer}}
151*67e74705SXin Li g({{0, 1}, {2, 3}});
152*67e74705SXin Li std::initializer_list<int> il = {1, 2};
153*67e74705SXin Li g({il, {2, 3}});
154*67e74705SXin Li }
155*67e74705SXin Li }
156*67e74705SXin Li
157*67e74705SXin Li namespace Decay {
158*67e74705SXin Li template<typename T>
f(std::initializer_list<T>)159*67e74705SXin Li void f(std::initializer_list<T>) {
160*67e74705SXin Li T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
161*67e74705SXin Li }
162*67e74705SXin Li
g()163*67e74705SXin Li void g() {
164*67e74705SXin Li f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
165*67e74705SXin Li
166*67e74705SXin Li auto x = { "A", "BB", "CCC" };
167*67e74705SXin Li std::initializer_list<const char *> *il = &x;
168*67e74705SXin Li
169*67e74705SXin Li for( auto s : {"A", "BB", "CCC", "DDD"}) { }
170*67e74705SXin Li }
171*67e74705SXin Li }
172*67e74705SXin Li
173*67e74705SXin Li namespace PR12436 {
174*67e74705SXin Li struct X {
175*67e74705SXin Li template<typename T>
176*67e74705SXin Li X(std::initializer_list<int>, T);
177*67e74705SXin Li };
178*67e74705SXin Li
179*67e74705SXin Li X x({}, 17);
180*67e74705SXin Li }
181*67e74705SXin Li
182*67e74705SXin Li namespace rdar11948732 {
183*67e74705SXin Li template<typename T> struct X {};
184*67e74705SXin Li
185*67e74705SXin Li struct XCtorInit {
186*67e74705SXin Li XCtorInit(std::initializer_list<X<int>>);
187*67e74705SXin Li };
188*67e74705SXin Li
f(X<int> & xi)189*67e74705SXin Li void f(X<int> &xi) {
190*67e74705SXin Li XCtorInit xc = { xi, xi };
191*67e74705SXin Li }
192*67e74705SXin Li }
193*67e74705SXin Li
194*67e74705SXin Li namespace PR14272 {
195*67e74705SXin Li auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
196*67e74705SXin Li }
197*67e74705SXin Li
198*67e74705SXin Li namespace initlist_of_array {
f(std::initializer_list<int[2]>)199*67e74705SXin Li void f(std::initializer_list<int[2]>) {}
200*67e74705SXin Li void f(std::initializer_list<int[2][2]>) = delete;
h()201*67e74705SXin Li void h() {
202*67e74705SXin Li f({{1,2},{3,4}});
203*67e74705SXin Li }
204*67e74705SXin Li }
205*67e74705SXin Li
206*67e74705SXin Li namespace init_list_deduction_failure {
207*67e74705SXin Li void f();
208*67e74705SXin Li void f(int);
209*67e74705SXin Li template<typename T> void g(std::initializer_list<T>);
210*67e74705SXin Li // expected-note@-1 {{candidate template ignored: couldn't resolve reference to overloaded function 'f'}}
h()211*67e74705SXin Li void h() { g({f}); }
212*67e74705SXin Li // expected-error@-1 {{no matching function for call to 'g'}}
213*67e74705SXin Li }
214*67e74705SXin Li
215*67e74705SXin Li namespace deleted_copy {
216*67e74705SXin Li struct X {
Xdeleted_copy::X217*67e74705SXin Li X(int i) {}
218*67e74705SXin Li X(const X& x) = delete; // expected-note {{here}}
219*67e74705SXin Li void operator=(const X& x) = delete;
220*67e74705SXin Li };
221*67e74705SXin Li
222*67e74705SXin Li std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}
223*67e74705SXin Li }
224*67e74705SXin Li
225*67e74705SXin Li namespace RefVersusInitList {
226*67e74705SXin Li struct S {};
227*67e74705SXin Li void f(const S &) = delete;
228*67e74705SXin Li void f(std::initializer_list<S>);
g(S s)229*67e74705SXin Li void g(S s) { f({S()}); }
230*67e74705SXin Li }
231*67e74705SXin Li
232*67e74705SXin Li namespace PR18013 {
233*67e74705SXin Li int f();
234*67e74705SXin Li std::initializer_list<long (*)()> x = {f}; // expected-error {{cannot initialize an array element of type 'long (*const)()' with an lvalue of type 'int ()': different return type ('long' vs 'int')}}
235*67e74705SXin Li }
236*67e74705SXin Li
237*67e74705SXin Li namespace DR1070 {
238*67e74705SXin Li struct S {
239*67e74705SXin Li S(std::initializer_list<int>);
240*67e74705SXin Li };
241*67e74705SXin Li S s[3] = { {1, 2, 3}, {4, 5} }; // ok
242*67e74705SXin Li S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok
243*67e74705SXin Li }
244*67e74705SXin Li
245*67e74705SXin Li namespace ListInitInstantiate {
246*67e74705SXin Li struct A {
247*67e74705SXin Li A(std::initializer_list<A>);
248*67e74705SXin Li A(std::initializer_list<int>);
249*67e74705SXin Li };
250*67e74705SXin Li struct B : A {
251*67e74705SXin Li B(int);
252*67e74705SXin Li };
253*67e74705SXin Li template<typename T> struct X {
254*67e74705SXin Li X();
255*67e74705SXin Li A a;
256*67e74705SXin Li };
X()257*67e74705SXin Li template<typename T> X<T>::X() : a{B{0}, B{1}} {}
258*67e74705SXin Li
259*67e74705SXin Li X<int> x;
260*67e74705SXin Li
261*67e74705SXin Li int f(const A&);
g()262*67e74705SXin Li template<typename T> void g() { int k = f({0}); }
263*67e74705SXin Li template void g<int>();
264*67e74705SXin Li }
265*67e74705SXin Li
266*67e74705SXin Li namespace TemporaryInitListSourceRange_PR22367 {
267*67e74705SXin Li struct A {
ATemporaryInitListSourceRange_PR22367::A268*67e74705SXin Li constexpr A() {}
269*67e74705SXin Li A(std::initializer_list<int>); // expected-note {{here}}
270*67e74705SXin Li };
f(A)271*67e74705SXin Li constexpr int f(A) { return 0; }
272*67e74705SXin Li constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
273*67e74705SXin Li // The point of this test is to check that the caret points to
274*67e74705SXin Li // 'std::initializer_list', not to '{0}'.
275*67e74705SXin Li std::initializer_list // expected-note {{constructor}}
276*67e74705SXin Li <int>
277*67e74705SXin Li {0}
278*67e74705SXin Li );
279*67e74705SXin Li }
280*67e74705SXin Li
281*67e74705SXin Li namespace ParameterPackNestedInitializerLists_PR23904c3 {
282*67e74705SXin Li template <typename ...T>
283*67e74705SXin Li void f(std::initializer_list<std::initializer_list<T>> ...tt);
284*67e74705SXin Li
foo()285*67e74705SXin Li void foo() { f({{0}}, {{'\0'}}); }
286*67e74705SXin Li }
287*67e74705SXin Li
288*67e74705SXin Li namespace update_rbrace_loc_crash {
289*67e74705SXin Li // We used to crash-on-invalid on this example when updating the right brace
290*67e74705SXin Li // location.
291*67e74705SXin Li template <typename T, T>
292*67e74705SXin Li struct A {};
293*67e74705SXin Li template <typename T, typename F, int... I>
ExplodeImpl(F p1,A<int,I...>)294*67e74705SXin Li std::initializer_list<T> ExplodeImpl(F p1, A<int, I...>) {
295*67e74705SXin Li // expected-error@+1 {{reference to type 'const update_rbrace_loc_crash::Incomplete' could not bind to an rvalue of type 'void'}}
296*67e74705SXin Li return {p1(I)...};
297*67e74705SXin Li }
298*67e74705SXin Li template <typename T, int N, typename F>
Explode(F p1)299*67e74705SXin Li void Explode(F p1) {
300*67e74705SXin Li // expected-note@+1 {{in instantiation of function template specialization}}
301*67e74705SXin Li ExplodeImpl<T>(p1, A<int, N>());
302*67e74705SXin Li }
303*67e74705SXin Li class Incomplete;
304*67e74705SXin Li struct ContainsIncomplete {
305*67e74705SXin Li const Incomplete &obstacle;
306*67e74705SXin Li };
f()307*67e74705SXin Li void f() {
308*67e74705SXin Li // expected-note@+1 {{in instantiation of function template specialization}}
309*67e74705SXin Li Explode<ContainsIncomplete, 4>([](int) {});
310*67e74705SXin Li }
311*67e74705SXin Li }
312