[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / cxx0x-initializer-stdinitializerlist.cpp
blob41b4f6dc308d9d9560e5ce4f3a41daeea54d67ec
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -DUNION_TEST -verify %s
5 #ifdef UNION_TEST
6 namespace std {
7 template<class E>
8 union initializer_list {
9 // expected-error@-1 {{'std::initializer_list<int>' layout not recognized.}}
10 const E* begin;
11 decltype(sizeof 0) size;
14 auto x = { 1, 2, 3 };
16 #else
17 // This must obviously come before the definition of std::initializer_list.
18 void missing_initializerlist() {
19 auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}
22 namespace std {
23 typedef decltype(sizeof(int)) size_t;
25 // libc++'s implementation
26 template <class _E>
27 class initializer_list
29 const _E* __begin_;
30 size_t __size_;
32 initializer_list(const _E* __b, size_t __s)
33 : __begin_(__b),
34 __size_(__s)
37 public:
38 typedef _E value_type;
39 typedef const _E& reference;
40 typedef const _E& const_reference;
41 typedef size_t size_type;
43 typedef const _E* iterator;
44 typedef const _E* const_iterator;
46 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
48 constexpr size_t size() const {return __size_;}
49 const _E* begin() const {return __begin_;}
50 const _E* end() const {return __begin_ + __size_;}
54 template <typename T, typename U>
55 struct same_type { static const bool value = false; };
56 template <typename T>
57 struct same_type<T, T> { static const bool value = true; };
59 struct one { char c[1]; };
60 struct two { char c[2]; };
62 struct A {
63 int a, b;
66 struct B {
67 B();
68 B(int, int);
71 void simple_list() {
72 std::initializer_list<int> il = { 1, 2, 3 };
73 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
74 std::initializer_list<A> al = { {1, 2}, {2, 3}, {3, 4} };
75 std::initializer_list<B> bl = { {1, 2}, {2, 3}, {} };
78 void function_call() {
79 void f(std::initializer_list<int>);
80 f({1, 2, 3});
82 void g(std::initializer_list<B>);
83 g({ {1, 2}, {2, 3}, {} });
86 struct C {
87 C(int);
90 struct D {
91 D();
92 operator int();
93 operator C();
96 void overloaded_call() {
97 one overloaded(std::initializer_list<int>);
98 two overloaded(std::initializer_list<B>);
100 static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one), "bad overload");
101 static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two), "bad overload");
103 void ambiguous(std::initializer_list<A>); // expected-note {{candidate}}
104 void ambiguous(std::initializer_list<B>); // expected-note {{candidate}}
105 ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}}
107 one ov2(std::initializer_list<int>); // expected-note {{candidate}}
108 two ov2(std::initializer_list<C>); // expected-note {{candidate}}
109 // Worst sequence to int is identity, whereas to C it's user-defined.
110 static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one), "bad overload");
111 // But here, user-defined is worst in both cases.
112 ov2({1, 2, D()}); // expected-error {{ambiguous}}
115 template <typename T>
116 T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
117 template <typename T>
118 T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
120 template<typename T, typename U> struct pair { pair(...); };
121 template<typename T> void deduce_pairs(std::initializer_list<pair<T, typename T::type>>);
122 // expected-note@-1 {{deduced type 'pair<[...], typename WithIntType::type>' of element of 1st parameter does not match adjusted type 'pair<[...], float>' of element of argument [with T = WithIntType]}}
123 struct WithIntType { typedef int type; };
125 template<typename ...T> void deduce_after_init_list_in_pack(void (*)(T...), T...); // expected-note {{<int, int> vs. <(no value), double>}}
127 void argument_deduction() {
128 static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction");
129 static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction");
131 deduce({1, 2.0}); // expected-error {{no matching function}}
133 static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction");
134 static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction");
136 deduce_ref({1, 2.0}); // expected-error {{no matching function}}
138 pair<WithIntType, int> pi;
139 pair<WithIntType, float> pf;
140 deduce_pairs({pi, pi, pi}); // ok
141 deduce_pairs({pi, pf, pi}); // expected-error {{no matching function}}
143 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0);
144 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0.0); // expected-error {{no matching function}}
147 void auto_deduction() {
148 auto l = {1, 2, 3, 4};
149 auto l2 {1, 2, 3, 4}; // expected-error {{initializer for variable 'l2' with type 'auto' contains multiple expressions}}
150 auto l3 {1};
151 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
152 static_assert(same_type<decltype(l3), int>::value, "");
153 auto bl = {1, 2.0}; // expected-error {{deduced conflicting types ('int' vs 'double') for initializer list element type}}
155 void f1(int), f1(float), f2(int), f3(float);
156 auto fil = {f1, f2};
157 auto ffl = {f1, f3};
158 auto fl = {f1, f2, f3}; // expected-error {{deduced conflicting types ('void (*)(int)' vs 'void (*)(float)') for initializer list element type}}
160 for (int i : {1, 2, 3, 4}) {}
161 for (int j : {1.0, 2.0, 3.0f, 4.0}) {} // expected-error {{deduced conflicting types ('double' vs 'float') for initializer list element type}}
164 void dangle() {
165 new auto{1, 2, 3}; // expected-error {{new expression for type 'auto' contains multiple constructor arguments}}
166 new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
169 struct haslist1 {
170 std::initializer_list<int> il // expected-note {{declared here}}
171 = {1, 2, 3}; // ok, unused
172 std::initializer_list<int> jl{1, 2, 3}; // expected-note {{default member init}}
173 haslist1();
176 haslist1::haslist1() // expected-error {{backing array for 'std::initializer_list' member 'jl' is a temporary object}}
177 : il{1, 2, 3} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
180 namespace PR12119 {
181 // Deduction with nested initializer lists.
182 template<typename T> void f(std::initializer_list<T>);
183 template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
185 void foo() {
186 f({0, {1}}); // expected-warning{{braces around scalar initializer}}
187 g({{0, 1}, {2, 3}});
188 std::initializer_list<int> il = {1, 2};
189 g({il, {2, 3}});
193 namespace Decay {
194 template<typename T>
195 void f(std::initializer_list<T>) {
196 T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
199 void g() {
200 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
202 auto x = { "A", "BB", "CCC" };
203 std::initializer_list<const char *> *il = &x;
205 for( auto s : {"A", "BB", "CCC", "DDD"}) { }
209 namespace PR12436 {
210 struct X {
211 template<typename T>
212 X(std::initializer_list<int>, T);
215 X x({}, 17);
218 namespace rdar11948732 {
219 template<typename T> struct X {};
221 struct XCtorInit {
222 XCtorInit(std::initializer_list<X<int>>);
225 void f(X<int> &xi) {
226 XCtorInit xc = { xi, xi };
230 namespace PR14272 {
231 auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
234 namespace initlist_of_array {
235 void f(std::initializer_list<int[2]>) {}
236 void f(std::initializer_list<int[2][2]>) = delete;
237 void h() {
238 f({{1,2},{3,4}});
242 namespace init_list_deduction_failure {
243 void f();
244 void f(int);
245 // FIXME: It'd be nice to track that 'T' became a non-deduced context due to
246 // overload resolution failure for 'f'.
247 template<typename T> void g(std::initializer_list<T>);
248 // expected-note@-1 {{candidate template ignored: couldn't infer template argument 'T'}}
249 void h() {
250 g({f}); // expected-error {{no matching function for call to 'g'}}
251 g({f, h}); // ok
255 namespace deleted_copy {
256 struct X {
257 X(int i) {}
258 X(const X& x) = delete; // expected-note {{here}}
259 void operator=(const X& x) = delete;
262 std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}
265 namespace RefVersusInitList {
266 struct S {};
267 void f(const S &) = delete;
268 void f(std::initializer_list<S>);
269 void g(S s) { f({S()}); }
272 namespace PR18013 {
273 int f();
274 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')}}
277 namespace DR1070 {
278 struct S {
279 S(std::initializer_list<int>);
281 S s[3] = { {1, 2, 3}, {4, 5} }; // ok
282 S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok
285 namespace ListInitInstantiate {
286 struct A {
287 A(std::initializer_list<A>);
288 A(std::initializer_list<int>);
290 struct B : A {
291 B(int);
293 template<typename T> struct X {
294 X();
295 A a;
297 template<typename T> X<T>::X() : a{B{0}, B{1}} {}
299 X<int> x;
301 int f(const A&);
302 template<typename T> void g() { int k = f({0}); }
303 template void g<int>();
306 namespace TemporaryInitListSourceRange_PR22367 {
307 struct A {
308 constexpr A() {}
309 A(std::initializer_list<int>); // expected-note {{here}}
311 constexpr int f(A) { return 0; }
312 constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
313 // The point of this test is to check that the caret points to
314 // 'std::initializer_list', not to '{0}'.
315 std::initializer_list // expected-note {{constructor}}
316 <int>
321 namespace ParameterPackNestedInitializerLists_PR23904c3 {
322 template <typename ...T>
323 void f(std::initializer_list<std::initializer_list<T>> ...tt); // expected-note 2{{conflicting}} expected-note {{incomplete pack}}
325 void foo() {
326 f({{0}}, {{'\0'}}); // ok, T = <int, char>
327 f({{0}, {'\0'}}); // expected-error {{no match}}
328 f({{0, '\0'}}); // expected-error {{no match}}
330 f({{0}}, {{{}}}); // expected-error {{no match}}
331 f({{0}}, {{{}, '\0'}}); // ok, T = <int, char>
332 f({{0}, {{}}}); // ok, T = <int>
333 f({{0, {}}}); // ok, T = <int>
337 namespace update_rbrace_loc_crash {
338 // We used to crash-on-invalid on this example when updating the right brace
339 // location.
340 template <typename T, T>
341 struct A {};
342 template <typename T, typename F, int... I>
343 std::initializer_list<T> ExplodeImpl(F p1, A<int, I...>) {
344 // expected-error@+1 {{reference to incomplete type 'const Incomplete' could not bind to an rvalue of type 'void'}}
345 return {p1(I)...};
347 template <typename T, int N, typename F>
348 void Explode(F p1) {
349 // expected-note@+1 {{in instantiation of function template specialization}}
350 ExplodeImpl<T>(p1, A<int, N>());
352 class Incomplete;
353 struct ContainsIncomplete {
354 const Incomplete &obstacle;
356 void f() {
357 // expected-note@+1 {{in instantiation of function template specialization}}
358 Explode<ContainsIncomplete, 4>([](int) {});
362 namespace no_conversion_after_auto_list_deduction {
363 // We used to deduce 'auto' == 'std::initializer_list<X>' here, and then
364 // incorrectly accept the declaration of 'x'.
365 struct X { using T = std::initializer_list<X> X::*; operator T(); };
366 auto X::*x = { X() }; // expected-error {{from initializer list}}
368 struct Y { using T = std::initializer_list<Y>(*)(); operator T(); };
369 auto (*y)() = { Y() }; // expected-error {{from initializer list}}
372 namespace designated_init {
373 constexpr auto a = {.a = 1, .b = 2}; // expected-error {{cannot deduce}}
374 constexpr auto b = {[0] = 1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning {{C99}}
375 constexpr auto c = {1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
376 constexpr auto d = {1, [0] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
378 // If we ever start accepting the above, these assertions should pass.
379 static_assert(c.size() == 5, "");
380 static_assert(d.size() == 1, "");
383 namespace weird_initlist {
384 template<int>
385 struct weird {};
387 template<> struct std::initializer_list<weird_initlist::weird<0>> { int a, b, c; };
388 // expected-error@-1 2 {{'std::initializer_list<weird<0>>' layout not recognized}}
389 template<> struct std::initializer_list<weird_initlist::weird<1>> { std::size_t sz; const weird_initlist::weird<1>* p; };
390 // expected-error@-1 {{'std::initializer_list<weird<1>>' layout not recognized}}
391 template<> struct std::initializer_list<weird_initlist::weird<2>> { const weird_initlist::weird<2>* first; const weird_initlist::weird<2>* last; };
392 template<> struct std::initializer_list<weird_initlist::weird<3>> { weird_initlist::weird<3>* p; std::size_t sz; };
393 // expected-error@-1 {{'std::initializer_list<weird<3>>' layout not recognized}}
394 template<> struct std::initializer_list<weird_initlist::weird<4>> { const weird_initlist::weird<4>& p; std::size_t sz; };
395 // expected-error@-1 {{'std::initializer_list<weird<4>>' layout not recognized}}
396 template<> struct std::initializer_list<weird_initlist::weird<5>> { const weird_initlist::weird<5>* p; std::size_t : sizeof(std::size_t) * __CHAR_BIT__; };
397 // expected-error@-1 {{'std::initializer_list<weird<5>>' layout not recognized}}
398 template<> struct std::initializer_list<weird_initlist::weird<6>> { const weird_initlist::weird<6>* p; std::size_t sz : sizeof(std::size_t) * __CHAR_BIT__; };
399 // expected-error@-1 {{'std::initializer_list<weird<6>>' layout not recognized}}
400 struct empty_base {};
401 template<> struct std::initializer_list<weird_initlist::weird<7>> : empty_base { const weird_initlist::weird<7>* p; std::size_t sz; };
402 // expected-error@-1 {{'std::initializer_list<weird<7>>' layout not recognized}}
403 template<> struct std::initializer_list<weird_initlist::weird<8>> { const weird_initlist::weird<8>* p; std::size_t sz; ~initializer_list(); };
404 template<> struct std::initializer_list<weird_initlist::weird<9>> { const weird_initlist::weird<9>* p; std::size_t sz; virtual void f(); };
405 // expected-error@-1 {{'std::initializer_list<weird<9>>' layout not recognized}}
406 template<> struct std::initializer_list<weird_initlist::weird<10>> { const weird_initlist::weird<10>* p; alignas(64) std::size_t sz; };
407 template<> struct std::initializer_list<weird_initlist::weird<11>>;
408 // expected-note@-1 {{forward declaration of 'std::initializer_list<weird_initlist::weird<11>>'}}
409 namespace weird_initlist {
410 auto _0 = {weird<0>{}, weird<0>{}, weird<0>{}, weird<0>{}, weird<0>{}};
411 constexpr auto _0c = {weird<0>{}, weird<0>{}, weird<0>{}, weird<0>{}, weird<0>{}};
412 auto _1 = {weird<1>{}, weird<1>{}};
413 auto _2 = {weird<2>{}, weird<2>{}, weird<2>{}};
414 constexpr auto _2c = {weird<2>{}, weird<2>{}, weird<2>{}}; // (Two pointer representation is supported)
415 static_assert(_2c.first + 3 == _2c.last, "");
416 auto _3 = {weird<3>{}, weird<3>{}};
417 auto _4 = {weird<4>{}, weird<4>{}};
418 auto _5 = {weird<5>{}, weird<5>{}};
419 auto _6 = {weird<6>{}, weird<6>{}};
420 auto _7 = {weird<7>{}, weird<7>{}};
421 auto _8 = {weird<8>{}, weird<8>{}};
422 auto _9 = {weird<9>{}, weird<9>{}};
423 auto _10 = {weird<10>{}, weird<10>{}};
424 constexpr auto _10c = {weird<10>{}, weird<10>{}, weird<10>{}};
425 static_assert(_10c.sz == 3, "");
426 const auto& _11 = {weird<11>{}, weird<11>{}}; // expected-error {{initialization of incomplete type 'const std::initializer_list<weird<11>>'}}
429 auto v = std::initializer_list<int>{1,2,3}; // expected-warning {{array backing local initializer list 'v' will be destroyed at the end of the full-expression}}
431 std::initializer_list<int> get(int cond) {
432 if (cond == 0)
433 return {};
434 if (cond == 1)
435 return {1, 2, 3}; // expected-warning {{returning address of local temporary object}}
436 return std::initializer_list<int>{1, 2, 3}; // expected-warning {{returning address of local temporary object}}
438 #endif // UNION_TEST