1 // RUN: %clang_cc1 -std=c++2b -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx11_2b,cxx2b    %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx98_20,cxx11_2b,cxx11_20 %s
3 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions                       -verify=expected,cxx98_20,cxx11_2b,cxx11_20 %s
4 // RUN: %clang_cc1 -std=c++98 -fsyntax-only -fcxx-exceptions -Wno-c++11-extensions -verify=expected,cxx98_20,cxx98 %s
5 
6 namespace test_delete_function {
7 struct A1 {
8   A1();
9   A1(const A1 &);
10   A1(A1 &&) = delete; // expected-note {{'A1' has been explicitly marked deleted here}}
11 };
12 A1 test1() {
13   A1 a;
14   return a; // expected-error {{call to deleted constructor of 'test_delete_function::A1'}}
15 }
16 
17 struct A2 {
18   A2();
19   A2(const A2 &);
20 
21 private:
22   A2(A2 &&); // expected-note {{declared private here}}
23 };
24 A2 test2() {
25   A2 a;
26   return a; // expected-error {{calling a private constructor of class 'test_delete_function::A2'}}
27 }
28 
29 struct C {};
30 
31 struct B1 {
32   B1(C &);
33   B1(C &&) = delete; // expected-note {{'B1' has been explicitly marked deleted here}}
34 };
35 B1 test3() {
36   C c;
37   return c; // expected-error {{conversion function from 'test_delete_function::C' to 'test_delete_function::B1' invokes a deleted function}}
38 }
39 
40 struct B2 {
41   B2(C &);
42 
43 private:
44   B2(C &&); // expected-note {{declared private here}}
45 };
46 B2 test4() {
47   C c;
48   return c; // expected-error {{calling a private constructor of class 'test_delete_function::B2'}}
49 }
50 } // namespace test_delete_function
51 
52 // Implicitly movable entity can be rvalue reference to non-volatile
53 // automatic object.
54 namespace test_implicitly_movable_rvalue_ref {
55 struct A1 {
56   A1(A1 &&);
57   A1(const A1 &) = delete;
58 };
59 A1 test1(A1 &&a) {
60   return a;
61 }
62 
63 struct A2 {
64   A2(A2 &&);
65 
66 private:
67   A2(const A2 &);
68 };
69 A2 test2(A2 &&a) {
70   return a;
71 }
72 
73 struct B1 {
74   B1(const B1 &);
75   B1(B1 &&) = delete; // expected-note {{'B1' has been explicitly marked deleted here}}
76 };
77 B1 test3(B1 &&b) {
78   return b; // expected-error {{call to deleted constructor of 'test_implicitly_movable_rvalue_ref::B1'}}
79 }
80 
81 struct B2 {
82   B2(const B2 &);
83 
84 private:
85   B2(B2 &&); // expected-note {{declared private here}}
86 };
87 B2 test4(B2 &&b) {
88   return b; // expected-error {{calling a private constructor of class 'test_implicitly_movable_rvalue_ref::B2'}}
89 }
90 } // namespace test_implicitly_movable_rvalue_ref
91 
92 // Operand of throw-expression can be function parameter or
93 // catch-clause parameter.
94 namespace test_throw_parameter {
95 void func();
96 
97 struct A1 {
98   A1(const A1 &);
99   A1(A1 &&) = delete; // expected-note 2{{'A1' has been explicitly marked deleted here}}
100 };
101 void test1() {
102   try {
103     func();
104   } catch (A1 a) {
105     throw a; // expected-error {{call to deleted constructor of 'test_throw_parameter::A1'}}
106   }
107 }
108 
109 struct A2 {
110   A2(const A2 &);
111 
112 private:
113   A2(A2 &&); // expected-note {{declared private here}}
114 };
115 void test2() {
116   try {
117     func();
118   } catch (A2 a) {
119     throw a; // expected-error {{calling a private constructor of class 'test_throw_parameter::A2'}}
120   }
121 }
122 
123 void test3(A1 a) try {
124   func();
125 } catch (...) {
126   throw a; // expected-error {{call to deleted constructor of 'test_throw_parameter::A1'}}
127 }
128 } // namespace test_throw_parameter
129 
130 // During the first overload resolution, the selected function no
131 // need to be a constructor.
132 namespace test_non_ctor_conversion {
133 class C {};
134 
135 struct A1 {
136   operator C() &&;
137   operator C() const & = delete;
138 };
139 C test1() {
140   A1 a;
141   return a;
142 }
143 
144 struct A2 {
145   operator C() &&;
146 
147 private:
148   operator C() const &;
149 };
150 C test2() {
151   A2 a;
152   return a;
153 }
154 
155 struct B1 {
156   operator C() const &;
157   operator C() && = delete; // expected-note {{'operator C' has been explicitly marked deleted here}}
158 };
159 C test3() {
160   B1 b;
161   return b; // expected-error {{conversion function from 'test_non_ctor_conversion::B1' to 'test_non_ctor_conversion::C' invokes a deleted function}}
162 }
163 
164 struct B2 {
165   operator C() const &;
166 
167 private:
168   operator C() &&; // expected-note {{declared private here}}
169 };
170 C test4() {
171   B2 b;
172   return b; // expected-error {{'operator C' is a private member of 'test_non_ctor_conversion::B2'}}
173 }
174 } // namespace test_non_ctor_conversion
175 
176 // During the first overload resolution, the first parameter of the
177 // selected function no need to be an rvalue reference to the object's type.
178 namespace test_ctor_param_rvalue_ref {
179 struct A1;
180 struct A2;
181 struct B1;
182 struct B2;
183 
184 struct NeedRvalueRef {
185   NeedRvalueRef(A1 &&);
186   NeedRvalueRef(A2 &&);
187   NeedRvalueRef(B1 &&);
188   NeedRvalueRef(B2 &&);
189 };
190 struct NeedValue {
191   NeedValue(A1); // cxx98-note 2 {{passing argument to parameter here}}
192   NeedValue(A2);
193   NeedValue(B1); // cxx11_2b-note 2 {{passing argument to parameter here}}
194   NeedValue(B2);
195 };
196 
197 struct A1 {
198   A1();
199   A1(A1 &&);
200   A1(const A1 &) = delete; // cxx98-note 2 {{marked deleted here}}
201 };
202 NeedValue test_1_1() {
203   // not rvalue reference
204   // same type
205   A1 a;
206   return a; // cxx98-error {{call to deleted constructor}}
207 }
208 class DerivedA1 : public A1 {};
209 A1 test_1_2() {
210   // rvalue reference
211   // not same type
212   DerivedA1 a;
213   return a;
214 }
215 NeedValue test_1_3() {
216   // not rvalue reference
217   // not same type
218   DerivedA1 a;
219   return a; // cxx98-error {{call to deleted constructor}}
220 }
221 
222 struct A2 {
223   A2();
224   A2(A2 &&);
225 
226 private:
227   A2(const A2 &); // cxx98-note 2 {{declared private here}}
228 };
229 NeedValue test_2_1() {
230   // not rvalue reference
231   // same type
232   A2 a;
233   return a; // cxx98-error {{calling a private constructor}}
234 }
235 class DerivedA2 : public A2 {};
236 A2 test_2_2() {
237   // rvalue reference
238   // not same type
239   DerivedA2 a;
240   return a;
241 }
242 NeedValue test_2_3() {
243   // not rvalue reference
244   // not same type
245   DerivedA2 a;
246   return a; // cxx98-error {{calling a private constructor}}
247 }
248 
249 struct B1 {
250   B1();
251   B1(const B1 &);
252   B1(B1 &&) = delete; // cxx11_2b-note 3 {{'B1' has been explicitly marked deleted here}}
253                       // cxx98-note@-1 {{'B1' has been explicitly marked deleted here}}
254 };
255 NeedValue test_3_1() {
256   // not rvalue reference
257   // same type
258   B1 b;
259   return b; // cxx11_2b-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}}
260 }
261 class DerivedB1 : public B1 {};
262 B1 test_3_2() {
263   // rvalue reference
264   // not same type
265   DerivedB1 b;
266   return b; // expected-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}}
267 }
268 NeedValue test_3_3() {
269   // not rvalue reference
270   // not same type
271   DerivedB1 b;
272   return b; // cxx11_2b-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}}
273 }
274 
275 struct B2 {
276   B2();
277   B2(const B2 &);
278 
279 private:
280   B2(B2 &&); // cxx11_2b-note 3 {{declared private here}}
281              // cxx98-note@-1 {{declared private here}}
282 };
283 NeedValue test_4_1() {
284   // not rvalue reference
285   // same type
286   B2 b;
287   return b; // cxx11_2b-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
288 }
289 class DerivedB2 : public B2 {};
290 B2 test_4_2() {
291   // rvalue reference
292   // not same type
293   DerivedB2 b;
294   return b; // expected-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
295 }
296 NeedValue test_4_3() {
297   // not rvalue reference
298   // not same type
299   DerivedB2 b;
300   return b; // cxx11_2b-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::B2'}}
301 }
302 } // namespace test_ctor_param_rvalue_ref
303 
304 namespace test_lvalue_ref_is_not_moved_from {
305 
306 struct Target {};
307 // expected-note@-1  {{candidate constructor (the implicit copy constructor) not viable}}
308 // cxx11_2b-note@-2  {{candidate constructor (the implicit move constructor) not viable}}
309 
310 struct CopyOnly {
311   CopyOnly(CopyOnly &&) = delete; // expected-note {{has been explicitly marked deleted here}}
312   CopyOnly(CopyOnly&);
313   operator Target() && = delete; // expected-note {{has been explicitly marked deleted here}}
314   operator Target() &;
315 };
316 
317 struct MoveOnly {
318   MoveOnly(MoveOnly &&); // cxx11_2b-note {{copy constructor is implicitly deleted because}}
319   operator Target() &&;  // expected-note {{candidate function not viable}}
320 };
321 
322 extern CopyOnly copyonly;
323 extern MoveOnly moveonly;
324 
325 CopyOnly t1() {
326     CopyOnly& r = copyonly;
327     return r;
328 }
329 
330 CopyOnly t2() {
331     CopyOnly&& r = static_cast<CopyOnly&&>(copyonly);
332     return r; // expected-error {{call to deleted constructor}}
333 }
334 
335 MoveOnly t3() {
336     MoveOnly& r = moveonly;
337     return r; // cxx11_2b-error {{call to implicitly-deleted copy constructor}}
338 }
339 
340 MoveOnly t4() {
341     MoveOnly&& r = static_cast<MoveOnly&&>(moveonly);
342     return r;
343 }
344 
345 Target t5() {
346     CopyOnly& r = copyonly;
347     return r;
348 }
349 
350 Target t6() {
351     CopyOnly&& r = static_cast<CopyOnly&&>(copyonly);
352     return r; // expected-error {{invokes a deleted function}}
353 }
354 
355 Target t7() {
356     MoveOnly& r = moveonly;
357     return r; // expected-error {{no viable conversion}}
358 }
359 
360 Target t8() {
361     MoveOnly&& r = static_cast<MoveOnly&&>(moveonly);
362     return r;
363 }
364 
365 } // namespace test_lvalue_ref_is_not_moved_from
366 
367 namespace test_rvalue_ref_to_nonobject {
368 
369 struct CopyOnly {};
370 struct MoveOnly {};
371 
372 struct Target {
373     Target(CopyOnly (&)());
374     Target(CopyOnly (&&)()) = delete;
375     Target(MoveOnly (&)()) = delete; // expected-note 2{{has been explicitly marked deleted here}}
376     Target(MoveOnly (&&)());
377 };
378 
379 CopyOnly make_copyonly();
380 MoveOnly make_moveonly();
381 
382 Target t1() {
383     CopyOnly (&r)() = make_copyonly;
384     return r;
385 }
386 
387 Target t2() {
388     CopyOnly (&&r)() = static_cast<CopyOnly(&&)()>(make_copyonly);
389     return r; // OK in all modes; not subject to implicit move
390 }
391 
392 Target t3() {
393     MoveOnly (&r)() = make_moveonly;
394     return r; // expected-error {{invokes a deleted function}}
395 }
396 
397 Target t4() {
398     MoveOnly (&&r)() = static_cast<MoveOnly(&&)()>(make_moveonly);
399     return r; // expected-error {{invokes a deleted function}}
400 }
401 
402 } // namespace test_rvalue_ref_to_nonobject
403 
404 // Both tests in test_constandnonconstcopy, and also test_conversion::test1, are
405 // "pure" C++98 tests (pretend 'delete' means 'private').
406 // However we may extend implicit moves into C++98, we must make sure the
407 // results in these are not changed.
408 namespace test_constandnonconstcopy {
409 struct ConstCopyOnly {
410   ConstCopyOnly();
411   ConstCopyOnly(ConstCopyOnly &) = delete; // cxx98-note {{marked deleted here}}
412   ConstCopyOnly(const ConstCopyOnly &);
413 };
414 ConstCopyOnly t1() {
415   ConstCopyOnly x;
416   return x; // cxx98-error {{call to deleted constructor}}
417 }
418 
419 struct NonConstCopyOnly {
420   NonConstCopyOnly();
421   NonConstCopyOnly(NonConstCopyOnly &);
422   NonConstCopyOnly(const NonConstCopyOnly &) = delete; // cxx11_2b-note {{marked deleted here}}
423 };
424 NonConstCopyOnly t2() {
425   NonConstCopyOnly x;
426   return x; // cxx11_2b-error {{call to deleted constructor}}
427 }
428 
429 } // namespace test_constandnonconstcopy
430 
431 namespace test_conversion {
432 
433 struct B;
434 struct A {
435   A(B &) = delete; // cxx98-note {{has been explicitly deleted}}
436 };
437 struct B {
438   operator A(); // cxx98-note {{candidate function}}
439 };
440 A test1(B x) { return x; } // cxx98-error-re {{conversion {{.*}} is ambiguous}}
441 
442 struct C {};
443 struct D {
444   operator C() &;
445   operator C() const & = delete; // expected-note {{marked deleted here}}
446 };
447 C test2(D x) { return x; } // expected-error {{invokes a deleted function}}
448 
449 } // namespace test_conversion
450 
451 namespace test_simpler_implicit_move {
452 
453 struct CopyOnly {
454   CopyOnly(); // cxx2b-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
455   // cxx2b-note@-1 {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
456   CopyOnly(CopyOnly &); // cxx2b-note {{candidate constructor not viable: expects an lvalue for 1st argument}}
457   // cxx2b-note@-1 {{candidate constructor not viable: expects an lvalue for 1st argument}}
458 };
459 struct MoveOnly {
460   MoveOnly();
461   MoveOnly(MoveOnly &&);
462 };
463 MoveOnly &&rref();
464 
465 MoveOnly &&test1(MoveOnly &&w) {
466   return w; // cxx98_20-error {{cannot bind to lvalue of type}}
467 }
468 
469 CopyOnly test2(bool b) {
470   static CopyOnly w1;
471   CopyOnly w2;
472   if (b) {
473     return w1;
474   } else {
475     return w2; // cxx2b-error {{no matching constructor for initialization}}
476   }
477 }
478 
479 template <class T> T &&test3(T &&x) { return x; } // cxx98_20-error {{cannot bind to lvalue of type}}
480 template MoveOnly& test3<MoveOnly&>(MoveOnly&);
481 template MoveOnly &&test3<MoveOnly>(MoveOnly &&); // cxx98_20-note {{in instantiation of function template specialization}}
482 
483 MoveOnly &&test4() {
484   MoveOnly &&x = rref();
485   return x; // cxx98_20-error {{cannot bind to lvalue of type}}
486 }
487 
488 void test5() try {
489   CopyOnly x;
490   throw x; // cxx2b-error {{no matching constructor for initialization}}
491 } catch (...) {
492 }
493 
494 } // namespace test_simpler_implicit_move
495 
496 namespace test_auto_variables {
497 
498 struct S {};
499 
500 template <class T> struct range {
501   S *begin() const;
502   S *end() const;
503 };
504 
505 template <class T> S test_dependent_ranged_for() {
506   for (auto x : range<T>())
507     return x;
508   return S();
509 }
510 template S test_dependent_ranged_for<int>();
511 
512 template <class T> struct X {};
513 
514 template <class T> X<T> test_dependent_invalid_decl() {
515   auto x = X<T>().foo(); // expected-error {{no member named 'foo'}}
516   return x;
517 }
518 template X<int> test_dependent_invalid_decl<int>(); // expected-note {{requested here}}
519 
520 } // namespace test_auto_variables
521 
522 namespace PR51708 {
523 
524 class a1;                  // expected-note 4 {{forward declaration of 'PR51708::a1'}}
525 template <class> class A2; // expected-note 4 {{template is declared here}}
526 using a2 = A2<int>;
527 
528 template <class b> b f() {
529   // expected-error@-1 {{incomplete result type 'PR51708::a1' in function definition}}
530   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
531 
532   b d;
533   // expected-error@-1 {{variable has incomplete type 'PR51708::a1'}}
534   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
535 
536   return d;
537 }
538 template a1 f<a1>(); // expected-note-re {{in instantiation {{.*}} requested here}}
539 template a2 f<a2>(); // expected-note-re {{in instantiation {{.*}} requested here}}
540 
541 template <class b> b g() {
542   // expected-error@-1 {{incomplete result type 'PR51708::a1' in function definition}}
543   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
544 
545   b d __attribute__((aligned(1)));
546   // expected-error@-1 {{variable has incomplete type 'PR51708::a1'}}
547   // expected-error@-2 {{implicit instantiation of undefined template 'PR51708::A2<int>}}
548 
549   return d;
550 }
551 template a1 g<a1>(); // expected-note-re {{in instantiation {{.*}} requested here}}
552 template a2 g<a2>(); // expected-note-re {{in instantiation {{.*}} requested here}}
553 
554 } // namespace PR51708
555