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 
     23 class CopyCtor {
     24   CopyCtor(CopyCtor &cc) { abort(); } // expected-note 4 {{because type 'CopyCtor' has a user-declared copy constructor}}
     25 };
     26 
     27 // FIXME: this should eventually trigger on the operator's declaration line
     28 class CopyAssign { // expected-note 4 {{because type 'CopyAssign' has a user-declared copy assignment operator}}
     29   CopyAssign& operator=(CopyAssign& CA) { abort(); }
     30 };
     31 
     32 class Dtor {
     33   ~Dtor() { abort(); } // expected-note 4 {{because type 'Dtor' has a user-declared destructor}}
     34 };
     35 
     36 union U1 {
     37   Virtual v; // expected-error {{union member 'v' has a non-trivial copy constructor}}
     38   VirtualBase vbase; // expected-error {{union member 'vbase' has a non-trivial copy constructor}}
     39   Ctor ctor; // expected-error {{union member 'ctor' has a non-trivial constructor}}
     40   Ctor2 ctor2; // expected-error {{union member 'ctor2' has a non-trivial constructor}}
     41   CopyCtor copyctor; // expected-error {{union member 'copyctor' has a non-trivial copy constructor}}
     42   CopyAssign copyassign; // expected-error {{union member 'copyassign' has a non-trivial copy assignment operator}}
     43   Dtor dtor; // expected-error {{union member 'dtor' has a non-trivial destructor}}
     44   Okay okay;
     45 };
     46 
     47 union U2 {
     48   struct {
     49     Virtual v; // expected-note {{because type 'U2::<anonymous struct}}
     50   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
     51   struct {
     52     VirtualBase vbase; // expected-note {{because type 'U2::<anonymous struct}}
     53   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
     54   struct {
     55     Ctor ctor; // expected-note {{because type 'U2::<anonymous struct}}
     56   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
     57   struct {
     58     Ctor2 ctor2; // expected-note {{because type 'U2::<anonymous struct}}
     59   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
     60   struct {
     61     CopyCtor copyctor; // expected-note {{because type 'U2::<anonymous struct}}
     62   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
     63   struct {
     64     CopyAssign copyassign; // expected-note {{because type 'U2::<anonymous struct}}
     65   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
     66   struct {
     67     Dtor dtor; // expected-note {{because type 'U2::<anonymous struct}}
     68   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
     69   struct {
     70     Okay okay;
     71   } m7;
     72 };
     73 
     74 union U3 {
     75   struct s1 : Virtual { // expected-note {{because type 'U3::s1' has a base class with a non-trivial copy constructor}}
     76   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
     77   struct s2 : VirtualBase { // expected-note {{because type 'U3::s2' has a base class with a non-trivial copy constructor}}
     78   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
     79   struct s3 : Ctor { // expected-note {{because type 'U3::s3' has a base class with a non-trivial constructor}}
     80   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
     81   struct s3a : Ctor2 { // expected-note {{because type 'U3::s3a' has a base class with a non-trivial constructor}}
     82   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
     83   struct s4 : CopyCtor { // expected-note {{because type 'U3::s4' has a base class with a non-trivial copy constructor}}
     84   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
     85   struct s5 : CopyAssign { // expected-note {{because type 'U3::s5' has a base class with a non-trivial copy assignment operator}}
     86   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
     87   struct s6 : Dtor { // expected-note {{because type 'U3::s6' has a base class with a non-trivial destructor}}
     88   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
     89   struct s7 : Okay {
     90   } m7;
     91 };
     92 
     93 union U4 {
     94   static int i1; // expected-error {{static data member 'i1' not allowed in union}}
     95 };
     96 
     97 union U5 {
     98   int& i1; // expected-error {{union member 'i1' has reference type 'int &'}}
     99 };
    100 
    101 template <class A, class B> struct Either {
    102   bool tag;
    103   union { // expected-note 6 {{in instantiation of member class}}
    104     A a;
    105     B b; // expected-error 6 {{non-trivial}}
    106   };
    107 
    108   Either(const A& a) : tag(true), a(a) {}
    109   Either(const B& b) : tag(false), b(b) {}
    110 };
    111 
    112 void fred() {
    113   Either<int,Virtual> virt(0); // expected-note {{in instantiation of template}}
    114   Either<int,VirtualBase> vbase(0); // expected-note {{in instantiation of template}}
    115   Either<int,Ctor> ctor(0); // expected-note {{in instantiation of template}}
    116   Either<int,CopyCtor> copyctor(0); // expected-note {{in instantiation of template}}
    117   Either<int,CopyAssign> copyassign(0); // expected-note {{in instantiation of template}}
    118   Either<int,Dtor> dtor(0); // expected-note {{in instantiation of template}}
    119   Either<int,Okay> okay(0);
    120 }
    121