1 // RUN: %clang_cc1 -std=c++2b -fsyntax-only -fcxx-exceptions -verify=expected,cxx20_2b %s 2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify=expected,cxx20_2b %s 3 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -fcxx-exceptions -verify=expected,cxx11_17 %s 4 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -fcxx-exceptions -verify=expected,cxx11_17 %s 5 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -verify=expected,cxx11_17 %s 6 7 namespace test_delete_function { 8 struct A1 { 9 A1(); 10 A1(const A1 &); 11 A1(A1 &&) = delete; // expected-note {{'A1' has been explicitly marked deleted here}} 12 }; 13 A1 test1() { 14 A1 a; 15 return a; // expected-error {{call to deleted constructor of 'test_delete_function::A1'}} 16 } 17 18 struct A2 { 19 A2(); 20 A2(const A2 &); 21 22 private: 23 A2(A2 &&); // expected-note {{declared private here}} 24 }; 25 A2 test2() { 26 A2 a; 27 return a; // expected-error {{calling a private constructor of class 'test_delete_function::A2'}} 28 } 29 30 struct C {}; 31 32 struct B1 { 33 B1(C &); 34 B1(C &&) = delete; // expected-note {{'B1' has been explicitly marked deleted here}} 35 }; 36 B1 test3() { 37 C c; 38 return c; // expected-error {{conversion function from 'test_delete_function::C' to 'test_delete_function::B1' invokes a deleted function}} 39 } 40 41 struct B2 { 42 B2(C &); 43 44 private: 45 B2(C &&); // expected-note {{declared private here}} 46 }; 47 B2 test4() { 48 C c; 49 return c; // expected-error {{calling a private constructor of class 'test_delete_function::B2'}} 50 } 51 } // namespace test_delete_function 52 53 // In C++20, implicitly movable entity can be rvalue reference to non-volatile 54 // automatic object. 55 namespace test_implicitly_movable_rvalue_ref { 56 struct A1 { 57 A1(A1 &&); 58 A1(const A1 &) = delete; // cxx11_17-note {{'A1' has been explicitly marked deleted here}} 59 }; 60 A1 test1(A1 &&a) { 61 return a; // cxx11_17-error {{call to deleted constructor of 'test_implicitly_movable_rvalue_ref::A1'}} 62 } 63 64 struct A2 { 65 A2(A2 &&); 66 67 private: 68 A2(const A2 &); // cxx11_17-note {{declared private here}} 69 }; 70 A2 test2(A2 &&a) { 71 return a; // cxx11_17-error {{calling a private constructor of class 'test_implicitly_movable_rvalue_ref::A2'}} 72 } 73 74 struct B1 { 75 B1(const B1 &); 76 B1(B1 &&) = delete; // cxx20_2b-note {{'B1' has been explicitly marked deleted here}} 77 }; 78 B1 test3(B1 &&b) { 79 return b; // cxx20_2b-error {{call to deleted constructor of 'test_implicitly_movable_rvalue_ref::B1'}} 80 } 81 82 struct B2 { 83 B2(const B2 &); 84 85 private: 86 B2(B2 &&); // cxx20_2b-note {{declared private here}} 87 }; 88 B2 test4(B2 &&b) { 89 return b; // cxx20_2b-error {{calling a private constructor of class 'test_implicitly_movable_rvalue_ref::B2'}} 90 } 91 } // namespace test_implicitly_movable_rvalue_ref 92 93 // In C++20, operand of throw-expression can be function parameter or 94 // catch-clause parameter. 95 namespace test_throw_parameter { 96 void func(); 97 98 struct A1 { 99 A1(const A1 &); 100 A1(A1 &&) = delete; // cxx20_2b-note {{'A1' has been explicitly marked deleted here}} 101 // expected-note@-1 {{'A1' has been explicitly marked deleted here}} 102 }; 103 void test1() { 104 try { 105 func(); 106 } catch (A1 a) { 107 throw a; // cxx20_2b-error {{call to deleted constructor of 'test_throw_parameter::A1'}} 108 } 109 } 110 111 struct A2 { 112 A2(const A2 &); 113 114 private: 115 A2(A2 &&); // cxx20_2b-note {{declared private here}} 116 }; 117 void test2() { 118 try { 119 func(); 120 } catch (A2 a) { 121 throw a; // cxx20_2b-error {{calling a private constructor of class 'test_throw_parameter::A2'}} 122 } 123 } 124 125 void test3(A1 a) try { 126 func(); 127 } catch (...) { 128 throw a; // expected-error {{call to deleted constructor of 'test_throw_parameter::A1'}} 129 } 130 } // namespace test_throw_parameter 131 132 // In C++20, during the first overload resolution, the selected function no 133 // need to be a constructor. 134 namespace test_non_ctor_conversion { 135 class C {}; 136 137 struct A1 { 138 operator C() &&; 139 operator C() const & = delete; // cxx11_17-note {{'operator C' has been explicitly marked deleted here}} 140 }; 141 C test1() { 142 A1 a; 143 return a; // cxx11_17-error {{conversion function from 'test_non_ctor_conversion::A1' to 'test_non_ctor_conversion::C' invokes a deleted function}} 144 } 145 146 struct A2 { 147 operator C() &&; 148 149 private: 150 operator C() const &; // cxx11_17-note {{declared private here}} 151 }; 152 C test2() { 153 A2 a; 154 return a; // cxx11_17-error {{'operator C' is a private member of 'test_non_ctor_conversion::A2'}} 155 } 156 157 struct B1 { 158 operator C() const &; 159 operator C() && = delete; // cxx20_2b-note {{'operator C' has been explicitly marked deleted here}} 160 }; 161 C test3() { 162 B1 b; 163 return b; // cxx20_2b-error {{conversion function from 'test_non_ctor_conversion::B1' to 'test_non_ctor_conversion::C' invokes a deleted function}} 164 } 165 166 struct B2 { 167 operator C() const &; 168 169 private: 170 operator C() &&; // cxx20_2b-note {{declared private here}} 171 }; 172 C test4() { 173 B2 b; 174 return b; // cxx20_2b-error {{'operator C' is a private member of 'test_non_ctor_conversion::B2'}} 175 } 176 } // namespace test_non_ctor_conversion 177 178 // In C++20, during the first overload resolution, the first parameter of the 179 // selected function no need to be an rvalue reference to the object's type. 180 namespace test_ctor_param_rvalue_ref { 181 struct A1; 182 struct A2; 183 struct B1; 184 struct B2; 185 186 struct NeedRvalueRef { 187 NeedRvalueRef(A1 &&); 188 NeedRvalueRef(A2 &&); 189 NeedRvalueRef(B1 &&); 190 NeedRvalueRef(B2 &&); 191 }; 192 struct NeedValue { 193 NeedValue(A1); // cxx11_17-note 2 {{passing argument to parameter here}} 194 NeedValue(A2); 195 NeedValue(B1); // cxx20_2b-note 2 {{passing argument to parameter here}} 196 NeedValue(B2); 197 }; 198 199 struct A1 { 200 A1(); 201 A1(A1 &&); 202 A1(const A1 &) = delete; // cxx11_17-note 3 {{'A1' has been explicitly marked deleted here}} 203 }; 204 NeedValue test_1_1() { 205 // not rvalue reference 206 // same type 207 A1 a; 208 return a; // cxx11_17-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::A1'}} 209 } 210 class DerivedA1 : public A1 {}; 211 A1 test_1_2() { 212 // rvalue reference 213 // not same type 214 DerivedA1 a; 215 return a; // cxx11_17-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::A1'}} 216 } 217 NeedValue test_1_3() { 218 // not rvalue reference 219 // not same type 220 DerivedA1 a; 221 return a; // cxx11_17-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::A1'}} 222 } 223 224 struct A2 { 225 A2(); 226 A2(A2 &&); 227 228 private: 229 A2(const A2 &); // cxx11_17-note 3 {{declared private here}} 230 }; 231 NeedValue test_2_1() { 232 // not rvalue reference 233 // same type 234 A2 a; 235 return a; // cxx11_17-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::A2'}} 236 } 237 class DerivedA2 : public A2 {}; 238 A2 test_2_2() { 239 // rvalue reference 240 // not same type 241 DerivedA2 a; 242 return a; // cxx11_17-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::A2'}} 243 } 244 NeedValue test_2_3() { 245 // not rvalue reference 246 // not same type 247 DerivedA2 a; 248 return a; // cxx11_17-error {{calling a private constructor of class 'test_ctor_param_rvalue_ref::A2'}} 249 } 250 251 struct B1 { 252 B1(); 253 B1(const B1 &); 254 B1(B1 &&) = delete; // cxx20_2b-note 3 {{'B1' has been explicitly marked deleted here}} 255 }; 256 NeedValue test_3_1() { 257 // not rvalue reference 258 // same type 259 B1 b; 260 return b; // cxx20_2b-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}} 261 } 262 class DerivedB1 : public B1 {}; 263 B1 test_3_2() { 264 // rvalue reference 265 // not same type 266 DerivedB1 b; 267 return b; // cxx20_2b-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}} 268 } 269 NeedValue test_3_3() { 270 // not rvalue reference 271 // not same type 272 DerivedB1 b; 273 return b; // cxx20_2b-error {{call to deleted constructor of 'test_ctor_param_rvalue_ref::B1'}} 274 } 275 276 struct B2 { 277 B2(); 278 B2(const B2 &); 279 280 private: 281 B2(B2 &&); // cxx20_2b-note 3 {{declared private here}} 282 }; 283 NeedValue test_4_1() { 284 // not rvalue reference 285 // same type 286 B2 b; 287 return b; // cxx20_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; // cxx20_2b-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; // cxx20_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 // expected-note@-2 {{candidate constructor (the implicit move constructor) not viable}} 309 // cxx11_17-note@-3 {{candidate constructor (the implicit copy constructor) not viable}} 310 // cxx11_17-note@-4 {{candidate constructor (the implicit move constructor) not viable}} 311 312 struct CopyOnly { 313 CopyOnly(CopyOnly&&) = delete; // cxx20_2b-note {{has been explicitly marked deleted here}} 314 CopyOnly(CopyOnly&); 315 operator Target() && = delete; // cxx20_2b-note {{has been explicitly marked deleted here}} 316 operator Target() &; 317 }; 318 319 struct MoveOnly { 320 MoveOnly(MoveOnly&&); // expected-note {{copy constructor is implicitly deleted because}} 321 // cxx11_17-note@-1 {{copy constructor is implicitly deleted because}} 322 operator Target() &&; // expected-note {{candidate function not viable}} 323 // cxx11_17-note@-1 {{candidate function not viable}} 324 }; 325 326 extern CopyOnly copyonly; 327 extern MoveOnly moveonly; 328 329 CopyOnly t1() { 330 CopyOnly& r = copyonly; 331 return r; 332 } 333 334 CopyOnly t2() { 335 CopyOnly&& r = static_cast<CopyOnly&&>(copyonly); 336 return r; // cxx20_2b-error {{call to deleted constructor}} 337 } 338 339 MoveOnly t3() { 340 MoveOnly& r = moveonly; 341 return r; // expected-error {{call to implicitly-deleted copy constructor}} 342 } 343 344 MoveOnly t4() { 345 MoveOnly&& r = static_cast<MoveOnly&&>(moveonly); 346 return r; // cxx11_17-error {{call to implicitly-deleted copy constructor}} 347 } 348 349 Target t5() { 350 CopyOnly& r = copyonly; 351 return r; 352 } 353 354 Target t6() { 355 CopyOnly&& r = static_cast<CopyOnly&&>(copyonly); 356 return r; // cxx20_2b-error {{invokes a deleted function}} 357 } 358 359 Target t7() { 360 MoveOnly& r = moveonly; 361 return r; // expected-error {{no viable conversion}} 362 } 363 364 Target t8() { 365 MoveOnly&& r = static_cast<MoveOnly&&>(moveonly); 366 return r; // cxx11_17-error {{no viable conversion}} 367 } 368 369 } // namespace test_lvalue_ref_is_not_moved_from 370 371 namespace test_rvalue_ref_to_nonobject { 372 373 struct CopyOnly {}; 374 struct MoveOnly {}; 375 376 struct Target { 377 Target(CopyOnly (&)()); 378 Target(CopyOnly (&&)()) = delete; 379 Target(MoveOnly (&)()) = delete; // expected-note {{has been explicitly marked deleted here}} 380 // expected-note@-1 {{has been explicitly marked deleted here}} 381 Target(MoveOnly (&&)()); 382 }; 383 384 CopyOnly make_copyonly(); 385 MoveOnly make_moveonly(); 386 387 Target t1() { 388 CopyOnly (&r)() = make_copyonly; 389 return r; 390 } 391 392 Target t2() { 393 CopyOnly (&&r)() = static_cast<CopyOnly(&&)()>(make_copyonly); 394 return r; // OK in all modes; not subject to implicit move 395 } 396 397 Target t3() { 398 MoveOnly (&r)() = make_moveonly; 399 return r; // expected-error {{invokes a deleted function}} 400 } 401 402 Target t4() { 403 MoveOnly (&&r)() = static_cast<MoveOnly(&&)()>(make_moveonly); 404 return r; // expected-error {{invokes a deleted function}} 405 } 406 407 } // namespace test_rvalue_ref_to_nonobject 408 409 namespace test_simpler_implicit_move { 410 411 struct CopyOnly { 412 CopyOnly(); 413 CopyOnly(CopyOnly &); 414 }; 415 struct MoveOnly { 416 MoveOnly(); 417 MoveOnly(MoveOnly &&); 418 }; 419 MoveOnly &&rref(); 420 421 MoveOnly &&test1(MoveOnly &&w) { 422 return w; // expected-error {{cannot bind to lvalue of type}} 423 } 424 425 CopyOnly test2(bool b) { 426 static CopyOnly w1; 427 CopyOnly w2; 428 if (b) { 429 return w1; 430 } else { 431 return w2; 432 } 433 } 434 435 template <class T> T &&test3(T &&x) { return x; } // expected-error {{cannot bind to lvalue of type}} 436 template MoveOnly& test3<MoveOnly&>(MoveOnly&); 437 template MoveOnly&& test3<MoveOnly>(MoveOnly&&); // expected-note {{in instantiation of function template specialization}} 438 439 MoveOnly &&test4() { 440 MoveOnly &&x = rref(); 441 return x; // expected-error {{cannot bind to lvalue of type}} 442 } 443 444 void test5() try { 445 CopyOnly x; 446 throw x; 447 } catch (...) { 448 } 449 450 } // namespace test_simpler_implicit_move 451