xref: /aosp_15_r20/external/clang/test/SemaCXX/cxx0x-cursory-default-delete.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li struct non_copiable {
4*67e74705SXin Li   non_copiable(const non_copiable&) = delete; // expected-note {{marked deleted here}}
5*67e74705SXin Li   non_copiable& operator = (const non_copiable&) = delete; // expected-note {{explicitly deleted}}
6*67e74705SXin Li   non_copiable() = default;
7*67e74705SXin Li };
8*67e74705SXin Li 
9*67e74705SXin Li struct non_const_copy {
10*67e74705SXin Li   non_const_copy(non_const_copy&);
11*67e74705SXin Li   non_const_copy& operator = (non_const_copy&) &;
12*67e74705SXin Li   non_const_copy& operator = (non_const_copy&) &&;
13*67e74705SXin Li   non_const_copy() = default; // expected-note {{not viable}}
14*67e74705SXin Li   int uninit_field;
15*67e74705SXin Li };
16*67e74705SXin Li non_const_copy::non_const_copy(non_const_copy&) = default; // expected-note {{not viable}}
17*67e74705SXin Li non_const_copy& non_const_copy::operator = (non_const_copy&) & = default; // expected-note {{not viable}}
18*67e74705SXin Li non_const_copy& non_const_copy::operator = (non_const_copy&) && = default; // expected-note {{not viable}}
19*67e74705SXin Li 
fn1()20*67e74705SXin Li void fn1 () {
21*67e74705SXin Li   non_copiable nc;
22*67e74705SXin Li   non_copiable nc2 = nc; // expected-error {{deleted constructor}}
23*67e74705SXin Li   nc = nc; // expected-error {{deleted operator}}
24*67e74705SXin Li 
25*67e74705SXin Li   non_const_copy ncc;
26*67e74705SXin Li   non_const_copy ncc2 = ncc;
27*67e74705SXin Li   ncc = ncc2;
28*67e74705SXin Li   const non_const_copy cncc{};
29*67e74705SXin Li   const non_const_copy cncc1; // expected-error {{default initialization of an object of const type 'const non_const_copy' without a user-provided default constructor}}
30*67e74705SXin Li   non_const_copy ncc3 = cncc; // expected-error {{no matching}}
31*67e74705SXin Li   ncc = cncc; // expected-error {{no viable overloaded}}
32*67e74705SXin Li };
33*67e74705SXin Li 
34*67e74705SXin Li struct no_fields { };
35*67e74705SXin Li struct all_init {
36*67e74705SXin Li   int a = 0;
37*67e74705SXin Li   int b = 0;
38*67e74705SXin Li };
39*67e74705SXin Li struct some_init {
40*67e74705SXin Li   int a = 0;
41*67e74705SXin Li   int b;
42*67e74705SXin Li   int c = 0;
43*67e74705SXin Li };
44*67e74705SXin Li struct some_init_mutable {
45*67e74705SXin Li   int a = 0;
46*67e74705SXin Li   mutable int b;
47*67e74705SXin Li   int c = 0;
48*67e74705SXin Li };
49*67e74705SXin Li struct some_init_def {
50*67e74705SXin Li   some_init_def() = default;
51*67e74705SXin Li   int a = 0;
52*67e74705SXin Li   int b;
53*67e74705SXin Li   int c = 0;
54*67e74705SXin Li };
55*67e74705SXin Li struct some_init_ctor {
56*67e74705SXin Li   some_init_ctor();
57*67e74705SXin Li   int a = 0;
58*67e74705SXin Li   int b;
59*67e74705SXin Li   int c = 0;
60*67e74705SXin Li };
61*67e74705SXin Li struct sub_some_init : public some_init_def { };
62*67e74705SXin Li struct sub_some_init_ctor : public some_init_def {
63*67e74705SXin Li   sub_some_init_ctor();
64*67e74705SXin Li };
65*67e74705SXin Li struct sub_some_init_ctor2 : public some_init_ctor {
66*67e74705SXin Li };
67*67e74705SXin Li struct some_init_container {
68*67e74705SXin Li   some_init_def sid;
69*67e74705SXin Li };
70*67e74705SXin Li struct some_init_container_ctor {
71*67e74705SXin Li   some_init_container_ctor();
72*67e74705SXin Li   some_init_def sid;
73*67e74705SXin Li };
74*67e74705SXin Li struct no_fields_container {
75*67e74705SXin Li   no_fields nf;
76*67e74705SXin Li };
77*67e74705SXin Li struct param_pack_ctor {
78*67e74705SXin Li   template <typename... T>
79*67e74705SXin Li   param_pack_ctor(T...);
80*67e74705SXin Li   int n;
81*67e74705SXin Li };
82*67e74705SXin Li struct param_pack_ctor_field {
83*67e74705SXin Li   param_pack_ctor ndc;
84*67e74705SXin Li };
85*67e74705SXin Li struct multi_param_pack_ctor {
86*67e74705SXin Li   template <typename... T, typename... U>
87*67e74705SXin Li   multi_param_pack_ctor(T..., U..., int f = 0);
88*67e74705SXin Li   int n;
89*67e74705SXin Li };
90*67e74705SXin Li struct ignored_template_ctor_and_def {
91*67e74705SXin Li   template <class T> ignored_template_ctor_and_def(T* f = nullptr);
92*67e74705SXin Li   ignored_template_ctor_and_def() = default;
93*67e74705SXin Li   int field;
94*67e74705SXin Li };
95*67e74705SXin Li template<bool, typename = void> struct enable_if {};
96*67e74705SXin Li template<typename T> struct enable_if<true, T> { typedef T type; };
97*67e74705SXin Li struct multi_param_pack_and_defaulted {
98*67e74705SXin Li   template <typename... T,
99*67e74705SXin Li             typename enable_if<sizeof...(T) != 0>::type* = nullptr>
100*67e74705SXin Li   multi_param_pack_and_defaulted(T...);
101*67e74705SXin Li   multi_param_pack_and_defaulted() = default;
102*67e74705SXin Li   int n;
103*67e74705SXin Li };
104*67e74705SXin Li 
constobjs()105*67e74705SXin Li void constobjs() {
106*67e74705SXin Li   const no_fields nf; // ok
107*67e74705SXin Li   const all_init ai; // ok
108*67e74705SXin Li   const some_init si; // expected-error {{default initialization of an object of const type 'const some_init' without a user-provided default constructor}}
109*67e74705SXin Li   const some_init_mutable sim; // ok
110*67e74705SXin Li   const some_init_def sid; // expected-error {{default initialization of an object of const type 'const some_init_def' without a user-provided default constructor}}
111*67e74705SXin Li   const some_init_ctor sic; // ok
112*67e74705SXin Li   const sub_some_init ssi; // expected-error {{default initialization of an object of const type 'const sub_some_init' without a user-provided default constructor}}
113*67e74705SXin Li   const sub_some_init_ctor ssic; // ok
114*67e74705SXin Li   const sub_some_init_ctor2 ssic2; // ok
115*67e74705SXin Li   const some_init_container sicon; // expected-error {{default initialization of an object of const type 'const some_init_container' without a user-provided default constructor}}
116*67e74705SXin Li   const some_init_container_ctor siconc; // ok
117*67e74705SXin Li   const no_fields_container nfc; // ok
118*67e74705SXin Li   const param_pack_ctor ppc; // ok
119*67e74705SXin Li   const param_pack_ctor_field ppcf; // ok
120*67e74705SXin Li   const multi_param_pack_ctor mppc; // ok
121*67e74705SXin Li   const multi_param_pack_and_defaulted mppad; // expected-error {{default initialization of an object of const type 'const multi_param_pack_and_defaulted' without a user-provided default constructor}}
122*67e74705SXin Li   const ignored_template_ctor_and_def itcad; // expected-error {{default initialization of an object of const type 'const ignored_template_ctor_and_def' without a user-provided default constructor}}
123*67e74705SXin Li 
124*67e74705SXin Li }
125*67e74705SXin Li 
126*67e74705SXin Li struct non_const_derived : non_const_copy {
127*67e74705SXin Li   non_const_derived(const non_const_derived&) = default; // expected-error {{requires it to be non-const}}
128*67e74705SXin Li   non_const_derived& operator =(non_const_derived&) = default;
129*67e74705SXin Li };
130*67e74705SXin Li 
131*67e74705SXin Li struct bad_decls {
132*67e74705SXin Li   bad_decls(volatile bad_decls&) = default; // expected-error {{may not be volatile}}
133*67e74705SXin Li   bad_decls&& operator = (bad_decls) = default; // expected-error {{lvalue reference}} expected-error {{must return 'bad_decls &'}}
134*67e74705SXin Li   bad_decls& operator = (volatile bad_decls&) = default; // expected-error {{may not be volatile}}
135*67e74705SXin Li   bad_decls& operator = (const bad_decls&) const = default; // expected-error {{may not have 'const', 'constexpr' or 'volatile' qualifiers}}
136*67e74705SXin Li };
137*67e74705SXin Li 
138*67e74705SXin Li struct DefaultDelete {
139*67e74705SXin Li   DefaultDelete() = default; // expected-note {{previous declaration is here}}
140*67e74705SXin Li   DefaultDelete() = delete; // expected-error {{constructor cannot be redeclared}}
141*67e74705SXin Li 
142*67e74705SXin Li   ~DefaultDelete() = default; // expected-note {{previous declaration is here}}
143*67e74705SXin Li   ~DefaultDelete() = delete; // expected-error {{destructor cannot be redeclared}}
144*67e74705SXin Li 
145*67e74705SXin Li   DefaultDelete &operator=(const DefaultDelete &) = default; // expected-note {{previous declaration is here}}
146*67e74705SXin Li   DefaultDelete &operator=(const DefaultDelete &) = delete; // expected-error {{class member cannot be redeclared}}
147*67e74705SXin Li };
148*67e74705SXin Li 
149*67e74705SXin Li struct DeleteDefault {
150*67e74705SXin Li   DeleteDefault() = delete; // expected-note {{previous definition is here}}
151*67e74705SXin Li   DeleteDefault() = default; // expected-error {{constructor cannot be redeclared}}
152*67e74705SXin Li 
153*67e74705SXin Li   ~DeleteDefault() = delete; // expected-note {{previous definition is here}}
154*67e74705SXin Li   ~DeleteDefault() = default; // expected-error {{destructor cannot be redeclared}}
155*67e74705SXin Li 
156*67e74705SXin Li   DeleteDefault &operator=(const DeleteDefault &) = delete; // expected-note {{previous definition is here}}
157*67e74705SXin Li   DeleteDefault &operator=(const DeleteDefault &) = default; // expected-error {{class member cannot be redeclared}}
158*67e74705SXin Li };
159*67e74705SXin Li 
160*67e74705SXin Li struct A {}; struct B {};
161*67e74705SXin Li 
162*67e74705SXin Li struct except_spec_a {
163*67e74705SXin Li   virtual ~except_spec_a() throw(A);
164*67e74705SXin Li   except_spec_a() throw(A);
165*67e74705SXin Li };
166*67e74705SXin Li struct except_spec_b {
167*67e74705SXin Li   virtual ~except_spec_b() throw(B);
168*67e74705SXin Li   except_spec_b() throw(B);
169*67e74705SXin Li };
170*67e74705SXin Li 
171*67e74705SXin Li struct except_spec_d_good : except_spec_a, except_spec_b {
172*67e74705SXin Li   ~except_spec_d_good();
173*67e74705SXin Li };
174*67e74705SXin Li except_spec_d_good::~except_spec_d_good() = default;
175*67e74705SXin Li struct except_spec_d_good2 : except_spec_a, except_spec_b {
176*67e74705SXin Li   ~except_spec_d_good2() = default;
177*67e74705SXin Li };
178*67e74705SXin Li struct except_spec_d_bad : except_spec_a, except_spec_b {
179*67e74705SXin Li   ~except_spec_d_bad() noexcept;
180*67e74705SXin Li };
181*67e74705SXin Li // FIXME: This should error because this exception spec is not
182*67e74705SXin Li // compatible with the implicit exception spec.
183*67e74705SXin Li except_spec_d_bad::~except_spec_d_bad() noexcept = default;
184*67e74705SXin Li 
185*67e74705SXin Li // FIXME: This should error because this exception spec is not
186*67e74705SXin Li // compatible with the implicit exception spec.
187*67e74705SXin Li struct except_spec_d_mismatch : except_spec_a, except_spec_b {
188*67e74705SXin Li   except_spec_d_mismatch() throw(A) = default;
189*67e74705SXin Li };
190*67e74705SXin Li struct except_spec_d_match : except_spec_a, except_spec_b {
191*67e74705SXin Li   except_spec_d_match() throw(A, B) = default;
192*67e74705SXin Li };
193*67e74705SXin Li 
194*67e74705SXin Li // gcc-compatibility: allow attributes on default definitions
195*67e74705SXin Li // (but not normal definitions)
196*67e74705SXin Li struct S { S(); };
197*67e74705SXin Li S::S() __attribute((pure)) = default;
198*67e74705SXin Li 
199*67e74705SXin Li using size_t = decltype(sizeof(0));
200*67e74705SXin Li void *operator new(size_t) = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}
201*67e74705SXin Li void operator delete(void *) noexcept = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}
202