1*67e74705SXin Li // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 2*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 3*67e74705SXin Li // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 4*67e74705SXin Li // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 5*67e74705SXin Li 6*67e74705SXin Li #if __cplusplus < 201103L 7*67e74705SXin Li // expected-no-diagnostics 8*67e74705SXin Li #endif 9*67e74705SXin Li 10*67e74705SXin Li namespace dr1460 { // dr1460: 3.5 11*67e74705SXin Li #if __cplusplus >= 201103L 12*67e74705SXin Li namespace DRExample { 13*67e74705SXin Li union A { 14*67e74705SXin Li union {}; 15*67e74705SXin Li union {}; A()16*67e74705SXin Li constexpr A() {} 17*67e74705SXin Li }; 18*67e74705SXin Li constexpr A a = A(); 19*67e74705SXin Li 20*67e74705SXin Li union B { 21*67e74705SXin Li union {}; 22*67e74705SXin Li union {}; 23*67e74705SXin Li constexpr B() = default; 24*67e74705SXin Li }; 25*67e74705SXin Li constexpr B b = B(); 26*67e74705SXin Li 27*67e74705SXin Li union C { 28*67e74705SXin Li union {}; 29*67e74705SXin Li union {}; 30*67e74705SXin Li }; 31*67e74705SXin Li constexpr C c = C(); 32*67e74705SXin Li #if __cplusplus > 201103L f()33*67e74705SXin Li constexpr void f() { C c; } 34*67e74705SXin Li static_assert((f(), true), ""); 35*67e74705SXin Li #endif 36*67e74705SXin Li } 37*67e74705SXin Li 38*67e74705SXin Li union A {}; 39*67e74705SXin Li union B { int n; }; // expected-note +{{here}} 40*67e74705SXin Li union C { int n = 0; }; 41*67e74705SXin Li struct D { union {}; }; 42*67e74705SXin Li struct E { union { int n; }; }; // expected-note +{{here}} 43*67e74705SXin Li struct F { union { int n = 0; }; }; 44*67e74705SXin Li 45*67e74705SXin Li struct X { 46*67e74705SXin Li friend constexpr A::A() noexcept; 47*67e74705SXin Li friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}} 48*67e74705SXin Li friend constexpr C::C() noexcept; 49*67e74705SXin Li friend constexpr D::D() noexcept; 50*67e74705SXin Li friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}} 51*67e74705SXin Li friend constexpr F::F() noexcept; 52*67e74705SXin Li }; 53*67e74705SXin Li 54*67e74705SXin Li // These are OK, because value-initialization doesn't actually invoke the 55*67e74705SXin Li // constructor. 56*67e74705SXin Li constexpr A a = A(); 57*67e74705SXin Li constexpr B b = B(); 58*67e74705SXin Li constexpr C c = C(); 59*67e74705SXin Li constexpr D d = D(); 60*67e74705SXin Li constexpr E e = E(); 61*67e74705SXin Li constexpr F f = F(); 62*67e74705SXin Li 63*67e74705SXin Li namespace Defaulted { 64*67e74705SXin Li union A { constexpr A() = default; }; 65*67e74705SXin Li union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}} 66*67e74705SXin Li union C { int n = 0; constexpr C() = default; }; 67*67e74705SXin Li struct D { union {}; constexpr D() = default; }; 68*67e74705SXin Li struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}} 69*67e74705SXin Li struct F { union { int n = 0; }; constexpr F() = default; }; 70*67e74705SXin Li 71*67e74705SXin Li struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}} 72*67e74705SXin Li struct H { 73*67e74705SXin Li union { 74*67e74705SXin Li int n = 0; 75*67e74705SXin Li }; 76*67e74705SXin Li union { // expected-note 2{{member not initialized}} 77*67e74705SXin Li int m; 78*67e74705SXin Li }; Hdr1460::Defaulted::H79*67e74705SXin Li constexpr H() {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H80*67e74705SXin Li constexpr H(bool) : m(1) {} Hdr1460::Defaulted::H81*67e74705SXin Li constexpr H(char) : n(1) {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H82*67e74705SXin Li constexpr H(double) : m(1), n(1) {} 83*67e74705SXin Li }; 84*67e74705SXin Li } 85*67e74705SXin Li 86*67e74705SXin Li #if __cplusplus > 201103L check()87*67e74705SXin Li template<typename T> constexpr bool check() { 88*67e74705SXin Li T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}} 89*67e74705SXin Li return true; 90*67e74705SXin Li } 91*67e74705SXin Li static_assert(check<A>(), ""); 92*67e74705SXin Li static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}} 93*67e74705SXin Li static_assert(check<C>(), ""); 94*67e74705SXin Li static_assert(check<D>(), ""); 95*67e74705SXin Li static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}} 96*67e74705SXin Li static_assert(check<F>(), ""); 97*67e74705SXin Li #endif 98*67e74705SXin Li 99*67e74705SXin Li union G { 100*67e74705SXin Li int a = 0; // expected-note {{previous initialization is here}} 101*67e74705SXin Li int b = 0; // expected-error {{initializing multiple members of union}} 102*67e74705SXin Li }; 103*67e74705SXin Li union H { 104*67e74705SXin Li union { 105*67e74705SXin Li int a = 0; // expected-note {{previous initialization is here}} 106*67e74705SXin Li }; 107*67e74705SXin Li union { 108*67e74705SXin Li int b = 0; // expected-error {{initializing multiple members of union}} 109*67e74705SXin Li }; 110*67e74705SXin Li }; 111*67e74705SXin Li struct I { 112*67e74705SXin Li union { 113*67e74705SXin Li int a = 0; // expected-note {{previous initialization is here}} 114*67e74705SXin Li int b = 0; // expected-error {{initializing multiple members of union}} 115*67e74705SXin Li }; 116*67e74705SXin Li }; 117*67e74705SXin Li struct J { 118*67e74705SXin Li union { int a = 0; }; 119*67e74705SXin Li union { int b = 0; }; 120*67e74705SXin Li }; 121*67e74705SXin Li 122*67e74705SXin Li namespace Overriding { 123*67e74705SXin Li struct A { 124*67e74705SXin Li int a = 1, b, c = 3; Adr1460::Overriding::A125*67e74705SXin Li constexpr A() : b(2) {} 126*67e74705SXin Li }; 127*67e74705SXin Li static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 128*67e74705SXin Li 129*67e74705SXin Li union B { 130*67e74705SXin Li int a, b = 2, c; B()131*67e74705SXin Li constexpr B() : a(1) {} B(char)132*67e74705SXin Li constexpr B(char) : b(4) {} B(int)133*67e74705SXin Li constexpr B(int) : c(3) {} B(const char *)134*67e74705SXin Li constexpr B(const char*) {} 135*67e74705SXin Li }; 136*67e74705SXin Li static_assert(B().a == 1, ""); 137*67e74705SXin Li static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 138*67e74705SXin Li static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 139*67e74705SXin Li static_assert(B('x').b == 4, ""); 140*67e74705SXin Li static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 141*67e74705SXin Li static_assert(B(123).c == 3, ""); 142*67e74705SXin Li static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 143*67e74705SXin Li static_assert(B("").b == 2, ""); 144*67e74705SXin Li static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 145*67e74705SXin Li 146*67e74705SXin Li struct C { 147*67e74705SXin Li union { int a, b = 2, c; }; 148*67e74705SXin Li union { int d, e = 5, f; }; Cdr1460::Overriding::C149*67e74705SXin Li constexpr C() : a(1) {} Cdr1460::Overriding::C150*67e74705SXin Li constexpr C(char) : c(3) {} Cdr1460::Overriding::C151*67e74705SXin Li constexpr C(int) : d(4) {} Cdr1460::Overriding::C152*67e74705SXin Li constexpr C(float) : f(6) {} Cdr1460::Overriding::C153*67e74705SXin Li constexpr C(const char*) {} 154*67e74705SXin Li }; 155*67e74705SXin Li 156*67e74705SXin Li static_assert(C().a == 1, ""); 157*67e74705SXin Li static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 158*67e74705SXin Li static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 159*67e74705SXin Li static_assert(C().e == 5, ""); 160*67e74705SXin Li 161*67e74705SXin Li static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 162*67e74705SXin Li static_assert(C('x').c == 3, ""); 163*67e74705SXin Li static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 164*67e74705SXin Li static_assert(C('x').e == 5, ""); 165*67e74705SXin Li 166*67e74705SXin Li static_assert(C(1).b == 2, ""); 167*67e74705SXin Li static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 168*67e74705SXin Li static_assert(C(1).d == 4, ""); 169*67e74705SXin Li static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 170*67e74705SXin Li 171*67e74705SXin Li static_assert(C(1.f).b == 2, ""); 172*67e74705SXin Li static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 173*67e74705SXin Li static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 174*67e74705SXin Li static_assert(C(1.f).f == 6, ""); 175*67e74705SXin Li 176*67e74705SXin Li static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 177*67e74705SXin Li static_assert(C("").b == 2, ""); 178*67e74705SXin Li static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 179*67e74705SXin Li static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 180*67e74705SXin Li static_assert(C("").e == 5, ""); 181*67e74705SXin Li static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 182*67e74705SXin Li 183*67e74705SXin Li struct D; 184*67e74705SXin Li extern const D d; 185*67e74705SXin Li struct D { 186*67e74705SXin Li int a; 187*67e74705SXin Li union { 188*67e74705SXin Li int b = const_cast<D&>(d).a = 1; // not evaluated 189*67e74705SXin Li int c; 190*67e74705SXin Li }; Ddr1460::Overriding::D191*67e74705SXin Li constexpr D() : a(0), c(0) {} 192*67e74705SXin Li }; 193*67e74705SXin Li constexpr D d {}; 194*67e74705SXin Li static_assert(d.a == 0, ""); 195*67e74705SXin Li } 196*67e74705SXin Li #endif 197*67e74705SXin Li } 198*67e74705SXin Li 199*67e74705SXin Li #if __cplusplus >= 201103L 200*67e74705SXin Li namespace std { 201*67e74705SXin Li typedef decltype(sizeof(int)) size_t; 202*67e74705SXin Li 203*67e74705SXin Li // libc++'s implementation 204*67e74705SXin Li template <class _E> 205*67e74705SXin Li class initializer_list 206*67e74705SXin Li { 207*67e74705SXin Li const _E* __begin_; 208*67e74705SXin Li size_t __size_; 209*67e74705SXin Li initializer_list(const _E * __b,size_t __s)210*67e74705SXin Li initializer_list(const _E* __b, size_t __s) 211*67e74705SXin Li : __begin_(__b), __size_(__s) {} 212*67e74705SXin Li 213*67e74705SXin Li public: 214*67e74705SXin Li typedef _E value_type; 215*67e74705SXin Li typedef const _E& reference; 216*67e74705SXin Li typedef const _E& const_reference; 217*67e74705SXin Li typedef size_t size_type; 218*67e74705SXin Li 219*67e74705SXin Li typedef const _E* iterator; 220*67e74705SXin Li typedef const _E* const_iterator; 221*67e74705SXin Li initializer_list()222*67e74705SXin Li initializer_list() : __begin_(nullptr), __size_(0) {} 223*67e74705SXin Li size() const224*67e74705SXin Li size_t size() const {return __size_;} begin() const225*67e74705SXin Li const _E* begin() const {return __begin_;} end() const226*67e74705SXin Li const _E* end() const {return __begin_ + __size_;} 227*67e74705SXin Li }; 228*67e74705SXin Li } // std 229*67e74705SXin Li 230*67e74705SXin Li namespace dr1467 { // dr1467: 3.7 c++11 231*67e74705SXin Li // List-initialization of aggregate from same-type object 232*67e74705SXin Li 233*67e74705SXin Li namespace basic0 { 234*67e74705SXin Li struct S { 235*67e74705SXin Li int i = 42; 236*67e74705SXin Li }; 237*67e74705SXin Li 238*67e74705SXin Li S a; 239*67e74705SXin Li S b(a); 240*67e74705SXin Li S c{a}; 241*67e74705SXin Li 242*67e74705SXin Li struct SS : public S { } x; 243*67e74705SXin Li S y(x); 244*67e74705SXin Li S z{x}; 245*67e74705SXin Li } // basic0 246*67e74705SXin Li 247*67e74705SXin Li namespace basic1 { 248*67e74705SXin Li struct S { 249*67e74705SXin Li int i{42}; 250*67e74705SXin Li }; 251*67e74705SXin Li 252*67e74705SXin Li S a; 253*67e74705SXin Li S b(a); 254*67e74705SXin Li S c{a}; 255*67e74705SXin Li 256*67e74705SXin Li struct SS : public S { } x; 257*67e74705SXin Li S y(x); 258*67e74705SXin Li S z{x}; 259*67e74705SXin Li } // basic1 260*67e74705SXin Li 261*67e74705SXin Li namespace basic2 { 262*67e74705SXin Li struct S { 263*67e74705SXin Li int i = {42}; 264*67e74705SXin Li }; 265*67e74705SXin Li 266*67e74705SXin Li S a; 267*67e74705SXin Li S b(a); 268*67e74705SXin Li S c{a}; 269*67e74705SXin Li 270*67e74705SXin Li struct SS : public S { } x; 271*67e74705SXin Li S y(x); 272*67e74705SXin Li S z{x}; 273*67e74705SXin Li } // basic2 274*67e74705SXin Li 275*67e74705SXin Li namespace dr_example { 276*67e74705SXin Li struct OK { 277*67e74705SXin Li OK() = default; 278*67e74705SXin Li OK(const OK&) = default; OKdr1467::dr_example::OK279*67e74705SXin Li OK(int) { } 280*67e74705SXin Li }; 281*67e74705SXin Li 282*67e74705SXin Li OK ok; 283*67e74705SXin Li OK ok2{ok}; 284*67e74705SXin Li 285*67e74705SXin Li struct X { 286*67e74705SXin Li X() = default; 287*67e74705SXin Li X(const X&) = default; 288*67e74705SXin Li }; 289*67e74705SXin Li 290*67e74705SXin Li X x; 291*67e74705SXin Li X x2{x}; 292*67e74705SXin Li } // dr_example 293*67e74705SXin Li 294*67e74705SXin Li namespace nonaggregate { 295*67e74705SXin Li struct NonAggregate { NonAggregatedr1467::nonaggregate::NonAggregate296*67e74705SXin Li NonAggregate() {} 297*67e74705SXin Li }; 298*67e74705SXin Li 299*67e74705SXin Li struct WantsIt { 300*67e74705SXin Li WantsIt(NonAggregate); 301*67e74705SXin Li }; 302*67e74705SXin Li 303*67e74705SXin Li void f(NonAggregate); 304*67e74705SXin Li void f(WantsIt); 305*67e74705SXin Li test1()306*67e74705SXin Li void test1() { 307*67e74705SXin Li NonAggregate n; 308*67e74705SXin Li f({n}); 309*67e74705SXin Li } 310*67e74705SXin Li test2()311*67e74705SXin Li void test2() { 312*67e74705SXin Li NonAggregate x; 313*67e74705SXin Li NonAggregate y{x}; 314*67e74705SXin Li NonAggregate z{{x}}; 315*67e74705SXin Li } 316*67e74705SXin Li } // nonaggregate 317*67e74705SXin Li 318*67e74705SXin Li namespace SelfInitIsNotListInit { 319*67e74705SXin Li struct S { 320*67e74705SXin Li S(); 321*67e74705SXin Li explicit S(S &); 322*67e74705SXin Li S(const S &); 323*67e74705SXin Li }; 324*67e74705SXin Li S s1; 325*67e74705SXin Li S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor 326*67e74705SXin Li } 327*67e74705SXin Li 328*67e74705SXin Li struct NestedInit { int a, b, c; }; 329*67e74705SXin Li NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; 330*67e74705SXin Li 331*67e74705SXin Li namespace NestedInit2 { 332*67e74705SXin Li struct Pair { int a, b; }; 333*67e74705SXin Li struct TwoPairs { TwoPairs(Pair, Pair); }; 334*67e74705SXin Li struct Value { Value(Pair); Value(TwoPairs); }; f()335*67e74705SXin Li void f() { Value{{{1,2},{3,4}}}; } 336*67e74705SXin Li } 337*67e74705SXin Li } // dr1467 338*67e74705SXin Li 339*67e74705SXin Li namespace dr1490 { // dr1490: 3.7 c++11 340*67e74705SXin Li // List-initialization from a string literal 341*67e74705SXin Li 342*67e74705SXin Li char s[4]{"abc"}; // Ok 343*67e74705SXin Li std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}} 344*67e74705SXin Li } // dr190 345*67e74705SXin Li #endif 346