439 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s2// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter3// RUN: %clang_cc1 -std=c++11 -fsyntax-only -DUNION_TEST -verify %s4 5#ifdef UNION_TEST6namespace 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;12 };13 14 auto x = { 1, 2, 3 };15};16#else17// This must obviously come before the definition of std::initializer_list.18void missing_initializerlist() {19 auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}20}21 22namespace std {23 typedef decltype(sizeof(int)) size_t;24 25 // libc++'s implementation26 template <class _E>27 class initializer_list28 {29 const _E* __begin_;30 size_t __size_;31 32 initializer_list(const _E* __b, size_t __s)33 : __begin_(__b),34 __size_(__s)35 {}36 37 public:38 typedef _E value_type;39 typedef const _E& reference;40 typedef const _E& const_reference;41 typedef size_t size_type;42 43 typedef const _E* iterator;44 typedef const _E* const_iterator;45 46 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}47 48 constexpr size_t size() const {return __size_;}49 const _E* begin() const {return __begin_;}50 const _E* end() const {return __begin_ + __size_;}51 };52}53 54template <typename T, typename U>55struct same_type { static const bool value = false; };56template <typename T>57struct same_type<T, T> { static const bool value = true; };58 59struct one { char c[1]; };60struct two { char c[2]; };61 62struct A {63 int a, b;64};65 66struct B {67 B();68 B(int, int);69};70 71void 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}, {} };76}77 78void function_call() {79 void f(std::initializer_list<int>);80 f({1, 2, 3});81 82 void g(std::initializer_list<B>);83 g({ {1, 2}, {2, 3}, {} });84}85 86struct C {87 C(int);88};89 90struct D {91 D();92 operator int();93 operator C();94};95 96void overloaded_call() {97 one overloaded(std::initializer_list<int>);98 two overloaded(std::initializer_list<B>);99 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");102 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}}106 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}}113}114 115template <typename T>116T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}117template <typename T>118T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}119 120template<typename T, typename U> struct pair { pair(...); };121template<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]}}123struct WithIntType { typedef int type; };124 125template<typename ...T> void deduce_after_init_list_in_pack(void (*)(T...), T...); // expected-note {{<int, int> vs. <(no value), double>}}126 127void 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");130 131 deduce({1, 2.0}); // expected-error {{no matching function}}132 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");135 136 deduce_ref({1, 2.0}); // expected-error {{no matching function}}137 138 pair<WithIntType, int> pi;139 pair<WithIntType, float> pf;140 deduce_pairs({pi, pi, pi}); // ok141 deduce_pairs({pi, pf, pi}); // expected-error {{no matching function}}142 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}}145}146 147void 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}}154 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}}159 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}}162}163 164void 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}}167}168 169struct haslist1 {170 std::initializer_list<int> il // expected-note {{declared here}}171 = {1, 2, 3}; // ok, unused172 std::initializer_list<int> jl{1, 2, 3}; // expected-note {{default member init}}173 haslist1();174};175 176haslist1::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}}178{}179 180namespace 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>>);184 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}});190 }191}192 193namespace 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'}}197 }198 199 void g() {200 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}201 202 auto x = { "A", "BB", "CCC" };203 std::initializer_list<const char *> *il = &x;204 205 for( auto s : {"A", "BB", "CCC", "DDD"}) { }206 }207}208 209namespace PR12436 {210 struct X {211 template<typename T>212 X(std::initializer_list<int>, T);213 };214 215 X x({}, 17);216}217 218namespace rdar11948732 {219 template<typename T> struct X {};220 221 struct XCtorInit {222 XCtorInit(std::initializer_list<X<int>>);223 };224 225 void f(X<int> &xi) {226 XCtorInit xc = { xi, xi };227 }228}229 230namespace PR14272 {231 auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}232}233 234namespace 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}});239 }240}241 242namespace 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 to246 // 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}); // ok252 }253}254 255namespace 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;260 };261 262 std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}263}264 265namespace RefVersusInitList {266 struct S {};267 void f(const S &) = delete;268 void f(std::initializer_list<S>);269 void g(S s) { f({S()}); }270}271 272namespace 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')}}275}276 277namespace DR1070 {278 struct S {279 S(std::initializer_list<int>);280 };281 S s[3] = { {1, 2, 3}, {4, 5} }; // ok282 S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok283}284 285namespace ListInitInstantiate {286 struct A {287 A(std::initializer_list<A>);288 A(std::initializer_list<int>);289 };290 struct B : A {291 B(int);292 };293 template<typename T> struct X {294 X();295 A a;296 };297 template<typename T> X<T>::X() : a{B{0}, B{1}} {}298 299 X<int> x;300 301 int f(const A&);302 template<typename T> void g() { int k = f({0}); }303 template void g<int>();304}305 306namespace TemporaryInitListSourceRange_PR22367 {307 struct A {308 constexpr A() {}309 A(std::initializer_list<int>); // expected-note {{here}}310 };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 to314 // 'std::initializer_list', not to '{0}'.315 std::initializer_list // expected-note {{constructor}}316 <int>317 {0}318 );319}320 321namespace 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}}324 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}}329 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>334 }335}336 337namespace update_rbrace_loc_crash {338 // We used to crash-on-invalid on this example when updating the right brace339 // 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)...};346 }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>());351 }352 class Incomplete;353 struct ContainsIncomplete {354 const Incomplete &obstacle;355 };356 void f() {357 // expected-note@+1 {{in instantiation of function template specialization}}358 Explode<ContainsIncomplete, 4>([](int) {});359 }360}361 362namespace no_conversion_after_auto_list_deduction {363 // We used to deduce 'auto' == 'std::initializer_list<X>' here, and then364 // 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}}367 368 struct Y { using T = std::initializer_list<Y>(*)(); operator T(); };369 auto (*y)() = { Y() }; // expected-error {{from initializer list}}370}371 372namespace 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}}377 378 // If we ever start accepting the above, these assertions should pass.379 static_assert(c.size() == 5, "");380 static_assert(d.size() == 1, "");381}382 383namespace weird_initlist {384 template<int>385 struct weird {};386}387template<> 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}}389template<> 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}}391template<> struct std::initializer_list<weird_initlist::weird<2>> { const weird_initlist::weird<2>* first; const weird_initlist::weird<2>* last; };392template<> 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}}394template<> 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}}396template<> 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}}398template<> 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}}400struct empty_base {};401template<> 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}}403template<> struct std::initializer_list<weird_initlist::weird<8>> { const weird_initlist::weird<8>* p; std::size_t sz; ~initializer_list(); };404template<> 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}}406template<> struct std::initializer_list<weird_initlist::weird<10>> { const weird_initlist::weird<10>* p; alignas(64) std::size_t sz; };407template<> struct std::initializer_list<weird_initlist::weird<11>>;408// expected-note@-1 {{forward declaration of 'std::initializer_list<weird_initlist::weird<11>>'}}409namespace 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>>'}}427}428 429auto 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}}430 431std::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}}437}438#endif // UNION_TEST439