xref: /minix3/external/bsd/llvm/dist/clang/test/CXX/class/class.union/p1.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc void abort() __attribute__((noreturn));
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc class Okay {
6*f4a2713aSLionel Sambuc   int a_;
7*f4a2713aSLionel Sambuc };
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc class Virtual {
foo()10*f4a2713aSLionel Sambuc   virtual void foo() { abort(); } // expected-note 4 {{because type 'Virtual' has a virtual member function}}
11*f4a2713aSLionel Sambuc };
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc class VirtualBase : virtual Okay { // expected-note 4 {{because type 'VirtualBase' has a virtual base class}}
14*f4a2713aSLionel Sambuc };
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc class Ctor {
Ctor()17*f4a2713aSLionel Sambuc   Ctor() { abort(); } // expected-note 2{{because type 'Ctor' has a user-provided default constructor}} expected-note 2{{here}}
18*f4a2713aSLionel Sambuc };
19*f4a2713aSLionel Sambuc class Ctor2 {
20*f4a2713aSLionel Sambuc   Ctor2(); // expected-note {{because type 'Ctor2' has a user-provided default constructor}} expected-note 2{{here}}
21*f4a2713aSLionel Sambuc };
22*f4a2713aSLionel Sambuc class CtorTmpl { // expected-note {{because type 'CtorTmpl' has no default constructor}}
23*f4a2713aSLionel Sambuc   template<typename T> CtorTmpl(); // expected-note {{implicit default constructor suppressed by user-declared constructor}}
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc class CopyCtor { // expected-note 2{{because no constructor can be used to copy an object of type 'const CopyCtor'}}
CopyCtor(CopyCtor & cc)27*f4a2713aSLionel Sambuc   CopyCtor(CopyCtor &cc) { abort(); }
28*f4a2713aSLionel Sambuc };
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc class CopyAssign { // expected-note 2 {{because no assignment operator can be used to copy an object of type 'const CopyAssign'}}
operator =(CopyAssign & CA)31*f4a2713aSLionel Sambuc   CopyAssign& operator=(CopyAssign& CA) { abort(); }
32*f4a2713aSLionel Sambuc };
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc class Dtor {
~Dtor()35*f4a2713aSLionel Sambuc   ~Dtor() { abort(); } // expected-note 2 {{because type 'Dtor' has a user-provided destructor}} expected-note 2{{here}}
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc union U1 {
39*f4a2713aSLionel Sambuc   Virtual v; // expected-error {{union member 'v' has a non-trivial copy constructor}}
40*f4a2713aSLionel Sambuc   VirtualBase vbase; // expected-error {{union member 'vbase' has a non-trivial copy constructor}}
41*f4a2713aSLionel Sambuc   Ctor ctor; // expected-error {{union member 'ctor' has a non-trivial constructor}}
42*f4a2713aSLionel Sambuc   Ctor2 ctor2; // expected-error {{union member 'ctor2' has a non-trivial constructor}}
43*f4a2713aSLionel Sambuc   CtorTmpl ctortmpl; // expected-error {{union member 'ctortmpl' has a non-trivial constructor}}
44*f4a2713aSLionel Sambuc   CopyCtor copyctor; // expected-error {{union member 'copyctor' has a non-trivial copy constructor}}
45*f4a2713aSLionel Sambuc   CopyAssign copyassign; // expected-error {{union member 'copyassign' has a non-trivial copy assignment operator}}
46*f4a2713aSLionel Sambuc   Dtor dtor; // expected-error {{union member 'dtor' has a non-trivial destructor}}
47*f4a2713aSLionel Sambuc   Okay okay;
48*f4a2713aSLionel Sambuc };
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc union U2 {
51*f4a2713aSLionel Sambuc   struct {
52*f4a2713aSLionel Sambuc     Virtual v; // expected-note {{because the function selected to copy field of type 'Virtual' is not trivial}}
53*f4a2713aSLionel Sambuc   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
54*f4a2713aSLionel Sambuc   struct {
55*f4a2713aSLionel Sambuc     VirtualBase vbase; // expected-note {{because the function selected to copy field of type 'VirtualBase' is not trivial}}
56*f4a2713aSLionel Sambuc   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
57*f4a2713aSLionel Sambuc   struct {
58*f4a2713aSLionel Sambuc     Ctor ctor; // expected-note {{because field of type 'Ctor' has a user-provided default constructor}}
59*f4a2713aSLionel Sambuc   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
60*f4a2713aSLionel Sambuc   struct {
61*f4a2713aSLionel Sambuc     Ctor2 ctor2; // expected-note {{because field of type 'Ctor2' has a user-provided default constructor}}
62*f4a2713aSLionel Sambuc   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
63*f4a2713aSLionel Sambuc   struct { // expected-note {{no constructor can be used to copy an object of type 'const}}
64*f4a2713aSLionel Sambuc     CopyCtor copyctor;
65*f4a2713aSLionel Sambuc   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
66*f4a2713aSLionel Sambuc   struct { // expected-note {{no assignment operator can be used to copy an object of type 'const}}
67*f4a2713aSLionel Sambuc     CopyAssign copyassign;
68*f4a2713aSLionel Sambuc   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
69*f4a2713aSLionel Sambuc   struct {
70*f4a2713aSLionel Sambuc     Dtor dtor; // expected-note {{because field of type 'Dtor' has a user-provided destructor}}
71*f4a2713aSLionel Sambuc   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
72*f4a2713aSLionel Sambuc   struct {
73*f4a2713aSLionel Sambuc     Okay okay;
74*f4a2713aSLionel Sambuc   } m7;
75*f4a2713aSLionel Sambuc };
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc union U3 {
78*f4a2713aSLionel Sambuc   struct s1 : Virtual { // expected-note {{because the function selected to copy base class of type 'Virtual' is not trivial}}
79*f4a2713aSLionel Sambuc   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
80*f4a2713aSLionel Sambuc   struct s2 : VirtualBase { // expected-note {{because the function selected to copy base class of type 'VirtualBase' is not trivial}}
81*f4a2713aSLionel Sambuc   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
82*f4a2713aSLionel Sambuc   struct s3 : Ctor { // expected-note {{because base class of type 'Ctor' has a user-provided default constructor}}
83*f4a2713aSLionel Sambuc   } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
84*f4a2713aSLionel Sambuc   struct s3a : Ctor2 { // expected-note {{because base class of type 'Ctor2' has a user-provided default constructor}}
85*f4a2713aSLionel Sambuc   } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
86*f4a2713aSLionel Sambuc   struct s4 : CopyCtor { // expected-note {{because no constructor can be used to copy an object of type 'const U3::s4'}}
87*f4a2713aSLionel Sambuc   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
88*f4a2713aSLionel Sambuc   struct s5 : CopyAssign { // expected-note {{because no assignment operator can be used to copy an object of type 'const U3::s5'}}
89*f4a2713aSLionel Sambuc   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
90*f4a2713aSLionel Sambuc   struct s6 : Dtor { // expected-note {{because base class of type 'Dtor' has a user-provided destructor}}
91*f4a2713aSLionel Sambuc   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
92*f4a2713aSLionel Sambuc   struct s7 : Okay {
93*f4a2713aSLionel Sambuc   } m7;
94*f4a2713aSLionel Sambuc   struct s8 {
95*f4a2713aSLionel Sambuc     s8(...) = delete; // expected-note {{because it is a variadic function}} expected-warning {{C++11}}
96*f4a2713aSLionel Sambuc   } m8; // expected-error {{union member 'm8' has a non-trivial constructor}}
97*f4a2713aSLionel Sambuc };
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc union U4 {
100*f4a2713aSLionel Sambuc   static int i1; // expected-warning {{static data member 'i1' in union is a C++11 extension}}
101*f4a2713aSLionel Sambuc };
102*f4a2713aSLionel Sambuc int U4::i1 = 10;
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc union U5 {
105*f4a2713aSLionel Sambuc   int& i1; // expected-error {{union member 'i1' has reference type 'int &'}}
106*f4a2713aSLionel Sambuc };
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc union U6 {
109*f4a2713aSLionel Sambuc   struct S {
110*f4a2713aSLionel Sambuc     int &i;
111*f4a2713aSLionel Sambuc   } s; // ok
112*f4a2713aSLionel Sambuc };
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc template <class A, class B> struct Either {
115*f4a2713aSLionel Sambuc   bool tag;
116*f4a2713aSLionel Sambuc   union { // expected-note 6 {{in instantiation of member class}}
117*f4a2713aSLionel Sambuc     A a;
118*f4a2713aSLionel Sambuc     B b; // expected-error 6 {{non-trivial}}
119*f4a2713aSLionel Sambuc   };
120*f4a2713aSLionel Sambuc 
EitherEither121*f4a2713aSLionel Sambuc   Either(const A& a) : tag(true), a(a) {}
EitherEither122*f4a2713aSLionel Sambuc   Either(const B& b) : tag(false), b(b) {}
123*f4a2713aSLionel Sambuc };
124*f4a2713aSLionel Sambuc 
fred()125*f4a2713aSLionel Sambuc void fred() {
126*f4a2713aSLionel Sambuc   Either<int,Virtual> virt(0); // expected-note {{in instantiation of template}}
127*f4a2713aSLionel Sambuc   Either<int,VirtualBase> vbase(0); // expected-note {{in instantiation of template}}
128*f4a2713aSLionel Sambuc   Either<int,Ctor> ctor(0); // expected-note {{in instantiation of template}}
129*f4a2713aSLionel Sambuc   Either<int,CopyCtor> copyctor(0); // expected-note {{in instantiation of template}}
130*f4a2713aSLionel Sambuc   Either<int,CopyAssign> copyassign(0); // expected-note {{in instantiation of template}}
131*f4a2713aSLionel Sambuc   Either<int,Dtor> dtor(0); // expected-note {{in instantiation of template}}
132*f4a2713aSLionel Sambuc   Either<int,Okay> okay(0);
133*f4a2713aSLionel Sambuc }
134