1 // RUN: %clang_cc1 -std=c++1z -verify -Wno-unused %s 2 // RUN: %clang_cc1 -std=c++1z -verify -Wno-unused %s -fexperimental-new-constant-interpreter 3 4 struct Noncopyable { 5 Noncopyable(); 6 Noncopyable(const Noncopyable &) = delete; // expected-note 1+{{deleted}} expected-note 1+ {{not viable}} 7 virtual ~Noncopyable(); 8 }; 9 struct Derived : Noncopyable {}; 10 struct NoncopyableAggr { // expected-note 3{{candidate}} 11 Noncopyable nc; 12 }; 13 struct Indestructible { 14 Indestructible(); 15 ~Indestructible() = delete; // expected-note 1+{{deleted}} 16 }; 17 struct Incomplete; // expected-note 1+{{declar}} 18 make(int kind=0)19Noncopyable make(int kind = 0) { 20 switch (kind) { 21 case 0: return {}; 22 case 1: return Noncopyable(); 23 case 2: return Noncopyable{}; 24 case 3: return make(); 25 } 26 __builtin_unreachable(); 27 } 28 29 Indestructible make_indestructible(); 30 Incomplete make_incomplete(); // expected-note 1+{{here}} 31 take(Noncopyable nc)32void take(Noncopyable nc) {} 33 nrvo()34Noncopyable nrvo() { 35 Noncopyable nrvo; 36 return nrvo; // expected-error {{deleted constructor}} 37 } 38 39 Noncopyable nc1 = make(); 40 Noncopyable nc2 = Noncopyable(); 41 Noncopyable nc3 = Derived(); // expected-error {{deleted constructor}} 42 Noncopyable nc4((Noncopyable())); 43 Noncopyable nc5 = {Noncopyable()}; 44 Noncopyable nc6{Noncopyable()}; 45 46 NoncopyableAggr nca1 = NoncopyableAggr{}; 47 NoncopyableAggr nca2 = NoncopyableAggr{{}}; 48 NoncopyableAggr nca3 = NoncopyableAggr{NoncopyableAggr{Noncopyable()}}; 49 50 template<typename T> struct Convert { operator T(); }; // expected-note 1+{{candidate}} 51 Noncopyable conv1 = Convert<Noncopyable>(); 52 Noncopyable conv2((Convert<Noncopyable>())); 53 Noncopyable conv3 = {Convert<Noncopyable>()}; 54 Noncopyable conv4{Convert<Noncopyable>()}; 55 56 Noncopyable ref_conv1 = Convert<Noncopyable&>(); // expected-error {{deleted constructor}} 57 Noncopyable ref_conv2((Convert<Noncopyable&>())); // expected-error {{deleted constructor}} 58 Noncopyable ref_conv3 = {Convert<Noncopyable&>()}; // expected-error {{deleted constructor}} 59 Noncopyable ref_conv4{Convert<Noncopyable&>()}; // expected-error {{deleted constructor}} 60 61 Noncopyable derived_conv1 = Convert<Derived>(); // expected-error {{deleted constructor}} 62 Noncopyable derived_conv2((Convert<Derived>())); // expected-error {{deleted constructor}} 63 Noncopyable derived_conv3 = {Convert<Derived>()}; // expected-error {{deleted constructor}} 64 Noncopyable derived_conv4{Convert<Derived>()}; // expected-error {{deleted constructor}} 65 66 NoncopyableAggr nc_aggr1 = Convert<NoncopyableAggr>(); 67 NoncopyableAggr nc_aggr2((Convert<NoncopyableAggr>())); 68 NoncopyableAggr nc_aggr3 = {Convert<NoncopyableAggr>()}; // expected-error {{no viable conversion}} 69 NoncopyableAggr nc_aggr4{Convert<NoncopyableAggr>()}; // expected-error {{no viable conversion}} 70 NoncopyableAggr nc_aggr5 = Convert<Noncopyable>(); // expected-error {{no viable}} 71 NoncopyableAggr nc_aggr6((Convert<Noncopyable>())); // expected-error {{no matching constructor}} 72 NoncopyableAggr nc_aggr7 = {Convert<Noncopyable>()}; 73 NoncopyableAggr nc_aggr8{Convert<Noncopyable>()}; 74 test_expressions(bool b)75void test_expressions(bool b) { 76 auto lambda = [a = make()] {}; 77 78 take({}); 79 take(Noncopyable()); 80 take(Noncopyable{}); 81 take(make()); 82 83 Noncopyable &&dc1 = dynamic_cast<Noncopyable&&>(Noncopyable()); 84 Noncopyable &&dc2 = dynamic_cast<Noncopyable&&>(nc1); 85 Noncopyable &&dc3 = dynamic_cast<Noncopyable&&>(Derived()); 86 87 Noncopyable sc1 = static_cast<Noncopyable>(Noncopyable()); 88 Noncopyable sc2 = static_cast<Noncopyable>(nc1); // expected-error {{deleted}} 89 Noncopyable sc3 = static_cast<Noncopyable&&>(Noncopyable()); // expected-error {{deleted}} 90 Noncopyable sc4 = static_cast<Noncopyable>(static_cast<Noncopyable&&>(Noncopyable())); // expected-error {{deleted}} 91 92 Noncopyable cc1 = (Noncopyable)Noncopyable(); 93 Noncopyable cc2 = (Noncopyable)Derived(); // expected-error {{deleted}} 94 95 Noncopyable fc1 = Noncopyable(Noncopyable()); 96 Noncopyable fc2 = Noncopyable(Derived()); // expected-error {{deleted}} 97 98 // We must check for a complete type for every materialized temporary. (Note 99 // that in the special case of the top level of a decltype, no temporary is 100 // materialized.) 101 make_incomplete(); // expected-error {{incomplete}} 102 make_incomplete().a; // expected-error {{incomplete}} 103 make_incomplete().*(int Incomplete::*)nullptr; // expected-error {{incomplete}} 104 dynamic_cast<Incomplete&&>(make_incomplete()); // expected-error {{incomplete}} 105 const_cast<Incomplete&&>(make_incomplete()); // expected-error {{incomplete}} 106 107 sizeof(Indestructible{}); // expected-error {{deleted}} 108 sizeof(make_indestructible()); // expected-error {{deleted}} 109 sizeof(make_incomplete()); // expected-error {{incomplete}} 110 typeid(Indestructible{}); // expected-error {{deleted}} expected-error {{you need to include <typeinfo>}} 111 typeid(make_indestructible()); // expected-error {{deleted}} \ 112 // expected-error {{need to include <typeinfo>}} 113 typeid(make_incomplete()); // expected-error {{incomplete}} \ 114 // expected-error {{need to include <typeinfo>}} 115 116 // FIXME: The first two cases here are now also valid in C++17 onwards. 117 using I = decltype(Indestructible()); // expected-error {{deleted}} 118 using I = decltype(Indestructible{}); // expected-error {{deleted}} 119 using I = decltype(make_indestructible()); 120 using J = decltype(make_incomplete()); 121 122 Noncopyable cond1 = b ? Noncopyable() : make(); 123 Noncopyable cond2 = b ? Noncopyable() : Derived(); // expected-error {{incompatible}} 124 Noncopyable cond3 = b ? Derived() : Noncopyable(); // expected-error {{incompatible}} 125 Noncopyable cond4 = b ? Noncopyable() : nc1; // expected-error {{deleted}} 126 Noncopyable cond5 = b ? nc1 : Noncopyable(); // expected-error {{deleted}} 127 // Could convert both to an xvalue of type Noncopyable here, but we're not 128 // permitted to consider that. 129 Noncopyable &&cond6 = b ? Noncopyable() : Derived(); // expected-error {{incompatible}} 130 Noncopyable &&cond7 = b ? Derived() : Noncopyable(); // expected-error {{incompatible}} 131 // Could convert both to a const lvalue of type Noncopyable here, but we're 132 // not permitted to consider that, either. 133 const Noncopyable cnc; 134 const Noncopyable &cond8 = b ? cnc : Derived(); // expected-error {{incompatible}} 135 const Noncopyable &cond9 = b ? Derived() : cnc; // expected-error {{incompatible}} 136 137 extern const volatile Noncopyable make_cv(); 138 Noncopyable cv_difference1 = make_cv(); 139 const volatile Noncopyable cv_difference2 = make(); 140 } 141 142 template<typename T> struct ConversionFunction { operator T(); }; 143 Noncopyable cf1 = ConversionFunction<Noncopyable>(); 144 Noncopyable cf2 = ConversionFunction<Noncopyable&&>(); // expected-error {{deleted}} 145 Noncopyable cf3 = ConversionFunction<const volatile Noncopyable>(); 146 const volatile Noncopyable cf4 = ConversionFunction<Noncopyable>(); 147 Noncopyable cf5 = ConversionFunction<Derived>(); // expected-error {{deleted}} 148 149 struct AsMember { 150 Noncopyable member; AsMemberAsMember151 AsMember() : member(make()) {} 152 }; 153 // FIXME: DR (no number yet): we still get a copy for base or delegating construction. 154 struct AsBase : Noncopyable { AsBaseAsBase155 AsBase() : Noncopyable(make()) {} // expected-error {{deleted}} 156 }; 157 struct AsDelegating final { 158 AsDelegating(const AsDelegating &) = delete; // expected-note {{deleted}} 159 static AsDelegating make(int); 160 161 // The base constructor version of this is problematic; the complete object 162 // version would be OK. Perhaps we could allow copy omission here for final 163 // classes? AsDelegatingAsDelegating164 AsDelegating(int n) : AsDelegating(make(n)) {} // expected-error {{deleted}} 165 }; 166 167 namespace CtorTemplateBeatsNonTemplateConversionFn { 168 struct Foo { template <typename Derived> Foo(const Derived &); }; 169 template <typename Derived> struct Base { operator Foo() const = delete; }; // expected-note {{deleted}} 170 struct Derived : Base<Derived> {}; 171 f(Derived d)172 Foo f(Derived d) { return d; } // expected-error {{invokes a deleted function}} g(Derived d)173 Foo g(Derived d) { return Foo(d); } // ok, calls constructor 174 } 175 176 // Make sure we don't consider conversion functions for guaranteed copy elision 177 namespace GH39319 { 178 struct A { 179 A(); 180 A(const A&) = delete; // expected-note {{'A' has been explicitly marked deleted here}} 181 }; 182 struct B { 183 operator A(); 184 } C; A()185A::A() : A(C) {} // expected-error {{call to deleted constructor of}} 186 187 struct A2 { 188 struct B2 { 189 operator A2(); 190 }; A2GH39319::A2191 A2() : A2(B2()) {} // expected-error {{call to deleted constructor of}} 192 A2(const A2&) = delete; // expected-note {{'A2' has been explicitly marked deleted here}} 193 }; 194 195 template<typename A3> 196 class B3 : A3 { 197 template<bool = C3<B3>()> // expected-warning 2{{use of function template name with no prior declaration in function call with explicit}} 198 B3(); 199 }; B3(); // expected-error {{deduction guide declaration without trailing return type}} \ 200 // expected-note {{while building implicit deduction guide first needed here}} 201 } 202