[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / cxx1z-copy-omission.cpp
blob92ebfdd8e4c735737e2f77b8dbbbf799198da32c
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
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;
13 struct Indestructible {
14 Indestructible();
15 ~Indestructible() = delete; // expected-note 1+{{deleted}}
17 struct Incomplete; // expected-note 1+{{declar}}
19 Noncopyable 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();
26 __builtin_unreachable();
29 Indestructible make_indestructible();
30 Incomplete make_incomplete(); // expected-note 1+{{here}}
32 void take(Noncopyable nc) {}
34 Noncopyable nrvo() {
35 Noncopyable nrvo;
36 return nrvo; // expected-error {{deleted constructor}}
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()};
46 NoncopyableAggr nca1 = NoncopyableAggr{};
47 NoncopyableAggr nca2 = NoncopyableAggr{{}};
48 NoncopyableAggr nca3 = NoncopyableAggr{NoncopyableAggr{Noncopyable()}};
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>()};
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}}
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}}
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>()};
75 void test_expressions(bool b) {
76 auto lambda = [a = make()] {};
78 take({});
79 take(Noncopyable());
80 take(Noncopyable{});
81 take(make());
83 Noncopyable &&dc1 = dynamic_cast<Noncopyable&&>(Noncopyable());
84 Noncopyable &&dc2 = dynamic_cast<Noncopyable&&>(nc1);
85 Noncopyable &&dc3 = dynamic_cast<Noncopyable&&>(Derived());
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}}
92 Noncopyable cc1 = (Noncopyable)Noncopyable();
93 Noncopyable cc2 = (Noncopyable)Derived(); // expected-error {{deleted}}
95 Noncopyable fc1 = Noncopyable(Noncopyable());
96 Noncopyable fc2 = Noncopyable(Derived()); // expected-error {{deleted}}
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}}
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>}}
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());
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}}
137 extern const volatile Noncopyable make_cv();
138 Noncopyable cv_difference1 = make_cv();
139 const volatile Noncopyable cv_difference2 = make();
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}}
149 struct AsMember {
150 Noncopyable member;
151 AsMember() : member(make()) {}
153 // FIXME: DR (no number yet): we still get a copy for base or delegating construction.
154 struct AsBase : Noncopyable {
155 AsBase() : Noncopyable(make()) {} // expected-error {{deleted}}
157 struct AsDelegating final {
158 AsDelegating(const AsDelegating &) = delete; // expected-note {{deleted}}
159 static AsDelegating make(int);
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?
164 AsDelegating(int n) : AsDelegating(make(n)) {} // expected-error {{deleted}}
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> {};
172 Foo f(Derived d) { return d; } // expected-error {{invokes a deleted function}}
173 Foo g(Derived d) { return Foo(d); } // ok, calls constructor
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}}
182 struct B {
183 operator A();
184 } C;
185 A::A() : A(C) {} // expected-error {{call to deleted constructor of}}
187 struct A2 {
188 struct B2 {
189 operator A2();
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}}
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}}