1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 3*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 4*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 5*0a6a1f1dSLionel Sambuc 6*0a6a1f1dSLionel Sambuc #if __cplusplus < 201103L 7*0a6a1f1dSLionel Sambuc // expected-no-diagnostics 8*0a6a1f1dSLionel Sambuc #endif 9*0a6a1f1dSLionel Sambuc 10*0a6a1f1dSLionel Sambuc namespace dr1460 { // dr1460: 3.5 11*0a6a1f1dSLionel Sambuc #if __cplusplus >= 201103L 12*0a6a1f1dSLionel Sambuc namespace DRExample { 13*0a6a1f1dSLionel Sambuc union A { 14*0a6a1f1dSLionel Sambuc union {}; 15*0a6a1f1dSLionel Sambuc union {}; A()16*0a6a1f1dSLionel Sambuc constexpr A() {} 17*0a6a1f1dSLionel Sambuc }; 18*0a6a1f1dSLionel Sambuc constexpr A a = A(); 19*0a6a1f1dSLionel Sambuc 20*0a6a1f1dSLionel Sambuc union B { 21*0a6a1f1dSLionel Sambuc union {}; 22*0a6a1f1dSLionel Sambuc union {}; 23*0a6a1f1dSLionel Sambuc constexpr B() = default; 24*0a6a1f1dSLionel Sambuc }; 25*0a6a1f1dSLionel Sambuc constexpr B b = B(); 26*0a6a1f1dSLionel Sambuc 27*0a6a1f1dSLionel Sambuc union C { 28*0a6a1f1dSLionel Sambuc union {}; 29*0a6a1f1dSLionel Sambuc union {}; 30*0a6a1f1dSLionel Sambuc }; 31*0a6a1f1dSLionel Sambuc constexpr C c = C(); 32*0a6a1f1dSLionel Sambuc #if __cplusplus > 201103L f()33*0a6a1f1dSLionel Sambuc constexpr void f() { C c; } 34*0a6a1f1dSLionel Sambuc static_assert((f(), true), ""); 35*0a6a1f1dSLionel Sambuc #endif 36*0a6a1f1dSLionel Sambuc } 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel Sambuc union A {}; 39*0a6a1f1dSLionel Sambuc union B { int n; }; // expected-note +{{here}} 40*0a6a1f1dSLionel Sambuc union C { int n = 0; }; 41*0a6a1f1dSLionel Sambuc struct D { union {}; }; 42*0a6a1f1dSLionel Sambuc struct E { union { int n; }; }; // expected-note +{{here}} 43*0a6a1f1dSLionel Sambuc struct F { union { int n = 0; }; }; 44*0a6a1f1dSLionel Sambuc 45*0a6a1f1dSLionel Sambuc struct X { 46*0a6a1f1dSLionel Sambuc friend constexpr A::A() noexcept; 47*0a6a1f1dSLionel Sambuc friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}} 48*0a6a1f1dSLionel Sambuc friend constexpr C::C() noexcept; 49*0a6a1f1dSLionel Sambuc friend constexpr D::D() noexcept; 50*0a6a1f1dSLionel Sambuc friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}} 51*0a6a1f1dSLionel Sambuc friend constexpr F::F() noexcept; 52*0a6a1f1dSLionel Sambuc }; 53*0a6a1f1dSLionel Sambuc 54*0a6a1f1dSLionel Sambuc // These are OK, because value-initialization doesn't actually invoke the 55*0a6a1f1dSLionel Sambuc // constructor. 56*0a6a1f1dSLionel Sambuc constexpr A a = A(); 57*0a6a1f1dSLionel Sambuc constexpr B b = B(); 58*0a6a1f1dSLionel Sambuc constexpr C c = C(); 59*0a6a1f1dSLionel Sambuc constexpr D d = D(); 60*0a6a1f1dSLionel Sambuc constexpr E e = E(); 61*0a6a1f1dSLionel Sambuc constexpr F f = F(); 62*0a6a1f1dSLionel Sambuc 63*0a6a1f1dSLionel Sambuc namespace Defaulted { 64*0a6a1f1dSLionel Sambuc union A { constexpr A() = default; }; 65*0a6a1f1dSLionel Sambuc union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}} 66*0a6a1f1dSLionel Sambuc union C { int n = 0; constexpr C() = default; }; 67*0a6a1f1dSLionel Sambuc struct D { union {}; constexpr D() = default; }; 68*0a6a1f1dSLionel Sambuc struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}} 69*0a6a1f1dSLionel Sambuc struct F { union { int n = 0; }; constexpr F() = default; }; 70*0a6a1f1dSLionel Sambuc 71*0a6a1f1dSLionel Sambuc struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}} 72*0a6a1f1dSLionel Sambuc struct H { 73*0a6a1f1dSLionel Sambuc union { 74*0a6a1f1dSLionel Sambuc int n = 0; 75*0a6a1f1dSLionel Sambuc }; 76*0a6a1f1dSLionel Sambuc union { // expected-note 2{{member not initialized}} 77*0a6a1f1dSLionel Sambuc int m; 78*0a6a1f1dSLionel Sambuc }; Hdr1460::Defaulted::H79*0a6a1f1dSLionel Sambuc constexpr H() {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H80*0a6a1f1dSLionel Sambuc constexpr H(bool) : m(1) {} Hdr1460::Defaulted::H81*0a6a1f1dSLionel Sambuc constexpr H(char) : n(1) {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H82*0a6a1f1dSLionel Sambuc constexpr H(double) : m(1), n(1) {} 83*0a6a1f1dSLionel Sambuc }; 84*0a6a1f1dSLionel Sambuc } 85*0a6a1f1dSLionel Sambuc 86*0a6a1f1dSLionel Sambuc #if __cplusplus > 201103L check()87*0a6a1f1dSLionel Sambuc template<typename T> constexpr bool check() { 88*0a6a1f1dSLionel Sambuc T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}} 89*0a6a1f1dSLionel Sambuc return true; 90*0a6a1f1dSLionel Sambuc } 91*0a6a1f1dSLionel Sambuc static_assert(check<A>(), ""); 92*0a6a1f1dSLionel Sambuc static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}} 93*0a6a1f1dSLionel Sambuc static_assert(check<C>(), ""); 94*0a6a1f1dSLionel Sambuc static_assert(check<D>(), ""); 95*0a6a1f1dSLionel Sambuc static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}} 96*0a6a1f1dSLionel Sambuc static_assert(check<F>(), ""); 97*0a6a1f1dSLionel Sambuc #endif 98*0a6a1f1dSLionel Sambuc 99*0a6a1f1dSLionel Sambuc union G { 100*0a6a1f1dSLionel Sambuc int a = 0; // expected-note {{previous initialization is here}} 101*0a6a1f1dSLionel Sambuc int b = 0; // expected-error {{initializing multiple members of union}} 102*0a6a1f1dSLionel Sambuc }; 103*0a6a1f1dSLionel Sambuc union H { 104*0a6a1f1dSLionel Sambuc union { 105*0a6a1f1dSLionel Sambuc int a = 0; // expected-note {{previous initialization is here}} 106*0a6a1f1dSLionel Sambuc }; 107*0a6a1f1dSLionel Sambuc union { 108*0a6a1f1dSLionel Sambuc int b = 0; // expected-error {{initializing multiple members of union}} 109*0a6a1f1dSLionel Sambuc }; 110*0a6a1f1dSLionel Sambuc }; 111*0a6a1f1dSLionel Sambuc struct I { 112*0a6a1f1dSLionel Sambuc union { 113*0a6a1f1dSLionel Sambuc int a = 0; // expected-note {{previous initialization is here}} 114*0a6a1f1dSLionel Sambuc int b = 0; // expected-error {{initializing multiple members of union}} 115*0a6a1f1dSLionel Sambuc }; 116*0a6a1f1dSLionel Sambuc }; 117*0a6a1f1dSLionel Sambuc struct J { 118*0a6a1f1dSLionel Sambuc union { int a = 0; }; 119*0a6a1f1dSLionel Sambuc union { int b = 0; }; 120*0a6a1f1dSLionel Sambuc }; 121*0a6a1f1dSLionel Sambuc 122*0a6a1f1dSLionel Sambuc namespace Overriding { 123*0a6a1f1dSLionel Sambuc struct A { 124*0a6a1f1dSLionel Sambuc int a = 1, b, c = 3; Adr1460::Overriding::A125*0a6a1f1dSLionel Sambuc constexpr A() : b(2) {} 126*0a6a1f1dSLionel Sambuc }; 127*0a6a1f1dSLionel Sambuc static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 128*0a6a1f1dSLionel Sambuc 129*0a6a1f1dSLionel Sambuc union B { 130*0a6a1f1dSLionel Sambuc int a, b = 2, c; B()131*0a6a1f1dSLionel Sambuc constexpr B() : a(1) {} B(char)132*0a6a1f1dSLionel Sambuc constexpr B(char) : b(4) {} B(int)133*0a6a1f1dSLionel Sambuc constexpr B(int) : c(3) {} B(const char *)134*0a6a1f1dSLionel Sambuc constexpr B(const char*) {} 135*0a6a1f1dSLionel Sambuc }; 136*0a6a1f1dSLionel Sambuc static_assert(B().a == 1, ""); 137*0a6a1f1dSLionel Sambuc static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 138*0a6a1f1dSLionel Sambuc static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 139*0a6a1f1dSLionel Sambuc static_assert(B('x').b == 4, ""); 140*0a6a1f1dSLionel Sambuc static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 141*0a6a1f1dSLionel Sambuc static_assert(B(123).c == 3, ""); 142*0a6a1f1dSLionel Sambuc static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 143*0a6a1f1dSLionel Sambuc static_assert(B("").b == 2, ""); 144*0a6a1f1dSLionel Sambuc static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 145*0a6a1f1dSLionel Sambuc 146*0a6a1f1dSLionel Sambuc struct C { 147*0a6a1f1dSLionel Sambuc union { int a, b = 2, c; }; 148*0a6a1f1dSLionel Sambuc union { int d, e = 5, f; }; Cdr1460::Overriding::C149*0a6a1f1dSLionel Sambuc constexpr C() : a(1) {} Cdr1460::Overriding::C150*0a6a1f1dSLionel Sambuc constexpr C(char) : c(3) {} Cdr1460::Overriding::C151*0a6a1f1dSLionel Sambuc constexpr C(int) : d(4) {} Cdr1460::Overriding::C152*0a6a1f1dSLionel Sambuc constexpr C(float) : f(6) {} Cdr1460::Overriding::C153*0a6a1f1dSLionel Sambuc constexpr C(const char*) {} 154*0a6a1f1dSLionel Sambuc }; 155*0a6a1f1dSLionel Sambuc 156*0a6a1f1dSLionel Sambuc static_assert(C().a == 1, ""); 157*0a6a1f1dSLionel Sambuc static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 158*0a6a1f1dSLionel Sambuc static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 159*0a6a1f1dSLionel Sambuc static_assert(C().e == 5, ""); 160*0a6a1f1dSLionel Sambuc 161*0a6a1f1dSLionel Sambuc static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 162*0a6a1f1dSLionel Sambuc static_assert(C('x').c == 3, ""); 163*0a6a1f1dSLionel Sambuc static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 164*0a6a1f1dSLionel Sambuc static_assert(C('x').e == 5, ""); 165*0a6a1f1dSLionel Sambuc 166*0a6a1f1dSLionel Sambuc static_assert(C(1).b == 2, ""); 167*0a6a1f1dSLionel Sambuc static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 168*0a6a1f1dSLionel Sambuc static_assert(C(1).d == 4, ""); 169*0a6a1f1dSLionel Sambuc static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 170*0a6a1f1dSLionel Sambuc 171*0a6a1f1dSLionel Sambuc static_assert(C(1.f).b == 2, ""); 172*0a6a1f1dSLionel Sambuc static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 173*0a6a1f1dSLionel Sambuc static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 174*0a6a1f1dSLionel Sambuc static_assert(C(1.f).f == 6, ""); 175*0a6a1f1dSLionel Sambuc 176*0a6a1f1dSLionel Sambuc static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 177*0a6a1f1dSLionel Sambuc static_assert(C("").b == 2, ""); 178*0a6a1f1dSLionel Sambuc static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 179*0a6a1f1dSLionel Sambuc static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 180*0a6a1f1dSLionel Sambuc static_assert(C("").e == 5, ""); 181*0a6a1f1dSLionel Sambuc static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 182*0a6a1f1dSLionel Sambuc 183*0a6a1f1dSLionel Sambuc struct D; 184*0a6a1f1dSLionel Sambuc extern const D d; 185*0a6a1f1dSLionel Sambuc struct D { 186*0a6a1f1dSLionel Sambuc int a; 187*0a6a1f1dSLionel Sambuc union { 188*0a6a1f1dSLionel Sambuc int b = const_cast<D&>(d).a = 1; // not evaluated 189*0a6a1f1dSLionel Sambuc int c; 190*0a6a1f1dSLionel Sambuc }; Ddr1460::Overriding::D191*0a6a1f1dSLionel Sambuc constexpr D() : a(0), c(0) {} 192*0a6a1f1dSLionel Sambuc }; 193*0a6a1f1dSLionel Sambuc constexpr D d {}; 194*0a6a1f1dSLionel Sambuc static_assert(d.a == 0, ""); 195*0a6a1f1dSLionel Sambuc } 196*0a6a1f1dSLionel Sambuc #endif 197*0a6a1f1dSLionel Sambuc } 198