1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s 27a614d8380297fcd2bc23986241905d97222948cRichard Smith 37a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct Bitfield { 47a614d8380297fcd2bc23986241905d97222948cRichard Smith int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}} 57a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 67a614d8380297fcd2bc23986241905d97222948cRichard Smith 77a614d8380297fcd2bc23986241905d97222948cRichard Smithint a; 87a614d8380297fcd2bc23986241905d97222948cRichard Smithclass NoWarning { 97a614d8380297fcd2bc23986241905d97222948cRichard Smith int &n = a; 107a614d8380297fcd2bc23986241905d97222948cRichard Smithpublic: 117a614d8380297fcd2bc23986241905d97222948cRichard Smith int &GetN() { return n; } 127a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 137a614d8380297fcd2bc23986241905d97222948cRichard Smith 147a614d8380297fcd2bc23986241905d97222948cRichard Smithbool b(); 157a614d8380297fcd2bc23986241905d97222948cRichard Smithint k; 167a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct Recurse { 17b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith int &n = b() ? Recurse().n : k; // expected-error {{defaulted default constructor of 'Recurse' cannot be used by non-static data member initializer which appears before end of class definition}} 187a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 197a614d8380297fcd2bc23986241905d97222948cRichard Smith 207a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct UnknownBound { 217a614d8380297fcd2bc23986241905d97222948cRichard Smith int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}} 227a614d8380297fcd2bc23986241905d97222948cRichard Smith int bs[4] = { 4, 5, 6, 7 }; 237a614d8380297fcd2bc23986241905d97222948cRichard Smith int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}} 247a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 257a614d8380297fcd2bc23986241905d97222948cRichard Smith 267a614d8380297fcd2bc23986241905d97222948cRichard Smithtemplate<int n> struct T { static const int B; }; 277a614d8380297fcd2bc23986241905d97222948cRichard Smithtemplate<> struct T<2> { template<int C, int D> using B = int; }; 287a614d8380297fcd2bc23986241905d97222948cRichard Smithconst int C = 0, D = 0; 297a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct S { 307a614d8380297fcd2bc23986241905d97222948cRichard Smith int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}} 3183a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith T<sizeof(as) / sizeof(int)> x; 323164c14cadbb09a05ba811602221e9156077cf44David Blaikie // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid 333164c14cadbb09a05ba811602221e9156077cf44David Blaikie operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \ 343164c14cadbb09a05ba811602221e9156077cf44David Blaikie // expected-error {{array bound cannot be deduced from an in-class initializer}} 357a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 367a614d8380297fcd2bc23986241905d97222948cRichard Smith 377a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct ThrowCtor { ThrowCtor(int) noexcept(false); }; 387a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct NoThrowCtor { NoThrowCtor(int) noexcept(true); }; 397a614d8380297fcd2bc23986241905d97222948cRichard Smith 407a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct Throw { ThrowCtor tc = 42; }; 417a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct NoThrow { NoThrowCtor tc = 42; }; 427a614d8380297fcd2bc23986241905d97222948cRichard Smith 437a614d8380297fcd2bc23986241905d97222948cRichard Smithstatic_assert(!noexcept(Throw()), "incorrect exception specification"); 447a614d8380297fcd2bc23986241905d97222948cRichard Smithstatic_assert(noexcept(NoThrow()), "incorrect exception specification"); 457a614d8380297fcd2bc23986241905d97222948cRichard Smith 467a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct CheckExcSpec { 477a614d8380297fcd2bc23986241905d97222948cRichard Smith CheckExcSpec() noexcept(true) = default; 487a614d8380297fcd2bc23986241905d97222948cRichard Smith int n = 0; 497a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 507a614d8380297fcd2bc23986241905d97222948cRichard Smithstruct CheckExcSpecFail { 517a614d8380297fcd2bc23986241905d97222948cRichard Smith CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}} 527a614d8380297fcd2bc23986241905d97222948cRichard Smith ThrowCtor tc = 123; 537a614d8380297fcd2bc23986241905d97222948cRichard Smith}; 54c2cdd5354aba8d6a74c45231829f3bbbbfeb2781Richard Smith 55c2cdd5354aba8d6a74c45231829f3bbbbfeb2781Richard Smithstruct TypedefInit { 56c2cdd5354aba8d6a74c45231829f3bbbbfeb2781Richard Smith typedef int A = 0; // expected-error {{illegal initializer}} 57c2cdd5354aba8d6a74c45231829f3bbbbfeb2781Richard Smith}; 582eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor 592eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor// PR10578 / <rdar://problem/9877267> 602eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregornamespace PR10578 { 612eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor template<typename T> 622eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor struct X { 632eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor X() { 642eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} 652eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor } 662eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor }; 672eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor 682eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor struct Y : X<int> { 692eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor Y(); 702eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor }; 712eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor 722eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}} 732eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor } catch(...) { 742eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor } 752eef427c8666cbe9a3cad40d4947c67c3ba0c400Douglas Gregor} 76774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith 77774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smithnamespace PR14838 { 78774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith struct base { ~base() {} }; 79774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith class function : base { 80774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith ~function() {} // expected-note {{implicitly declared private here}} 81774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith public: 82774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith function(...) {} 83774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith }; 84774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith struct thing {}; 85774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith struct another { 86774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith another() : r(thing()) {} 87774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}} 88774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith // expected-warning@-2 {{binding reference member 'r' to a temporary value}} 89774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith const function &r; // expected-note {{reference member declared here}} 90774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith } af; 91774d8b4679ce1317da0f18336c3d27bdb11e5f63Richard Smith} 928150da3796300bdc876775e1782331f0e43d2d94Eli Friedman 938150da3796300bdc876775e1782331f0e43d2d94Eli Friedmannamespace rdar14084171 { 948150da3796300bdc876775e1782331f0e43d2d94Eli Friedman struct Point { // expected-note 3 {{candidate constructor}} 958150da3796300bdc876775e1782331f0e43d2d94Eli Friedman double x; 968150da3796300bdc876775e1782331f0e43d2d94Eli Friedman double y; 978150da3796300bdc876775e1782331f0e43d2d94Eli Friedman }; 988150da3796300bdc876775e1782331f0e43d2d94Eli Friedman struct Sprite { 998150da3796300bdc876775e1782331f0e43d2d94Eli Friedman Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}} 1008150da3796300bdc876775e1782331f0e43d2d94Eli Friedman }; 1018150da3796300bdc876775e1782331f0e43d2d94Eli Friedman void f(Sprite& x) { x = x; } 1028150da3796300bdc876775e1782331f0e43d2d94Eli Friedman} 103