272 lines · plain
1// RUN: %clang_cc1 -std=c++11 -fobjc-arc -fobjc-runtime-has-weak -fobjc-weak -verify -fblocks -fobjc-exceptions -emit-llvm -o - %s2 3// "Move" semantics, trivial version.4void move_it(__strong id &&from) {5 id to = static_cast<__strong id&&>(from);6}7 8// Deduction with 'auto'.9@interface A10+ alloc;11- init;12@end13 14// don't warn about this15extern "C" A* MakeA();16 17void test_nonexistent_method(A *a) {18 // This used to crash in codegen.19 auto a1 = [a foo]; // expected-error {{no visible @interface for 'A' declares the selector 'foo'}}20}21 22// Ensure that deduction works with lifetime qualifiers.23void deduction(id obj) {24 auto a = [[A alloc] init];25 __strong A** aPtr = &a;26 27 auto a2([[A alloc] init]);28 __strong A** aPtr2 = &a2;29 30 __strong id *idp = new auto(obj);31 32 __strong id array[17];33 for (auto x : array) { // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}34 __strong id *xPtr = &x;35 }36 37 @try {38 } @catch (auto e) { // expected-error {{'auto' not allowed in exception declaration}}39 }40}41 42void test1a() {43 __autoreleasing id p; // expected-note 2 {{'p' declared here}}44 (void) [&p] {};45 (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}46 (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}47}48void test1b() {49 __autoreleasing id v;50 __autoreleasing id &p = v; // expected-note 2 {{'p' declared here}}51 (void) [&p] {};52 (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}53 (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}54}55void test1c() {56 __autoreleasing id v; // expected-note {{'v' declared here}}57 __autoreleasing id &p = v;58 (void) ^{ (void) p; };59 (void) ^{ (void) v; }; // expected-error {{cannot capture __autoreleasing variable in a block}}60}61 62// warn when initializing an 'auto' variable with an 'id' initializer expression63 64void testAutoId(id obj) {65 auto x = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}66}67 68@interface Array69+ (instancetype)new;70- (id)objectAtIndex:(int)index;71@end72 73// ...but don't warn if it's coming from a template parameter.74template<typename T, int N>75void autoTemplateFunction(T param, id obj, Array *arr) {76 auto x = param; // no-warning77 auto y = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'y'}}78 auto z = [arr objectAtIndex:N]; // expected-warning{{'auto' deduced as 'id' in declaration of 'z'}}79}80 81void testAutoIdTemplate(id obj) {82 autoTemplateFunction<id, 2>(obj, obj, [Array new]); // no-warning83}84 85@interface NSObject @end86typedef __builtin_va_list va_list;87@interface MyClass : NSObject88@end89 90@implementation MyClass91+ (void)fooMethod:(id)firstArg, ... {92 va_list args;93 94 __builtin_va_arg(args, id);95}96@end97 98namespace rdar12078752 {99 void f() {100 NSObject* o =0;101 __autoreleasing decltype(o) o2 = o;102 __autoreleasing auto o3 = o;103 }104}105 106namespace test_err_arc_array_param_no_ownership {107 template <class T>108 void func(T a) {}109 110 void test() {111 func([](A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}112 func(^(A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}113 }114}115 116namespace test_union {117 // Implicitly-declared special functions of a union are deleted by default if118 // ARC is enabled and the union has an ObjC pointer field.119 union U0 {120 id f0; // expected-note 6 {{'U0' is implicitly deleted because variant field 'f0' is an ObjC pointer}}121 };122 123 union U1 {124 __weak id f0; // expected-note 12 {{'U1' is implicitly deleted because variant field 'f0' is an ObjC pointer}}125 U1() = default; // expected-warning {{explicitly defaulted default constructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}126 ~U1() = default; // expected-warning {{explicitly defaulted destructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}} expected-note{{replace 'default'}}127 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'}}128 U1(U1 &&) = default; // expected-warning {{explicitly defaulted move constructor is implicitly deleted}} expected-note{{replace 'default'}}129 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'}}130 U1 & operator=(U1 &&) = default; // expected-warning {{explicitly defaulted move assignment operator is implicitly deleted}} expected-note{{replace 'default'}}131 };132 133 id getStrong();134 135 // If the ObjC pointer field of a union has a default member initializer, the136 // implicitly-declared default constructor of the union is not deleted by137 // default.138 union U2 {139 id f0 = getStrong(); // expected-note 4 {{'U2' is implicitly deleted because variant field 'f0' is an ObjC pointer}}140 ~U2();141 };142 143 // It's fine if the user has explicitly defined the special functions.144 union U3 {145 id f0;146 U3();147 ~U3();148 U3(const U3 &);149 U3(U3 &&);150 U3 & operator=(const U3 &);151 U3 & operator=(U3 &&);152 };153 154 // ObjC pointer fields in anonymous union fields delete the defaulted special155 // functions of the containing class.156 struct S0 {157 union {158 id f0; // expected-note-re 6 {{{{.*}} of '(anonymous union at {{.*}})' is implicitly deleted because variant field 'f0' is an ObjC pointer}}159 char f1;160 };161 };162 163 struct S1 {164 union {165 union { // expected-note-re {{copy constructor of 'S1' is implicitly deleted because field 'test_union::S1::(anonymous union)::(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)::(anonymous union at {{.*}})' has a deleted copy assignment operator}} expected-note-re 4 {{'S1' is implicitly deleted because field 'test_union::S1::(anonymous union)::(anonymous union at {{.*}})' has a deleted}}166 id f0; // expected-note-re 2 {{{{.*}} of '(anonymous union at {{.*}}' is implicitly deleted because variant field 'f0' is an ObjC pointer}}167 char f1;168 };169 int f2;170 };171 };172 173 struct S2 {174 union {175 // FIXME: the note should say 'f0' is causing the special functions to be deleted.176 struct { // expected-note-re 6 {{'S2' is implicitly deleted because variant field 'test_union::S2::(anonymous union)::(anonymous struct at {{.*}})' has a non-trivial}}177 id f0;178 int f1;179 };180 int f2;181 };182 int f3;183 };184 185 U0 *x0;186 U1 *x1;187 U2 *x2;188 U3 *x3;189 S0 *x4;190 S1 *x5;191 S2 *x6;192 193 static union { // expected-error {{call to implicitly-deleted default constructor of}}194 id g0; // expected-note-re {{default constructor of '(unnamed union at {{.*}}' is implicitly deleted because variant field 'g0' is an ObjC pointer}}195 };196 197 static union { // expected-error {{call to implicitly-deleted default constructor of}}198 union { // expected-note-re {{default constructor of '(unnamed union at {{.*}}' is implicitly deleted because field 'test_union::(anonymous union)::(anonymous union at {{.*}})' has a deleted default constructor}}199 union { // expected-note-re {{default constructor of '(anonymous union at {{.*}}' is implicitly deleted because field 'test_union::(anonymous union)::(anonymous union)::(anonymous union at {{.*}})' has a deleted default constructor}}200 __weak id g1; // expected-note-re {{default constructor of '(anonymous union at {{.*}}' is implicitly deleted because variant field 'g1' is an ObjC pointer}}201 int g2;202 };203 int g3;204 };205 int g4;206 };207 208 void testDefaultConstructor() {209 U0 t0; // expected-error {{call to implicitly-deleted default constructor}}210 U1 t1; // expected-error {{call to implicitly-deleted default constructor}}211 U2 t2;212 U3 t3;213 S0 t4; // expected-error {{call to implicitly-deleted default constructor}}214 S1 t5; // expected-error {{call to implicitly-deleted default constructor}}215 S2 t6; // expected-error {{call to implicitly-deleted default constructor}}216 }217 218 void testDestructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {219 delete u0; // expected-error {{attempt to use a deleted function}}220 delete u1; // expected-error {{attempt to use a deleted function}}221 delete u2;222 delete u3;223 delete s0; // expected-error {{attempt to use a deleted function}}224 delete s1; // expected-error {{attempt to use a deleted function}}225 delete s2; // expected-error {{attempt to use a deleted function}}226 }227 228 void testCopyConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {229 U0 t0(*u0); // expected-error {{call to implicitly-deleted copy constructor}}230 U1 t1(*u1); // expected-error {{call to implicitly-deleted copy constructor}}231 U2 t2(*u2); // expected-error {{call to implicitly-deleted copy constructor}}232 U3 t3(*u3);233 S0 t4(*s0); // expected-error {{call to implicitly-deleted copy constructor}}234 S1 t5(*s1); // expected-error {{call to implicitly-deleted copy constructor}}235 S2 t6(*s2); // expected-error {{call to implicitly-deleted copy constructor}}236 }237 238 void testCopyAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {239 *x0 = *u0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}240 *x1 = *u1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}241 *x2 = *u2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}242 *x3 = *u3;243 *x4 = *s0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}244 *x5 = *s1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}245 *x6 = *s2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}246 }247 248 // The diagnostics below refer to the deleted copy constructors and assignment249 // operators since defaulted move constructors and assignment operators that are250 // defined as deleted are ignored by overload resolution.251 252 void testMoveConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {253 U0 t0(static_cast<U0 &&>(*u0)); // expected-error {{call to implicitly-deleted copy constructor}}254 U1 t1(static_cast<U1 &&>(*u1)); // expected-error {{call to implicitly-deleted copy constructor}}255 U2 t2(static_cast<U2 &&>(*u2)); // expected-error {{call to implicitly-deleted copy constructor}}256 U3 t3(static_cast<U3 &&>(*u3));257 S0 t4(static_cast<S0 &&>(*s0)); // expected-error {{call to implicitly-deleted copy constructor}}258 S1 t5(static_cast<S1 &&>(*s1)); // expected-error {{call to implicitly-deleted copy constructor}}259 S2 t6(static_cast<S2 &&>(*s2)); // expected-error {{call to implicitly-deleted copy constructor}}260 }261 262 void testMoveAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {263 *x0 = static_cast<U0 &&>(*u0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}264 *x1 = static_cast<U1 &&>(*u1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}265 *x2 = static_cast<U2 &&>(*u2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}266 *x3 = static_cast<U3 &&>(*u3);267 *x4 = static_cast<S0 &&>(*s0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}268 *x5 = static_cast<S1 &&>(*s1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}269 *x6 = static_cast<S2 &&>(*s2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}270 }271}272