xref: /llvm-project/clang/test/CXX/class/class.init/class.copy.elision/p3.cpp (revision ba15d186e5cef2620d562c6c9d9a6d570382cd0a)
1*ba15d186SMark de Wever // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx11_23,cxx23    %s
2*ba15d186SMark de Wever // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx98_20,cxx11_23,cxx11_20 %s
3*ba15d186SMark de Wever // RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx98_20,cxx11_23,cxx11_20 %s
47d2d5a3aSMatheus Izvekov // RUN: %clang_cc1 -std=c++98 -fsyntax-only -fcxx-exceptions -Wno-c++11-extensions -verify=expected,cxx98_20,cxx98 %s
574f93bc3SYang Fan 
674f93bc3SYang Fan namespace test_delete_function {
774f93bc3SYang Fan struct A1 {
874f93bc3SYang Fan   A1();
974f93bc3SYang Fan   A1(const A1 &);
1003282f2fSMatheus Izvekov   A1(A1 &&) = delete; // expected-note {{'A1' has been explicitly marked deleted here}}
1174f93bc3SYang Fan };
test1()1274f93bc3SYang Fan A1 test1() {
1374f93bc3SYang Fan   A1 a;
1415f3cd6bSMatheus Izvekov   return a; // expected-error {{call to deleted constructor of 'A1'}}
1574f93bc3SYang Fan }
1674f93bc3SYang Fan 
1774f93bc3SYang Fan struct A2 {
1874f93bc3SYang Fan   A2();
1974f93bc3SYang Fan   A2(const A2 &);
2074f93bc3SYang Fan 
2174f93bc3SYang Fan private:
2203282f2fSMatheus Izvekov   A2(A2 &&); // expected-note {{declared private here}}
2374f93bc3SYang Fan };
test2()2474f93bc3SYang Fan A2 test2() {
2574f93bc3SYang Fan   A2 a;
2603282f2fSMatheus Izvekov   return a; // expected-error {{calling a private constructor of class 'test_delete_function::A2'}}
2774f93bc3SYang Fan }
2874f93bc3SYang Fan 
2974f93bc3SYang Fan struct C {};
3074f93bc3SYang Fan 
3174f93bc3SYang Fan struct B1 {
3274f93bc3SYang Fan   B1(C &);
3303282f2fSMatheus Izvekov   B1(C &&) = delete; // expected-note {{'B1' has been explicitly marked deleted here}}
3474f93bc3SYang Fan };
test3()3574f93bc3SYang Fan B1 test3() {
3674f93bc3SYang Fan   C c;
3715f3cd6bSMatheus Izvekov   return c; // expected-error {{conversion function from 'C' to 'B1' invokes a deleted function}}
3874f93bc3SYang Fan }
3974f93bc3SYang Fan 
4074f93bc3SYang Fan struct B2 {
4174f93bc3SYang Fan   B2(C &);
4274f93bc3SYang Fan 
4374f93bc3SYang Fan private:
4403282f2fSMatheus Izvekov   B2(C &&); // expected-note {{declared private here}}
4574f93bc3SYang Fan };
test4()4674f93bc3SYang Fan B2 test4() {
4774f93bc3SYang Fan   C c;
4803282f2fSMatheus Izvekov   return c; // expected-error {{calling a private constructor of class 'test_delete_function::B2'}}
4974f93bc3SYang Fan }
5074f93bc3SYang Fan } // namespace test_delete_function
51fbee4a0cSYang Fan 
527d2d5a3aSMatheus Izvekov // Implicitly movable entity can be rvalue reference to non-volatile
53fbee4a0cSYang Fan // automatic object.
54fbee4a0cSYang Fan namespace test_implicitly_movable_rvalue_ref {
55fbee4a0cSYang Fan struct A1 {
56fbee4a0cSYang Fan   A1(A1 &&);
5703282f2fSMatheus Izvekov   A1(const A1 &) = delete;
58fbee4a0cSYang Fan };
test1(A1 && a)59fbee4a0cSYang Fan A1 test1(A1 &&a) {
6003282f2fSMatheus Izvekov   return a;
61fbee4a0cSYang Fan }
62fbee4a0cSYang Fan 
63fbee4a0cSYang Fan struct A2 {
64fbee4a0cSYang Fan   A2(A2 &&);
65fbee4a0cSYang Fan 
66fbee4a0cSYang Fan private:
6703282f2fSMatheus Izvekov   A2(const A2 &);
68fbee4a0cSYang Fan };
test2(A2 && a)69fbee4a0cSYang Fan A2 test2(A2 &&a) {
7003282f2fSMatheus Izvekov   return a;
71fbee4a0cSYang Fan }
72fbee4a0cSYang Fan 
73fbee4a0cSYang Fan struct B1 {
74fbee4a0cSYang Fan   B1(const B1 &);
7503282f2fSMatheus Izvekov   B1(B1 &&) = delete; // expected-note {{'B1' has been explicitly marked deleted here}}
76fbee4a0cSYang Fan };
test3(B1 && b)77fbee4a0cSYang Fan B1 test3(B1 &&b) {
7815f3cd6bSMatheus Izvekov   return b; // expected-error {{call to deleted constructor of 'B1'}}
79fbee4a0cSYang Fan }
80fbee4a0cSYang Fan 
81fbee4a0cSYang Fan struct B2 {
82fbee4a0cSYang Fan   B2(const B2 &);
83fbee4a0cSYang Fan 
84fbee4a0cSYang Fan private:
8503282f2fSMatheus Izvekov   B2(B2 &&); // expected-note {{declared private here}}
86fbee4a0cSYang Fan };
test4(B2 && b)87fbee4a0cSYang Fan B2 test4(B2 &&b) {
8803282f2fSMatheus Izvekov   return b; // expected-error {{calling a private constructor of class 'test_implicitly_movable_rvalue_ref::B2'}}
89fbee4a0cSYang Fan }
90fbee4a0cSYang Fan } // namespace test_implicitly_movable_rvalue_ref
91fbee4a0cSYang Fan 
927d2d5a3aSMatheus Izvekov // Operand of throw-expression can be function parameter or
93fbee4a0cSYang Fan // catch-clause parameter.
94fbee4a0cSYang Fan namespace test_throw_parameter {
95fbee4a0cSYang Fan void func();
96fbee4a0cSYang Fan 
97fbee4a0cSYang Fan struct A1 {
98fbee4a0cSYang Fan   A1(const A1 &);
99f62433f1SMatheus Izvekov   A1(A1 &&) = delete;
100f62433f1SMatheus Izvekov   // expected-note@-1 2{{'A1' has been explicitly marked deleted here}}
101*ba15d186SMark de Wever   // cxx11_23-note@-2 3{{'A1' has been explicitly marked deleted here}}
102fbee4a0cSYang Fan };
test1()103fbee4a0cSYang Fan void test1() {
104fbee4a0cSYang Fan   try {
105fbee4a0cSYang Fan     func();
106fbee4a0cSYang Fan   } catch (A1 a) {
10715f3cd6bSMatheus Izvekov     throw a; // expected-error {{call to deleted constructor of 'A1'}}
108fbee4a0cSYang Fan   }
109fbee4a0cSYang Fan }
110fbee4a0cSYang Fan 
111fbee4a0cSYang Fan struct A2 {
112fbee4a0cSYang Fan   A2(const A2 &);
113fbee4a0cSYang Fan 
114fbee4a0cSYang Fan private:
11503282f2fSMatheus Izvekov   A2(A2 &&); // expected-note {{declared private here}}
116fbee4a0cSYang Fan };
test2()117fbee4a0cSYang Fan void test2() {
118fbee4a0cSYang Fan   try {
119fbee4a0cSYang Fan     func();
120fbee4a0cSYang Fan   } catch (A2 a) {
12103282f2fSMatheus Izvekov     throw a; // expected-error {{calling a private constructor of class 'test_throw_parameter::A2'}}
122fbee4a0cSYang Fan   }
123fbee4a0cSYang Fan }
12418192228SMatheus Izvekov 
test3(A1 a)12518192228SMatheus Izvekov void test3(A1 a) try {
12618192228SMatheus Izvekov   func();
12718192228SMatheus Izvekov } catch (...) {
12815f3cd6bSMatheus Izvekov   throw a; // expected-error {{call to deleted constructor of 'A1'}}
12918192228SMatheus Izvekov }
130f62433f1SMatheus Izvekov 
131f62433f1SMatheus Izvekov #if __cplusplus >= 201103L
132f62433f1SMatheus Izvekov namespace PR54341 {
test4(A1 a)133f62433f1SMatheus Izvekov void test4(A1 a) {
134f62433f1SMatheus Izvekov   void f(decltype((throw a, 0)));
13515f3cd6bSMatheus Izvekov   // expected-error@-1 {{call to deleted constructor of 'A1'}}
136f62433f1SMatheus Izvekov 
137f62433f1SMatheus Izvekov   void g(int = decltype(throw a, 0){});
13815f3cd6bSMatheus Izvekov   // expected-error@-1 {{call to deleted constructor of 'A1'}}
139f62433f1SMatheus Izvekov }
140f62433f1SMatheus Izvekov 
test5(A1 a,int=decltype(throw a,0) {})141f62433f1SMatheus Izvekov void test5(A1 a, int = decltype(throw a, 0){}) {}
14215f3cd6bSMatheus Izvekov // expected-error@-1 {{call to deleted constructor of 'A1'}}
143f62433f1SMatheus Izvekov } // namespace PR54341
144f62433f1SMatheus Izvekov #endif
145f62433f1SMatheus Izvekov 
146fbee4a0cSYang Fan } // namespace test_throw_parameter
147fbee4a0cSYang Fan 
1487d2d5a3aSMatheus Izvekov // During the first overload resolution, the selected function no
149fbee4a0cSYang Fan // need to be a constructor.
150fbee4a0cSYang Fan namespace test_non_ctor_conversion {
151fbee4a0cSYang Fan class C {};
152fbee4a0cSYang Fan 
153fbee4a0cSYang Fan struct A1 {
154fbee4a0cSYang Fan   operator C() &&;
15503282f2fSMatheus Izvekov   operator C() const & = delete;
156fbee4a0cSYang Fan };
test1()157fbee4a0cSYang Fan C test1() {
158fbee4a0cSYang Fan   A1 a;
15903282f2fSMatheus Izvekov   return a;
160fbee4a0cSYang Fan }
161fbee4a0cSYang Fan 
162fbee4a0cSYang Fan struct A2 {
163fbee4a0cSYang Fan   operator C() &&;
164fbee4a0cSYang Fan 
165fbee4a0cSYang Fan private:
16603282f2fSMatheus Izvekov   operator C() const &;
167fbee4a0cSYang Fan };
test2()168fbee4a0cSYang Fan C test2() {
169fbee4a0cSYang Fan   A2 a;
17003282f2fSMatheus Izvekov   return a;
171fbee4a0cSYang Fan }
172fbee4a0cSYang Fan 
173fbee4a0cSYang Fan struct B1 {
174fbee4a0cSYang Fan   operator C() const &;
17503282f2fSMatheus Izvekov   operator C() && = delete; // expected-note {{'operator C' has been explicitly marked deleted here}}
176fbee4a0cSYang Fan };
test3()177fbee4a0cSYang Fan C test3() {
178fbee4a0cSYang Fan   B1 b;
17915f3cd6bSMatheus Izvekov   return b; // expected-error {{conversion function from 'B1' to 'C' invokes a deleted function}}
180fbee4a0cSYang Fan }
181fbee4a0cSYang Fan 
182fbee4a0cSYang Fan struct B2 {
183fbee4a0cSYang Fan   operator C() const &;
184fbee4a0cSYang Fan 
185fbee4a0cSYang Fan private:
18603282f2fSMatheus Izvekov   operator C() &&; // expected-note {{declared private here}}
187fbee4a0cSYang Fan };
test4()188fbee4a0cSYang Fan C test4() {
189fbee4a0cSYang Fan   B2 b;
19003282f2fSMatheus Izvekov   return b; // expected-error {{'operator C' is a private member of 'test_non_ctor_conversion::B2'}}
191fbee4a0cSYang Fan }
192fbee4a0cSYang Fan } // namespace test_non_ctor_conversion
193fbee4a0cSYang Fan 
1947d2d5a3aSMatheus Izvekov // During the first overload resolution, the first parameter of the
195fbee4a0cSYang Fan // selected function no need to be an rvalue reference to the object's type.
196fbee4a0cSYang Fan namespace test_ctor_param_rvalue_ref {
197fbee4a0cSYang Fan struct A1;
198fbee4a0cSYang Fan struct A2;
199fbee4a0cSYang Fan struct B1;
200fbee4a0cSYang Fan struct B2;
201fbee4a0cSYang Fan 
202fbee4a0cSYang Fan struct NeedRvalueRef {
203fbee4a0cSYang Fan   NeedRvalueRef(A1 &&);
204fbee4a0cSYang Fan   NeedRvalueRef(A2 &&);
205fbee4a0cSYang Fan   NeedRvalueRef(B1 &&);
206fbee4a0cSYang Fan   NeedRvalueRef(B2 &&);
207fbee4a0cSYang Fan };
208fbee4a0cSYang Fan struct NeedValue {
2097d2d5a3aSMatheus Izvekov   NeedValue(A1); // cxx98-note 2 {{passing argument to parameter here}}
210fbee4a0cSYang Fan   NeedValue(A2);
211*ba15d186SMark de Wever   NeedValue(B1); // cxx11_23-note 2 {{passing argument to parameter here}}
212fbee4a0cSYang Fan   NeedValue(B2);
213fbee4a0cSYang Fan };
214fbee4a0cSYang Fan 
215fbee4a0cSYang Fan struct A1 {
216fbee4a0cSYang Fan   A1();
217fbee4a0cSYang Fan   A1(A1 &&);
21803282f2fSMatheus Izvekov   A1(const A1 &) = delete; // cxx98-note 2 {{marked deleted here}}
219fbee4a0cSYang Fan };
test_1_1()220fbee4a0cSYang Fan NeedValue test_1_1() {
221fbee4a0cSYang Fan   // not rvalue reference
222fbee4a0cSYang Fan   // same type
223fbee4a0cSYang Fan   A1 a;
2247d2d5a3aSMatheus Izvekov   return a; // cxx98-error {{call to deleted constructor}}
225fbee4a0cSYang Fan }
226fbee4a0cSYang Fan class DerivedA1 : public A1 {};
test_1_2()227fbee4a0cSYang Fan A1 test_1_2() {
228fbee4a0cSYang Fan   // rvalue reference
229fbee4a0cSYang Fan   // not same type
230fbee4a0cSYang Fan   DerivedA1 a;
23103282f2fSMatheus Izvekov   return a;
232fbee4a0cSYang Fan }
test_1_3()233fbee4a0cSYang Fan NeedValue test_1_3() {
234fbee4a0cSYang Fan   // not rvalue reference
235fbee4a0cSYang Fan   // not same type
236fbee4a0cSYang Fan   DerivedA1 a;
2377d2d5a3aSMatheus Izvekov   return a; // cxx98-error {{call to deleted constructor}}
238fbee4a0cSYang Fan }
239fbee4a0cSYang Fan 
240fbee4a0cSYang Fan struct A2 {
241fbee4a0cSYang Fan   A2();
242fbee4a0cSYang Fan   A2(A2 &&);
243fbee4a0cSYang Fan 
244fbee4a0cSYang Fan private:
24503282f2fSMatheus Izvekov   A2(const A2 &); // cxx98-note 2 {{declared private here}}
246fbee4a0cSYang Fan };
test_2_1()247fbee4a0cSYang Fan NeedValue test_2_1() {
248fbee4a0cSYang Fan   // not rvalue reference
249fbee4a0cSYang Fan   // same type
250fbee4a0cSYang Fan   A2 a;
2517d2d5a3aSMatheus Izvekov   return a; // cxx98-error {{calling a private constructor}}
252fbee4a0cSYang Fan }
253fbee4a0cSYang Fan class DerivedA2 : public A2 {};
test_2_2()254fbee4a0cSYang Fan A2 test_2_2() {
255fbee4a0cSYang Fan   // rvalue reference
256fbee4a0cSYang Fan   // not same type
257fbee4a0cSYang Fan   DerivedA2 a;
25803282f2fSMatheus Izvekov   return a;
259fbee4a0cSYang Fan }
test_2_3()260fbee4a0cSYang Fan NeedValue test_2_3() {
261fbee4a0cSYang Fan   // not rvalue reference
262fbee4a0cSYang Fan   // not same type
263fbee4a0cSYang Fan   DerivedA2 a;
2647d2d5a3aSMatheus Izvekov   return a; // cxx98-error {{calling a private constructor}}
265fbee4a0cSYang Fan }
266fbee4a0cSYang Fan 
267fbee4a0cSYang Fan struct B1 {
268fbee4a0cSYang Fan   B1();
269fbee4a0cSYang Fan   B1(const B1 &);
270*ba15d186SMark de Wever   B1(B1 &&) = delete; // cxx11_23-note 3 {{'B1' has been explicitly marked deleted here}}
27103282f2fSMatheus Izvekov                       // cxx98-note@-1 {{'B1' has been explicitly marked deleted here}}
272fbee4a0cSYang Fan };
test_3_1()273fbee4a0cSYang Fan NeedValue test_3_1() {
274fbee4a0cSYang Fan   // not rvalue reference
275fbee4a0cSYang Fan   // same type
276fbee4a0cSYang Fan   B1 b;
277*ba15d186SMark de Wever   return b; // cxx11_23-error {{call to deleted constructor of 'B1'}}
278fbee4a0cSYang Fan }
279fbee4a0cSYang Fan class DerivedB1 : public B1 {};
test_3_2()280fbee4a0cSYang Fan B1 test_3_2() {
281fbee4a0cSYang Fan   // rvalue reference
282fbee4a0cSYang Fan   // not same type
283fbee4a0cSYang Fan   DerivedB1 b;
28415f3cd6bSMatheus Izvekov   return b; // expected-error {{call to deleted constructor of 'B1'}}
285fbee4a0cSYang Fan }
test_3_3()286fbee4a0cSYang Fan NeedValue test_3_3() {
287fbee4a0cSYang Fan   // not rvalue reference
288fbee4a0cSYang Fan   // not same type
289fbee4a0cSYang Fan   DerivedB1 b;
290*ba15d186SMark de Wever   return b; // cxx11_23-error {{call to deleted constructor of 'B1'}}
291fbee4a0cSYang Fan }
292fbee4a0cSYang Fan 
293fbee4a0cSYang Fan struct B2 {
294fbee4a0cSYang Fan   B2();
295fbee4a0cSYang Fan   B2(const B2 &);
296fbee4a0cSYang Fan 
297fbee4a0cSYang Fan private:
298*ba15d186SMark de Wever   B2(B2 &&); // cxx11_23-note 3 {{declared private here}}
29903282f2fSMatheus Izvekov              // cxx98-note@-1 {{declared private here}}
300fbee4a0cSYang Fan };
test_4_1()301fbee4a0cSYang Fan NeedValue test_4_1() {
302fbee4a0cSYang Fan   // not rvalue reference
303fbee4a0cSYang Fan   // same type
304fbee4a0cSYang Fan   B2 b;
305*ba15d186SMark de Wever   return b; // cxx11_23-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
306fbee4a0cSYang Fan }
307fbee4a0cSYang Fan class DerivedB2 : public B2 {};
test_4_2()308fbee4a0cSYang Fan B2 test_4_2() {
309fbee4a0cSYang Fan   // rvalue reference
310fbee4a0cSYang Fan   // not same type
311fbee4a0cSYang Fan   DerivedB2 b;
31203282f2fSMatheus Izvekov   return b; // expected-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
313fbee4a0cSYang Fan }
test_4_3()314fbee4a0cSYang Fan NeedValue test_4_3() {
315fbee4a0cSYang Fan   // not rvalue reference
316fbee4a0cSYang Fan   // not same type
317fbee4a0cSYang Fan   DerivedB2 b;
318*ba15d186SMark de Wever   return b; // cxx11_23-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
319fbee4a0cSYang Fan }
320fbee4a0cSYang Fan } // namespace test_ctor_param_rvalue_ref
3215f1de9caSArthur O'Dwyer 
3225f1de9caSArthur O'Dwyer namespace test_lvalue_ref_is_not_moved_from {
3235f1de9caSArthur O'Dwyer 
3245f1de9caSArthur O'Dwyer struct Target {};
32503282f2fSMatheus Izvekov // expected-note@-1  {{candidate constructor (the implicit copy constructor) not viable}}
326*ba15d186SMark de Wever // cxx11_23-note@-2  {{candidate constructor (the implicit move constructor) not viable}}
3275f1de9caSArthur O'Dwyer 
3285f1de9caSArthur O'Dwyer struct CopyOnly {
32903282f2fSMatheus Izvekov   CopyOnly(CopyOnly &&) = delete; // expected-note {{has been explicitly marked deleted here}}
3305f1de9caSArthur O'Dwyer   CopyOnly(CopyOnly&);
33103282f2fSMatheus Izvekov   operator Target() && = delete; // expected-note {{has been explicitly marked deleted here}}
3325f1de9caSArthur O'Dwyer   operator Target() &;
3335f1de9caSArthur O'Dwyer };
3345f1de9caSArthur O'Dwyer 
3355f1de9caSArthur O'Dwyer struct MoveOnly {
336*ba15d186SMark de Wever   MoveOnly(MoveOnly &&); // cxx11_23-note {{copy constructor is implicitly deleted because}}
33703282f2fSMatheus Izvekov   operator Target() &&;  // expected-note {{candidate function not viable}}
3385f1de9caSArthur O'Dwyer };
3395f1de9caSArthur O'Dwyer 
3405f1de9caSArthur O'Dwyer extern CopyOnly copyonly;
3415f1de9caSArthur O'Dwyer extern MoveOnly moveonly;
3425f1de9caSArthur O'Dwyer 
t1()3435f1de9caSArthur O'Dwyer CopyOnly t1() {
3445f1de9caSArthur O'Dwyer     CopyOnly& r = copyonly;
3455f1de9caSArthur O'Dwyer     return r;
3465f1de9caSArthur O'Dwyer }
3475f1de9caSArthur O'Dwyer 
t2()3485f1de9caSArthur O'Dwyer CopyOnly t2() {
3495f1de9caSArthur O'Dwyer     CopyOnly&& r = static_cast<CopyOnly&&>(copyonly);
35003282f2fSMatheus Izvekov     return r; // expected-error {{call to deleted constructor}}
3515f1de9caSArthur O'Dwyer }
3525f1de9caSArthur O'Dwyer 
t3()3535f1de9caSArthur O'Dwyer MoveOnly t3() {
3545f1de9caSArthur O'Dwyer     MoveOnly& r = moveonly;
355*ba15d186SMark de Wever     return r; // cxx11_23-error {{call to implicitly-deleted copy constructor}}
3565f1de9caSArthur O'Dwyer }
3575f1de9caSArthur O'Dwyer 
t4()3585f1de9caSArthur O'Dwyer MoveOnly t4() {
3595f1de9caSArthur O'Dwyer     MoveOnly&& r = static_cast<MoveOnly&&>(moveonly);
3607d2d5a3aSMatheus Izvekov     return r;
3615f1de9caSArthur O'Dwyer }
3625f1de9caSArthur O'Dwyer 
t5()3635f1de9caSArthur O'Dwyer Target t5() {
3645f1de9caSArthur O'Dwyer     CopyOnly& r = copyonly;
3655f1de9caSArthur O'Dwyer     return r;
3665f1de9caSArthur O'Dwyer }
3675f1de9caSArthur O'Dwyer 
t6()3685f1de9caSArthur O'Dwyer Target t6() {
3695f1de9caSArthur O'Dwyer     CopyOnly&& r = static_cast<CopyOnly&&>(copyonly);
37003282f2fSMatheus Izvekov     return r; // expected-error {{invokes a deleted function}}
3715f1de9caSArthur O'Dwyer }
3725f1de9caSArthur O'Dwyer 
t7()3735f1de9caSArthur O'Dwyer Target t7() {
3745f1de9caSArthur O'Dwyer     MoveOnly& r = moveonly;
3755f1de9caSArthur O'Dwyer     return r; // expected-error {{no viable conversion}}
3765f1de9caSArthur O'Dwyer }
3775f1de9caSArthur O'Dwyer 
t8()3785f1de9caSArthur O'Dwyer Target t8() {
3795f1de9caSArthur O'Dwyer     MoveOnly&& r = static_cast<MoveOnly&&>(moveonly);
38003282f2fSMatheus Izvekov     return r;
3815f1de9caSArthur O'Dwyer }
3825f1de9caSArthur O'Dwyer 
3835f1de9caSArthur O'Dwyer } // namespace test_lvalue_ref_is_not_moved_from
3845f1de9caSArthur O'Dwyer 
3855f1de9caSArthur O'Dwyer namespace test_rvalue_ref_to_nonobject {
3865f1de9caSArthur O'Dwyer 
3875f1de9caSArthur O'Dwyer struct CopyOnly {};
3885f1de9caSArthur O'Dwyer struct MoveOnly {};
3895f1de9caSArthur O'Dwyer 
3905f1de9caSArthur O'Dwyer struct Target {
3915f1de9caSArthur O'Dwyer     Target(CopyOnly (&)());
3925f1de9caSArthur O'Dwyer     Target(CopyOnly (&&)()) = delete;
3937d2d5a3aSMatheus Izvekov     Target(MoveOnly (&)()) = delete; // expected-note 2{{has been explicitly marked deleted here}}
3945f1de9caSArthur O'Dwyer     Target(MoveOnly (&&)());
3955f1de9caSArthur O'Dwyer };
3965f1de9caSArthur O'Dwyer 
3975f1de9caSArthur O'Dwyer CopyOnly make_copyonly();
3985f1de9caSArthur O'Dwyer MoveOnly make_moveonly();
3995f1de9caSArthur O'Dwyer 
t1()4005f1de9caSArthur O'Dwyer Target t1() {
4015f1de9caSArthur O'Dwyer     CopyOnly (&r)() = make_copyonly;
4025f1de9caSArthur O'Dwyer     return r;
4035f1de9caSArthur O'Dwyer }
4045f1de9caSArthur O'Dwyer 
t2()4055f1de9caSArthur O'Dwyer Target t2() {
4065f1de9caSArthur O'Dwyer     CopyOnly (&&r)() = static_cast<CopyOnly(&&)()>(make_copyonly);
4075f1de9caSArthur O'Dwyer     return r; // OK in all modes; not subject to implicit move
4085f1de9caSArthur O'Dwyer }
4095f1de9caSArthur O'Dwyer 
t3()4105f1de9caSArthur O'Dwyer Target t3() {
4115f1de9caSArthur O'Dwyer     MoveOnly (&r)() = make_moveonly;
4125f1de9caSArthur O'Dwyer     return r; // expected-error {{invokes a deleted function}}
4135f1de9caSArthur O'Dwyer }
4145f1de9caSArthur O'Dwyer 
t4()4155f1de9caSArthur O'Dwyer Target t4() {
4165f1de9caSArthur O'Dwyer     MoveOnly (&&r)() = static_cast<MoveOnly(&&)()>(make_moveonly);
4175f1de9caSArthur O'Dwyer     return r; // expected-error {{invokes a deleted function}}
4185f1de9caSArthur O'Dwyer }
4195f1de9caSArthur O'Dwyer 
4205f1de9caSArthur O'Dwyer } // namespace test_rvalue_ref_to_nonobject
42118192228SMatheus Izvekov 
42203282f2fSMatheus Izvekov // Both tests in test_constandnonconstcopy, and also test_conversion::test1, are
42303282f2fSMatheus Izvekov // "pure" C++98 tests (pretend 'delete' means 'private').
42403282f2fSMatheus Izvekov // However we may extend implicit moves into C++98, we must make sure the
42503282f2fSMatheus Izvekov // results in these are not changed.
4267d2d5a3aSMatheus Izvekov namespace test_constandnonconstcopy {
4277d2d5a3aSMatheus Izvekov struct ConstCopyOnly {
4287d2d5a3aSMatheus Izvekov   ConstCopyOnly();
4297d2d5a3aSMatheus Izvekov   ConstCopyOnly(ConstCopyOnly &) = delete; // cxx98-note {{marked deleted here}}
4307d2d5a3aSMatheus Izvekov   ConstCopyOnly(const ConstCopyOnly &);
4317d2d5a3aSMatheus Izvekov };
t1()4327d2d5a3aSMatheus Izvekov ConstCopyOnly t1() {
4337d2d5a3aSMatheus Izvekov   ConstCopyOnly x;
4347d2d5a3aSMatheus Izvekov   return x; // cxx98-error {{call to deleted constructor}}
4357d2d5a3aSMatheus Izvekov }
4367d2d5a3aSMatheus Izvekov 
4377d2d5a3aSMatheus Izvekov struct NonConstCopyOnly {
4387d2d5a3aSMatheus Izvekov   NonConstCopyOnly();
4397d2d5a3aSMatheus Izvekov   NonConstCopyOnly(NonConstCopyOnly &);
440*ba15d186SMark de Wever   NonConstCopyOnly(const NonConstCopyOnly &) = delete; // cxx11_23-note {{marked deleted here}}
4417d2d5a3aSMatheus Izvekov };
t2()4427d2d5a3aSMatheus Izvekov NonConstCopyOnly t2() {
4437d2d5a3aSMatheus Izvekov   NonConstCopyOnly x;
444*ba15d186SMark de Wever   return x; // cxx11_23-error {{call to deleted constructor}}
4457d2d5a3aSMatheus Izvekov }
4467d2d5a3aSMatheus Izvekov 
4477d2d5a3aSMatheus Izvekov } // namespace test_constandnonconstcopy
4487d2d5a3aSMatheus Izvekov 
4497d2d5a3aSMatheus Izvekov namespace test_conversion {
4507d2d5a3aSMatheus Izvekov 
4517d2d5a3aSMatheus Izvekov struct B;
4527d2d5a3aSMatheus Izvekov struct A {
4537d2d5a3aSMatheus Izvekov   A(B &) = delete; // cxx98-note {{has been explicitly deleted}}
4547d2d5a3aSMatheus Izvekov };
4557d2d5a3aSMatheus Izvekov struct B {
4567d2d5a3aSMatheus Izvekov   operator A(); // cxx98-note {{candidate function}}
4577d2d5a3aSMatheus Izvekov };
test1(B x)4587d2d5a3aSMatheus Izvekov A test1(B x) { return x; } // cxx98-error-re {{conversion {{.*}} is ambiguous}}
4597d2d5a3aSMatheus Izvekov 
4607d2d5a3aSMatheus Izvekov struct C {};
4617d2d5a3aSMatheus Izvekov struct D {
4627d2d5a3aSMatheus Izvekov   operator C() &;
46303282f2fSMatheus Izvekov   operator C() const & = delete; // expected-note {{marked deleted here}}
4647d2d5a3aSMatheus Izvekov };
test2(D x)46503282f2fSMatheus Izvekov C test2(D x) { return x; } // expected-error {{invokes a deleted function}}
4667d2d5a3aSMatheus Izvekov 
4677d2d5a3aSMatheus Izvekov } // namespace test_conversion
4687d2d5a3aSMatheus Izvekov 
46918192228SMatheus Izvekov namespace test_simpler_implicit_move {
47018192228SMatheus Izvekov 
47118192228SMatheus Izvekov struct CopyOnly {
472*ba15d186SMark de Wever   CopyOnly(); // cxx23-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
473*ba15d186SMark de Wever   // cxx23-note@-1 {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
474*ba15d186SMark de Wever   CopyOnly(CopyOnly &); // cxx23-note {{candidate constructor not viable: expects an lvalue for 1st argument}}
475*ba15d186SMark de Wever   // cxx23-note@-1 {{candidate constructor not viable: expects an lvalue for 1st argument}}
47618192228SMatheus Izvekov };
47718192228SMatheus Izvekov struct MoveOnly {
47818192228SMatheus Izvekov   MoveOnly();
47918192228SMatheus Izvekov   MoveOnly(MoveOnly &&);
48018192228SMatheus Izvekov };
48118192228SMatheus Izvekov MoveOnly &&rref();
48218192228SMatheus Izvekov 
test1(MoveOnly && w)48318192228SMatheus Izvekov MoveOnly &&test1(MoveOnly &&w) {
4847d2d5a3aSMatheus Izvekov   return w; // cxx98_20-error {{cannot bind to lvalue of type}}
48518192228SMatheus Izvekov }
48618192228SMatheus Izvekov 
test2(bool b)48718192228SMatheus Izvekov CopyOnly test2(bool b) {
48818192228SMatheus Izvekov   static CopyOnly w1;
48918192228SMatheus Izvekov   CopyOnly w2;
49018192228SMatheus Izvekov   if (b) {
49118192228SMatheus Izvekov     return w1;
49218192228SMatheus Izvekov   } else {
493*ba15d186SMark de Wever     return w2; // cxx23-error {{no matching constructor for initialization}}
49418192228SMatheus Izvekov   }
49518192228SMatheus Izvekov }
49618192228SMatheus Izvekov 
test3(T && x)4977d2d5a3aSMatheus Izvekov template <class T> T &&test3(T &&x) { return x; } // cxx98_20-error {{cannot bind to lvalue of type}}
49818192228SMatheus Izvekov template MoveOnly& test3<MoveOnly&>(MoveOnly&);
4997d2d5a3aSMatheus Izvekov template MoveOnly &&test3<MoveOnly>(MoveOnly &&); // cxx98_20-note {{in instantiation of function template specialization}}
50018192228SMatheus Izvekov 
test4()50118192228SMatheus Izvekov MoveOnly &&test4() {
50218192228SMatheus Izvekov   MoveOnly &&x = rref();
5037d2d5a3aSMatheus Izvekov   return x; // cxx98_20-error {{cannot bind to lvalue of type}}
50418192228SMatheus Izvekov }
50518192228SMatheus Izvekov 
test5()50618192228SMatheus Izvekov void test5() try {
50718192228SMatheus Izvekov   CopyOnly x;
508*ba15d186SMark de Wever   throw x; // cxx23-error {{no matching constructor for initialization}}
50918192228SMatheus Izvekov } catch (...) {
51018192228SMatheus Izvekov }
51118192228SMatheus Izvekov 
51218192228SMatheus Izvekov } // namespace test_simpler_implicit_move
513f2d5fce8SMatheus Izvekov 
514f2d5fce8SMatheus Izvekov namespace test_auto_variables {
515f2d5fce8SMatheus Izvekov 
516f2d5fce8SMatheus Izvekov struct S {};
517f2d5fce8SMatheus Izvekov 
518f2d5fce8SMatheus Izvekov template <class T> struct range {
519f2d5fce8SMatheus Izvekov   S *begin() const;
520f2d5fce8SMatheus Izvekov   S *end() const;
521f2d5fce8SMatheus Izvekov };
522f2d5fce8SMatheus Izvekov 
test_dependent_ranged_for()523f2d5fce8SMatheus Izvekov template <class T> S test_dependent_ranged_for() {
524f2d5fce8SMatheus Izvekov   for (auto x : range<T>())
525f2d5fce8SMatheus Izvekov     return x;
526f2d5fce8SMatheus Izvekov   return S();
527f2d5fce8SMatheus Izvekov }
528f2d5fce8SMatheus Izvekov template S test_dependent_ranged_for<int>();
529f2d5fce8SMatheus Izvekov 
530f2d5fce8SMatheus Izvekov template <class T> struct X {};
531f2d5fce8SMatheus Izvekov 
test_dependent_invalid_decl()532f2d5fce8SMatheus Izvekov template <class T> X<T> test_dependent_invalid_decl() {
533f2d5fce8SMatheus Izvekov   auto x = X<T>().foo(); // expected-error {{no member named 'foo'}}
534f2d5fce8SMatheus Izvekov   return x;
535f2d5fce8SMatheus Izvekov }
536f2d5fce8SMatheus Izvekov template X<int> test_dependent_invalid_decl<int>(); // expected-note {{requested here}}
537f2d5fce8SMatheus Izvekov 
538f2d5fce8SMatheus Izvekov } // namespace test_auto_variables
539d98c34f4SMatheus Izvekov 
540d98c34f4SMatheus Izvekov namespace PR51708 {
541d98c34f4SMatheus Izvekov 
542d98c34f4SMatheus Izvekov class a1;                  // expected-note 4 {{forward declaration of 'PR51708::a1'}}
543d98c34f4SMatheus Izvekov template <class> class A2; // expected-note 4 {{template is declared here}}
544d98c34f4SMatheus Izvekov using a2 = A2<int>;
545d98c34f4SMatheus Izvekov 
f()546d98c34f4SMatheus Izvekov template <class b> b f() {
547d98c34f4SMatheus Izvekov   // expected-error@-1 {{incomplete result type 'PR51708::a1' in function definition}}
548d98c34f4SMatheus Izvekov   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
549d98c34f4SMatheus Izvekov 
550d98c34f4SMatheus Izvekov   b d;
551d98c34f4SMatheus Izvekov   // expected-error@-1 {{variable has incomplete type 'PR51708::a1'}}
552d98c34f4SMatheus Izvekov   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
553d98c34f4SMatheus Izvekov 
554d98c34f4SMatheus Izvekov   return d;
555d98c34f4SMatheus Izvekov }
556d98c34f4SMatheus Izvekov template a1 f<a1>(); // expected-note-re {{in instantiation {{.*}} requested here}}
557d98c34f4SMatheus Izvekov template a2 f<a2>(); // expected-note-re {{in instantiation {{.*}} requested here}}
558d98c34f4SMatheus Izvekov 
g()559d98c34f4SMatheus Izvekov template <class b> b g() {
560d98c34f4SMatheus Izvekov   // expected-error@-1 {{incomplete result type 'PR51708::a1' in function definition}}
561d98c34f4SMatheus Izvekov   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
562d98c34f4SMatheus Izvekov 
563d98c34f4SMatheus Izvekov   b d __attribute__((aligned(1)));
564d98c34f4SMatheus Izvekov   // expected-error@-1 {{variable has incomplete type 'PR51708::a1'}}
565d98c34f4SMatheus Izvekov   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
566d98c34f4SMatheus Izvekov 
567d98c34f4SMatheus Izvekov   return d;
568d98c34f4SMatheus Izvekov }
569d98c34f4SMatheus Izvekov template a1 g<a1>(); // expected-note-re {{in instantiation {{.*}} requested here}}
570d98c34f4SMatheus Izvekov template a2 g<a2>(); // expected-note-re {{in instantiation {{.*}} requested here}}
571d98c34f4SMatheus Izvekov 
572d98c34f4SMatheus Izvekov } // namespace PR51708
573