202 lines · cpp
1// RUN: %clang_cc1 -std=c++1z -verify -Wno-unused %s2// RUN: %clang_cc1 -std=c++1z -verify -Wno-unused %s -fexperimental-new-constant-interpreter3 4struct Noncopyable {5 Noncopyable();6 Noncopyable(const Noncopyable &) = delete; // expected-note 1+{{deleted}} expected-note 1+ {{not viable}}7 virtual ~Noncopyable();8};9struct Derived : Noncopyable {};10struct NoncopyableAggr { // expected-note 3{{candidate}}11 Noncopyable nc;12};13struct Indestructible {14 Indestructible();15 ~Indestructible() = delete; // expected-note 1+{{deleted}}16};17struct Incomplete; // expected-note 1+{{declar}}18 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 29Indestructible make_indestructible();30Incomplete make_incomplete(); // expected-note 1+{{here}}31 32void take(Noncopyable nc) {}33 34Noncopyable nrvo() {35 Noncopyable nrvo;36 return nrvo; // expected-error {{deleted constructor}}37}38 39Noncopyable nc1 = make();40Noncopyable nc2 = Noncopyable();41Noncopyable nc3 = Derived(); // expected-error {{deleted constructor}}42Noncopyable nc4((Noncopyable()));43Noncopyable nc5 = {Noncopyable()};44Noncopyable nc6{Noncopyable()};45 46NoncopyableAggr nca1 = NoncopyableAggr{};47NoncopyableAggr nca2 = NoncopyableAggr{{}};48NoncopyableAggr nca3 = NoncopyableAggr{NoncopyableAggr{Noncopyable()}};49 50template<typename T> struct Convert { operator T(); }; // expected-note 1+{{candidate}}51Noncopyable conv1 = Convert<Noncopyable>();52Noncopyable conv2((Convert<Noncopyable>()));53Noncopyable conv3 = {Convert<Noncopyable>()};54Noncopyable conv4{Convert<Noncopyable>()};55 56Noncopyable ref_conv1 = Convert<Noncopyable&>(); // expected-error {{deleted constructor}}57Noncopyable ref_conv2((Convert<Noncopyable&>())); // expected-error {{deleted constructor}}58Noncopyable ref_conv3 = {Convert<Noncopyable&>()}; // expected-error {{deleted constructor}}59Noncopyable ref_conv4{Convert<Noncopyable&>()}; // expected-error {{deleted constructor}}60 61Noncopyable derived_conv1 = Convert<Derived>(); // expected-error {{deleted constructor}}62Noncopyable derived_conv2((Convert<Derived>())); // expected-error {{deleted constructor}}63Noncopyable derived_conv3 = {Convert<Derived>()}; // expected-error {{deleted constructor}}64Noncopyable derived_conv4{Convert<Derived>()}; // expected-error {{deleted constructor}}65 66NoncopyableAggr nc_aggr1 = Convert<NoncopyableAggr>();67NoncopyableAggr nc_aggr2((Convert<NoncopyableAggr>()));68NoncopyableAggr nc_aggr3 = {Convert<NoncopyableAggr>()}; // expected-error {{no viable conversion}}69NoncopyableAggr nc_aggr4{Convert<NoncopyableAggr>()}; // expected-error {{no viable conversion}}70NoncopyableAggr nc_aggr5 = Convert<Noncopyable>(); // expected-error {{no viable}}71NoncopyableAggr nc_aggr6((Convert<Noncopyable>())); // expected-error {{no matching constructor}}72NoncopyableAggr nc_aggr7 = {Convert<Noncopyable>()};73NoncopyableAggr nc_aggr8{Convert<Noncopyable>()};74 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. (Note99 // that in the special case of the top level of a decltype, no temporary is100 // 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 not128 // 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're132 // 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 142template<typename T> struct ConversionFunction { operator T(); };143Noncopyable cf1 = ConversionFunction<Noncopyable>();144Noncopyable cf2 = ConversionFunction<Noncopyable&&>(); // expected-error {{deleted}}145Noncopyable cf3 = ConversionFunction<const volatile Noncopyable>();146const volatile Noncopyable cf4 = ConversionFunction<Noncopyable>();147Noncopyable cf5 = ConversionFunction<Derived>(); // expected-error {{deleted}}148 149struct AsMember {150 Noncopyable member;151 AsMember() : member(make()) {}152};153// FIXME: DR (no number yet): we still get a copy for base or delegating construction.154struct AsBase : Noncopyable {155 AsBase() : Noncopyable(make()) {} // expected-error {{deleted}}156};157struct 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 object162 // version would be OK. Perhaps we could allow copy omission here for final163 // classes?164 AsDelegating(int n) : AsDelegating(make(n)) {} // expected-error {{deleted}}165};166 167namespace 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 172 Foo f(Derived d) { return d; } // expected-error {{invokes a deleted function}}173 Foo g(Derived d) { return Foo(d); } // ok, calls constructor174}175 176// Make sure we don't consider conversion functions for guaranteed copy elision177namespace GH39319 {178struct A {179 A();180 A(const A&) = delete; // expected-note {{'A' has been explicitly marked deleted here}}181};182struct B {183 operator A();184} C;185A::A() : A(C) {} // expected-error {{call to deleted constructor of}}186 187struct A2 {188 struct B2 {189 operator A2();190 };191 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 195template<typename A3>196class 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