1 // RUN: %clang_cc1 -std=c++0x -fsyntax-only -fexceptions -verify %s
2 
3 struct one { char c[1]; };
4 struct two { char c[2]; };
5 
6 namespace std {
7   typedef decltype(sizeof(int)) size_t;
8 
9   // libc++'s implementation
10   template <class _E>
11   class initializer_list
12   {
13     const _E* __begin_;
14     size_t    __size_;
15 
initializer_list(const _E * __b,size_t __s)16     initializer_list(const _E* __b, size_t __s)
17       : __begin_(__b),
18         __size_(__s)
19     {}
20 
21   public:
22     typedef _E        value_type;
23     typedef const _E& reference;
24     typedef const _E& const_reference;
25     typedef size_t    size_type;
26 
27     typedef const _E* iterator;
28     typedef const _E* const_iterator;
29 
initializer_list()30     initializer_list() : __begin_(nullptr), __size_(0) {}
31 
size() const32     size_t    size()  const {return __size_;}
begin() const33     const _E* begin() const {return __begin_;}
end() const34     const _E* end()   const {return __begin_ + __size_;}
35   };
36 }
37 
38 namespace objects {
39 
40   struct X1 { X1(int); };
41   struct X2 { explicit X2(int); }; // expected-note {{constructor declared here}}
42 
43   template <int N>
44   struct A {
Aobjects::A45     A() { static_assert(N == 0, ""); }
Aobjects::A46     A(int, double) { static_assert(N == 1, ""); }
47   };
48 
49   template <int N>
50   struct F {
Fobjects::F51     F() { static_assert(N == 0, ""); }
Fobjects::F52     F(int, double) { static_assert(N == 1, ""); }
Fobjects::F53     F(std::initializer_list<int>) { static_assert(N == 3, ""); }
54   };
55 
56   template <int N>
57   struct D {
Dobjects::D58     D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}}
Dobjects::D59     D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}}
60   };
61 
62   template <int N>
63   struct E {
Eobjects::E64     E(int, int) { static_assert(N == 0, ""); }
Eobjects::E65     E(X1, int) { static_assert(N == 1, ""); }
66   };
67 
overload_resolution()68   void overload_resolution() {
69     { A<0> a{}; }
70     { A<0> a = {}; }
71     { A<1> a{1, 1.0}; }
72     { A<1> a = {1, 1.0}; }
73 
74     { F<0> f{}; }
75     { F<0> f = {}; }
76     // Narrowing conversions don't affect viability. The next two choose
77     // the initializer_list constructor.
78     { F<3> f{1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
79     { F<3> f = {1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
80     { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; }
81     { F<3> f = {1, 2, 3, 4, 5, 6, 7, 8}; }
82     { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; }
83     { F<3> f{1, 2}; }
84 
85     { D<0> d{1, 2, 3}; }
86     { D<1> d{1.0, 2.0, 3.0}; }
87     { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}}
88 
89     { E<0> e{1, 2}; }
90   }
91 
explicit_implicit()92   void explicit_implicit() {
93     { X1 x{0}; }
94     { X1 x = {0}; }
95     { X2 x{0}; }
96     { X2 x = {0}; } // expected-error {{constructor is explicit}}
97   }
98 
99   struct C {
100     C();
101     C(int, double);
102     C(int, int);
103 
104     int operator[](C);
105   };
106 
function_call()107   C function_call() {
108     void takes_C(C);
109     takes_C({1, 1.0});
110 
111     C c;
112     c[{1, 1.0}];
113 
114     return {1, 1.0};
115   }
116 
inline_init()117   void inline_init() {
118     (void) C{1, 1.0};
119     (void) new C{1, 1.0};
120     (void) A<1>{1, 1.0};
121     (void) new A<1>{1, 1.0};
122   }
123 
124   struct B { // expected-note 2 {{candidate constructor}}
125     B(C, int, C); // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'objects::C'}}
126   };
127 
nested_init()128   void nested_init() {
129     B b1{{1, 1.0}, 2, {3, 4}};
130     B b2{{1, 1.0, 4}, 2, {3, 4}}; // expected-error {{no matching constructor for initialization of 'objects::B'}}
131   }
132 
overloaded_call()133   void overloaded_call() {
134     one ov1(B); // expected-note {{not viable: cannot convert initializer list}}
135     two ov1(C); // expected-note {{not viable: cannot convert initializer list}}
136 
137     static_assert(sizeof(ov1({})) == sizeof(two), "bad overload");
138     static_assert(sizeof(ov1({1, 2})) == sizeof(two), "bad overload");
139     static_assert(sizeof(ov1({{1, 1.0}, 2, {3, 4}})) == sizeof(one), "bad overload");
140 
141     ov1({1}); // expected-error {{no matching function}}
142 
143     one ov2(int);
144     two ov2(F<3>);
145     // expected-warning@+1 {{braces around scalar initializer}}
146     static_assert(sizeof(ov2({1})) == sizeof(one), "bad overload"); // list -> int ranks as identity
147     static_assert(sizeof(ov2({1, 2, 3})) == sizeof(two), "bad overload"); // list -> F only viable
148   }
149 
150   struct G { // expected-note 6 {{not viable}}
151     // This is not an initializer-list constructor.
152     template<typename ...T>
153     G(std::initializer_list<int>, T ...);  // expected-note 3 {{not viable}}
154   };
155 
156   struct H { // expected-note 6 {{not viable}}
157     explicit H(int, int); // expected-note 3 {{not viable}} expected-note {{declared here}}
158     H(int, void*); // expected-note 3 {{not viable}}
159   };
160 
edge_cases()161   void edge_cases() {
162     // invalid (the first phase only considers init-list ctors)
163     // (for the second phase, no constructor is viable)
164     G g1{1, 2, 3}; // expected-error {{no matching constructor}}
165     (void) new G{1, 2, 3}; // expected-error {{no matching constructor}}
166     (void) G{1, 2, 3} // expected-error {{no matching constructor}}
167 
168     // valid (T deduced to <>).
169     G g2({1, 2, 3});
170     (void) new G({1, 2, 3});
171     (void) G({1, 2, 3});
172 
173     // invalid
174     H h1({1, 2}); // expected-error {{no matching constructor}}
175     (void) new H({1, 2}); // expected-error {{no matching constructor}}
176     // FIXME: Bad diagnostic, mentions void type instead of init list.
177     (void) H({1, 2}); // expected-error {{no matching conversion}}
178 
179     // valid (by copy constructor).
180     H h2({1, nullptr});
181     (void) new H({1, nullptr});
182     (void) H({1, nullptr});
183 
184     // valid
185     H h3{1, 2};
186     (void) new H{1, 2};
187     (void) H{1, 2};
188   }
189 
190   struct memberinit {
191     H h1{1, nullptr};
192     H h2 = {1, nullptr};
193     H h3{1, 1};
194     H h4 = {1, 1}; // expected-error {{constructor is explicit}}
195   };
196 }
197 
198 namespace PR12092 {
199 
200   struct S {
201     S(const char*);
202   };
203   struct V {
204     template<typename T> V(T, T);
205     void f(std::initializer_list<S>);
206     void f(const V &);
207   };
208 
g()209   void g() {
210     extern V s;
211     s.f({"foo", "bar"});
212   }
213 
214 }
215 
216 namespace PR12117 {
217   struct A { A(int); };
218   struct B { B(A); } b{{0}};   //FIXME: non-conformant. Temporary fix until standard resolution.
219                                 // expected- error {{call to constructor of 'struct B' is ambiguous}} \
220                                 // expected- note 2{{candidate is the implicit}} \
221                                 // expected- note {{candidate constructor}}
222   struct C { C(int); } c{0};
223 }
224 
225 namespace PR12167 {
226   template<int N> struct string {};
227 
228   struct X {
229     X(const char v);
230     template<typename T> bool operator()(T) const;
231   };
232 
g(const string<N> & s,Comparator cmp)233   template<int N, class Comparator> bool g(const string<N>& s, Comparator cmp) {
234     return cmp(s);
235   }
f(const string<N> & s)236   template<int N> bool f(const string<N> &s) {
237     return g(s, X{'x'});
238   }
239 
240   bool s = f(string<1>());
241 }
242 
243 namespace PR12257_PR12241 {
244   struct command_pair
245   {
246     command_pair(int, int);
247   };
248 
249   struct command_map
250   {
251     command_map(std::initializer_list<command_pair>);
252   };
253 
254   struct generator_pair
255   {
256     generator_pair(const command_map);
257   };
258 
259   // 5 levels: init list, gen_pair, command_map, init list, command_pair
260   const std::initializer_list<generator_pair> x = {{{{{3, 4}}}}};
261 
262   // 4 levels: init list, gen_pair, command_map via init list, command_pair
263   const std::initializer_list<generator_pair> y = {{{{1, 2}}}};
264 }
265 
266 namespace PR12120 {
267   struct A { explicit A(int); A(float); }; // expected-note {{declared here}}
268   A a = { 0 }; // expected-error {{constructor is explicit}}
269 
270   struct B { explicit B(short); B(long); }; // expected-note 2 {{candidate}}
271   B b = { 0 }; // expected-error {{ambiguous}}
272 }
273 
274 namespace PR12498 {
275   class ArrayRef; // expected-note{{forward declaration}}
276 
277   struct C {
278     void foo(const ArrayRef&); // expected-note{{passing argument to parameter here}}
279   };
280 
bar(C * c)281   static void bar(C* c)
282   {
283     c->foo({ nullptr, 1 }); // expected-error{{initialization of incomplete type 'const PR12498::ArrayRef'}}
284   }
285 }
286 
287 namespace explicit_default {
288   struct A {
289     explicit A(); // expected-note{{here}}
290   };
291   A a {}; // ok
292   // This is copy-list-initialization, and we choose an explicit constructor
293   // (even though we do so via value-initialization), so the initialization is
294   // ill-formed.
295   A b = {}; // expected-error{{chosen constructor is explicit}}
296 }
297 
298 namespace init_list_default {
299   struct A {
300     A(std::initializer_list<int>);
301   };
302   A a {}; // calls initializer list constructor
303 
304   struct B {
305     B();
306     B(std::initializer_list<int>) = delete;
307   };
308   B b {}; // calls default constructor
309 }
310 
311 // PR13470, <rdar://problem/11974632>
312 namespace PR13470 {
313   struct W {
314     explicit W(int); // expected-note {{here}}
315   };
316 
317   struct X {
318     X(const X&) = delete; // expected-note 3 {{here}}
319     X(int);
320   };
321 
call(Fn f)322   template<typename T, typename Fn> void call(Fn f) {
323     f({1}); // expected-error {{constructor is explicit}}
324     f(T{1}); // expected-error {{call to deleted constructor}}
325   }
326 
327   void ref_w(const W &); // expected-note 2 {{not viable}}
call_ref_w()328   void call_ref_w() {
329     ref_w({1}); // expected-error {{no matching function}}
330     ref_w(W{1});
331     call<W>(ref_w); // expected-note {{instantiation of}}
332   }
333 
334   void ref_x(const X &);
call_ref_x()335   void call_ref_x() {
336     ref_x({1});
337     ref_x(X{1});
338     call<X>(ref_x); // ok
339   }
340 
341   void val_x(X); // expected-note 2 {{parameter}}
call_val_x()342   void call_val_x() {
343     val_x({1});
344     val_x(X{1}); // expected-error {{call to deleted constructor}}
345     call<X>(val_x); // expected-note {{instantiation of}}
346   }
347 
348   template<typename T>
349   struct Y {
350     X x{1};
fPR13470::Y351     void f() { X x{1}; }
hPR13470::Y352     void h() {
353       ref_w({1}); // expected-error {{no matching function}}
354       ref_w(W{1});
355       ref_x({1});
356       ref_x(X{1});
357       val_x({1});
358       val_x(X{1}); // expected-error {{call to deleted constructor}}
359     }
YPR13470::Y360     Y() {}
YPR13470::Y361     Y(int) : x{1} {}
362   };
363 
364   Y<int> yi;
365   Y<int> yi2(0);
g()366   void g() {
367     yi.f();
368     yi.h(); // ok, all diagnostics produced in template definition
369   }
370 }
371 
372 namespace PR19729 {
373   struct A {
374     A(int);
375     A(const A&) = delete;
376   };
377   struct B {
378     void *operator new(std::size_t, A);
379   };
380   B *p = new ({123}) B;
381 }
382 
383 namespace PR11410 {
384   struct A {
385     A() = delete; // expected-note 2{{deleted here}}
386     A(int);
387   };
388 
389   A a[3] = {
390     {1}, {2}
391   }; // expected-error {{call to deleted constructor}} \
392         expected-note {{in implicit initialization of array element 2 with omitted initializer}}
393 
394   struct B {
395     A a; // expected-note {{in implicit initialization of field 'a'}}
396   } b = {
397   }; // expected-error {{call to deleted constructor}}
398 
399   struct C {
400     C(int = 0); // expected-note 2{{candidate}}
401     C(float = 0); // expected-note 2{{candidate}}
402   };
403   C c[3] = {
404     0, 1
405   }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 2}}
406   C c2[3] = {
407     [0] = 1, [2] = 3
408   }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 1}}
409 }
410