1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -fobjc-weak -verify -fblocks -fobjc-exceptions %s
3 // "Move" semantics, trivial version.
4 void move_it(__strong id &&from) {
5 id to = static_cast<__strong id&&>(from);
8 // Deduction with 'auto'.
14 // don't warn about this
15 extern "C" A* MakeA();
17 // Ensure that deduction works with lifetime qualifiers.
18 void deduction(id obj) {
19 auto a = [[A alloc] init];
20 __strong A** aPtr = &a;
22 auto a2([[A alloc] init]);
23 __strong A** aPtr2 = &a2;
25 __strong id *idp = new auto(obj);
27 __strong id array[17];
28 for (auto x : array) { // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
29 __strong id *xPtr = &x;
33 } @catch (auto e) { // expected-error {{'auto' not allowed in exception declaration}}
38 __autoreleasing id p; // expected-note 2 {{'p' declared here}}
40 (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
41 (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
45 __autoreleasing id &p = v; // expected-note 2 {{'p' declared here}}
47 (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
48 (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
51 __autoreleasing id v; // expected-note {{'v' declared here}}
52 __autoreleasing id &p = v;
53 (void) ^{ (void) p; };
54 (void) ^{ (void) v; }; // expected-error {{cannot capture __autoreleasing variable in a block}}
57 // warn when initializing an 'auto' variable with an 'id' initializer expression
59 void testAutoId(id obj) {
60 auto x = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
65 - (id)objectAtIndex:(int)index;
68 // ...but don't warn if it's coming from a template parameter.
69 template<typename T, int N>
70 void autoTemplateFunction(T param, id obj, Array *arr) {
71 auto x = param; // no-warning
72 auto y = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'y'}}
73 auto z = [arr objectAtIndex:N]; // expected-warning{{'auto' deduced as 'id' in declaration of 'z'}}
76 void testAutoIdTemplate(id obj) {
77 autoTemplateFunction<id, 2>(obj, obj, [Array new]); // no-warning
80 @interface NSObject @end
81 typedef __builtin_va_list va_list;
82 @interface MyClass : NSObject
85 @implementation MyClass
86 + (void)fooMethod:(id)firstArg, ... {
89 __builtin_va_arg(args, id);
93 namespace rdar12078752 {
96 __autoreleasing decltype(o) o2 = o;
97 __autoreleasing auto o3 = o;
101 namespace test_err_arc_array_param_no_ownership {
106 func([](A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}
107 func(^(A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}
111 namespace test_union {
112 // Implicitly-declared special functions of a union are deleted by default if
113 // ARC is enabled and the union has an ObjC pointer field.
115 id f0; // expected-note 6 {{'U0' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
119 __weak id f0; // expected-note 12 {{'U1' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
120 U1() = default; // expected-warning {{explicitly defaulted default constructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}
121 ~U1() = default; // expected-warning {{explicitly defaulted destructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}
122 U1(const U1 &) = default; // expected-warning {{explicitly defaulted copy constructor is implicitly deleted}} expected-note 2 {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}
123 U1(U1 &&) = default; // expected-warning {{explicitly defaulted move constructor is implicitly deleted}} expected-note{{replace 'default'}}
124 U1 & operator=(const U1 &) = default; // expected-warning {{explicitly defaulted copy assignment operator is implicitly deleted}} expected-note 2 {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}
125 U1 & operator=(U1 &&) = default; // expected-warning {{explicitly defaulted move assignment operator is implicitly deleted}} expected-note{{replace 'default'}}
130 // If the ObjC pointer field of a union has a default member initializer, the
131 // implicitly-declared default constructor of the union is not deleted by
134 id f0 = getStrong(); // expected-note 4 {{'U2' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
138 // It's fine if the user has explicitly defined the special functions.
145 U3 & operator=(const U3 &);
146 U3 & operator=(U3 &&);
149 // ObjC pointer fields in anonymous union fields delete the defaulted special
150 // functions of the containing class.
153 id f0; // expected-note-re 6 {{{{.*}} of '(anonymous union at {{.*}})' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
160 union { // expected-note-re {{copy constructor of 'S1' is implicitly deleted because field 'test_union::S1::(anonymous union at {{.*}})' has a deleted copy constructor}} expected-note-re {{copy assignment operator of 'S1' is implicitly deleted because field 'test_union::S1::(anonymous union at {{.*}})' has a deleted copy assignment operator}} expected-note-re 4 {{'S1' is implicitly deleted because field 'test_union::S1::(anonymous union at {{.*}})' has a deleted}}
161 id f0; // expected-note-re 2 {{{{.*}} of '(anonymous union at {{.*}}' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
170 // FIXME: the note should say 'f0' is causing the special functions to be deleted.
171 struct { // expected-note-re 6 {{'S2' is implicitly deleted because variant field 'test_union::S2::(anonymous struct at {{.*}})' has a non-trivial}}
188 static union { // expected-error {{call to implicitly-deleted default constructor of}}
189 id g0; // expected-note-re {{default constructor of '(unnamed union at {{.*}}' is implicitly deleted because variant field 'g0' is an ObjC pointer}}
192 static union { // expected-error {{call to implicitly-deleted default constructor of}}
193 union { // expected-note-re {{default constructor of '(unnamed union at {{.*}}' is implicitly deleted because field 'test_union::(anonymous union at {{.*}})' has a deleted default constructor}}
194 union { // expected-note-re {{default constructor of '(anonymous union at {{.*}}' is implicitly deleted because field 'test_union::(anonymous union at {{.*}})' has a deleted default constructor}}
195 __weak id g1; // expected-note-re {{default constructor of '(anonymous union at {{.*}}' is implicitly deleted because variant field 'g1' is an ObjC pointer}}
203 void testDefaultConstructor() {
204 U0 t0; // expected-error {{call to implicitly-deleted default constructor}}
205 U1 t1; // expected-error {{call to implicitly-deleted default constructor}}
208 S0 t4; // expected-error {{call to implicitly-deleted default constructor}}
209 S1 t5; // expected-error {{call to implicitly-deleted default constructor}}
210 S2 t6; // expected-error {{call to implicitly-deleted default constructor}}
213 void testDestructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
214 delete u0; // expected-error {{attempt to use a deleted function}}
215 delete u1; // expected-error {{attempt to use a deleted function}}
218 delete s0; // expected-error {{attempt to use a deleted function}}
219 delete s1; // expected-error {{attempt to use a deleted function}}
220 delete s2; // expected-error {{attempt to use a deleted function}}
223 void testCopyConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
224 U0 t0(*u0); // expected-error {{call to implicitly-deleted copy constructor}}
225 U1 t1(*u1); // expected-error {{call to implicitly-deleted copy constructor}}
226 U2 t2(*u2); // expected-error {{call to implicitly-deleted copy constructor}}
228 S0 t4(*s0); // expected-error {{call to implicitly-deleted copy constructor}}
229 S1 t5(*s1); // expected-error {{call to implicitly-deleted copy constructor}}
230 S2 t6(*s2); // expected-error {{call to implicitly-deleted copy constructor}}
233 void testCopyAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
234 *x0 = *u0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
235 *x1 = *u1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
236 *x2 = *u2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
238 *x4 = *s0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
239 *x5 = *s1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
240 *x6 = *s2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
243 // The diagnostics below refer to the deleted copy constructors and assignment
244 // operators since defaulted move constructors and assignment operators that are
245 // defined as deleted are ignored by overload resolution.
247 void testMoveConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
248 U0 t0(static_cast<U0 &&>(*u0)); // expected-error {{call to implicitly-deleted copy constructor}}
249 U1 t1(static_cast<U1 &&>(*u1)); // expected-error {{call to implicitly-deleted copy constructor}}
250 U2 t2(static_cast<U2 &&>(*u2)); // expected-error {{call to implicitly-deleted copy constructor}}
251 U3 t3(static_cast<U3 &&>(*u3));
252 S0 t4(static_cast<S0 &&>(*s0)); // expected-error {{call to implicitly-deleted copy constructor}}
253 S1 t5(static_cast<S1 &&>(*s1)); // expected-error {{call to implicitly-deleted copy constructor}}
254 S2 t6(static_cast<S2 &&>(*s2)); // expected-error {{call to implicitly-deleted copy constructor}}
257 void testMoveAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
258 *x0 = static_cast<U0 &&>(*u0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
259 *x1 = static_cast<U1 &&>(*u1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
260 *x2 = static_cast<U2 &&>(*u2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
261 *x3 = static_cast<U3 &&>(*u3);
262 *x4 = static_cast<S0 &&>(*s0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
263 *x5 = static_cast<S1 &&>(*s1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
264 *x6 = static_cast<S2 &&>(*s2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}