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