Home | History | Annotate | Download | only in class.union
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 
      3 void abort() __attribute__((noreturn));
      4 
      5 class Okay {
      6   int a_;
      7 };
      8 
      9 class Virtual {
     10   virtual void foo() { abort(); } // expected-note 4 {{because type 'Virtual' has a virtual member function}}
     11 };
     12 
     13 class VirtualBase : virtual Okay { // expected-note 4 {{because type 'VirtualBase' has a virtual base class}}
     14 };
     15 
     16 class Ctor {
     17   Ctor() { abort(); } // expected-note 4 {{because type 'Ctor' has a user-declared constructor}}
     18 };
     19 class Ctor2 {
     20   Ctor2(); // expected-note 3 {{because type 'Ctor2' has a user-declared constructor}}
     21 };
     22 class CtorTmpl {
     23   template<typename T> CtorTmpl(); // expected-note {{because type 'CtorTmpl' has a user-declared constructor}}
     24 };
     25 
     26 class CopyCtor {
     27   CopyCtor(CopyCtor &cc) { abort(); } // expected-note 4 {{because type 'CopyCtor' has a user-declared copy constructor}}
     28 };
     29 
     30 // FIXME: this should eventually trigger on the operator's declaration line
     31 class CopyAssign { // expected-note 4 {{because type 'CopyAssign' has a user-declared copy assignment operator}}
     32   CopyAssign& operator=(CopyAssign& CA) { abort(); }
     33 };
     34 
     35 class Dtor {
     36   ~Dtor() { abort(); } // expected-note 4 {{because type 'Dtor' has a user-declared destructor}}
     37 };
     38 
     39 union U1 {
     40   Virtual v; // expected-error {{union member 'v' has a non-trivial copy constructor}}
     41   VirtualBase vbase; // expected-error {{union member 'vbase' has a non-trivial copy constructor}}
     42   Ctor ctor; // expected-error {{union member 'ctor' has a non-trivial constructor}}
     43   Ctor2 ctor2; // expected-error {{union member 'ctor2' has a non-trivial constructor}}
     44   CtorTmpl ctortmpl; // expected-error {{union member 'ctortmpl' has a non-trivial constructor}}
     45   CopyCtor copyctor; // expected-error {{union member 'copyctor' has a non-trivial copy constructor}}
     46   CopyAssign copyassign; // expected-error {{union member 'copyassign' has a non-trivial copy assignment operator}}
     47   Dtor dtor; // expected-error {{union member 'dtor' has a non-trivial destructor}}
     48   Okay okay;
     49 };
     50 
     51 union U2 {
     52   struct {
     53     Virtual v; // expected-note {{because type 'U2::<anonymous struct}}
     54   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
     55   struct {
     56     VirtualBase vbase; // expected-note {{because type 'U2::<anonymous struct}}
     57   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
     58   struct {
     59     Ctor ctor; // expected-note {{because type 'U2::<anonymous struct}}
     60   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
     61   struct {
     62     Ctor2 ctor2; // expected-note {{because type 'U2::<anonymous struct}}
     63   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
     64   struct {
     65     CopyCtor copyctor; // expected-note {{because type 'U2::<anonymous struct}}
     66   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
     67   struct {
     68     CopyAssign copyassign; // expected-note {{because type 'U2::<anonymous struct}}
     69   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
     70   struct {
     71     Dtor dtor; // expected-note {{because type 'U2::<anonymous struct}}
     72   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
     73   struct {
     74     Okay okay;
     75   } m7;
     76 };
     77 
     78 union U3 {
     79   struct s1 : Virtual { // expected-note {{because type 'U3::s1' has a base class with a non-trivial copy constructor}}
     80   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
     81   struct s2 : VirtualBase { // expected-note {{because type 'U3::s2' has a base class with a non-trivial copy constructor}}
     82   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
     83   struct s3 : Ctor { // expected-note {{because type 'U3::s3' has a base class with a non-trivial constructor}}
     84   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
     85   struct s3a : Ctor2 { // expected-note {{because type 'U3::s3a' has a base class with a non-trivial constructor}}
     86   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
     87   struct s4 : CopyCtor { // expected-note {{because type 'U3::s4' has a base class with a non-trivial copy constructor}}
     88   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
     89   struct s5 : CopyAssign { // expected-note {{because type 'U3::s5' has a base class with a non-trivial copy assignment operator}}
     90   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
     91   struct s6 : Dtor { // expected-note {{because type 'U3::s6' has a base class with a non-trivial destructor}}
     92   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
     93   struct s7 : Okay {
     94   } m7;
     95 };
     96 
     97 union U4 {
     98   static int i1; // expected-warning {{static data member 'i1' in union is a C++11 extension}}
     99 };
    100 int U4::i1 = 10;
    101 
    102 union U5 {
    103   int& i1; // expected-error {{union member 'i1' has reference type 'int &'}}
    104 };
    105 
    106 template <class A, class B> struct Either {
    107   bool tag;
    108   union { // expected-note 6 {{in instantiation of member class}}
    109     A a;
    110     B b; // expected-error 6 {{non-trivial}}
    111   };
    112 
    113   Either(const A& a) : tag(true), a(a) {}
    114   Either(const B& b) : tag(false), b(b) {}
    115 };
    116 
    117 void fred() {
    118   Either<int,Virtual> virt(0); // expected-note {{in instantiation of template}}
    119   Either<int,VirtualBase> vbase(0); // expected-note {{in instantiation of template}}
    120   Either<int,Ctor> ctor(0); // expected-note {{in instantiation of template}}
    121   Either<int,CopyCtor> copyctor(0); // expected-note {{in instantiation of template}}
    122   Either<int,CopyAssign> copyassign(0); // expected-note {{in instantiation of template}}
    123   Either<int,Dtor> dtor(0); // expected-note {{in instantiation of template}}
    124   Either<int,Okay> okay(0);
    125 }
    126