kindof.m 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
// RUN: %clang_cc1 -fblocks -fsyntax-only %s -verify -Wmethod-signatures

// Tests Objective-C 'kindof' types.

#if !__has_feature(objc_kindof)
#error does not support __kindof
#endif

@protocol NSObject
@end

@protocol NSCopying
- (id)copy;
+ (Class)classCopy;
@end

@protocol NSRandomProto
- (void)randomMethod;
+ (void)randomClassMethod;
@end

__attribute__((objc_root_class))
@interface NSObject <NSObject>
- (NSObject *)retain;
@end

@interface NSString : NSObject <NSCopying> // expected-note{{receiver is instance of class declared here}}
- (void)compare:(NSString *)string;
- (NSString *)stringByAppendingString:(NSString *)string;
+ (instancetype)string;
@end

@interface NSMutableString : NSString
- (void)appendString:(NSString *)string;
@end

@interface NSNumber : NSObject <NSCopying>
- (NSNumber *)numberByAddingNumber:(NSNumber *)number;
@end

// ---------------------------------------------------------------------------
// Parsing and semantic analysis for __kindof
// ---------------------------------------------------------------------------

// Test proper application of __kindof.
typedef __kindof NSObject *typedef1;
typedef NSObject __kindof *typedef2;
typedef __kindof NSObject<NSCopying> typedef3;
typedef NSObject<NSCopying> __kindof *typedef4;
typedef __kindof id<NSCopying> typedef5;
typedef __kindof Class<NSCopying> typedef6;

// Test redundancy of __kindof.
typedef __kindof id __kindof redundant_typedef1;
typedef __kindof NSObject __kindof *redundant_typedef2;

// Test application of __kindof to typedefs.
typedef NSObject *NSObject_ptr_typedef;
typedef NSObject NSObject_typedef;
typedef __kindof NSObject_ptr_typedef typedef_typedef1;
typedef __kindof NSObject_typedef typedef_typedef2;

// Test application of __kindof to non-object types.
typedef __kindof int nonobject_typedef1; // expected-error{{'__kindof' specifier cannot be applied to non-object type 'int'}}
typedef NSObject **NSObject_ptr_ptr;
typedef __kindof NSObject_ptr_ptr nonobject_typedef2; // expected-error{{'__kindof' specifier cannot be applied to non-object type 'NSObject_ptr_ptr' (aka 'NSObject **')}}

// Test application of __kindof outside of the decl-specifiers.
typedef NSObject * __kindof bad_specifier_location1; // expected-error{{'__kindof' type specifier must precede the declarator}}
typedef NSObject bad_specifier_location2 __kindof; // expected-error{{expected ';' after top level declarator}}
// expected-warning@-1{{declaration does not declare anything}}

// ---------------------------------------------------------------------------
// Pretty printing of __kindof
// ---------------------------------------------------------------------------
void test_pretty_print(int *ip) {
  __kindof NSObject *kindof_NSObject;
  ip = kindof_NSObject; // expected-warning{{from '__kindof NSObject *'}}
 
  __kindof NSObject_ptr_typedef kindof_NSObject_ptr;
  ip = kindof_NSObject_ptr; // expected-warning{{from '__kindof NSObject_ptr_typedef'}}

  __kindof id <NSCopying> *kindof_NSCopying;
  ip = kindof_NSCopying; // expected-warning{{from '__kindof id<NSCopying> *'}}

  __kindof NSObject_ptr_typedef *kindof_NSObject_ptr_typedef;
  ip = kindof_NSObject_ptr_typedef; // expected-warning{{from '__kindof NSObject_ptr_typedef *'}}
}

// ---------------------------------------------------------------------------
// Basic implicit conversions (dropping __kindof, upcasts, etc.)
// ---------------------------------------------------------------------------
void test_add_remove_kindof_conversions(void) {
  __kindof NSObject *kindof_NSObject_obj;
  NSObject *NSObject_obj;

  // Conversion back and forth
  kindof_NSObject_obj = NSObject_obj;
  NSObject_obj = kindof_NSObject_obj;

  // Qualified-id conversion back and forth.
  __kindof id <NSCopying> kindof_id_NSCopying_obj;
  id <NSCopying> id_NSCopying_obj;
  kindof_id_NSCopying_obj = id_NSCopying_obj;
  id_NSCopying_obj = kindof_id_NSCopying_obj;
}

void test_upcast_conversions(void) {
  __kindof NSObject *kindof_NSObject_obj;
  NSObject *NSObject_obj;

  // Upcasts
  __kindof NSString *kindof_NSString_obj;
  NSString *NSString_obj;
  kindof_NSObject_obj = kindof_NSString_obj;
  kindof_NSObject_obj = NSString_obj;
  NSObject_obj = kindof_NSString_obj;
  NSObject_obj = NSString_obj;

  // "Upcasts" with qualified-id.
  __kindof id <NSCopying> kindof_id_NSCopying_obj;
  id <NSCopying> id_NSCopying_obj;
  kindof_id_NSCopying_obj = kindof_NSString_obj;
  kindof_id_NSCopying_obj = NSString_obj;
  id_NSCopying_obj = kindof_NSString_obj;
  id_NSCopying_obj = NSString_obj;
}


void test_ptr_object_conversions(void) {
  __kindof NSObject **ptr_kindof_NSObject_obj;
  NSObject **ptr_NSObject_obj;

  // Conversions back and forth.
  ptr_kindof_NSObject_obj = ptr_NSObject_obj;
  ptr_NSObject_obj = ptr_kindof_NSObject_obj;

  // Conversions back and forth with qualified-id.
  __kindof id <NSCopying> *ptr_kindof_id_NSCopying_obj;
  id <NSCopying> *ptr_id_NSCopying_obj;
  ptr_kindof_id_NSCopying_obj = ptr_id_NSCopying_obj;
  ptr_id_NSCopying_obj = ptr_kindof_id_NSCopying_obj;

  // Upcasts.
  __kindof NSString **ptr_kindof_NSString_obj;
  NSString **ptr_NSString_obj;
  ptr_kindof_NSObject_obj = ptr_kindof_NSString_obj;
  ptr_kindof_NSObject_obj = ptr_NSString_obj;
  ptr_NSObject_obj = ptr_kindof_NSString_obj;
  ptr_NSObject_obj = ptr_NSString_obj;
}

// ---------------------------------------------------------------------------
// Implicit downcasting
// ---------------------------------------------------------------------------
void test_downcast_conversions(void) {
  __kindof NSObject *kindof_NSObject_obj;
  NSObject *NSObject_obj;
  __kindof NSString *kindof_NSString_obj;
  NSString *NSString_obj;

  // Implicit downcasting.
  kindof_NSString_obj = kindof_NSObject_obj;
  kindof_NSString_obj = NSObject_obj; // expected-warning{{assigning to '__kindof NSString *' from 'NSObject *'}}
  NSString_obj = kindof_NSObject_obj;
  NSString_obj = NSObject_obj; // expected-warning{{assigning to 'NSString *' from 'NSObject *'}}

  // Implicit downcasting with qualified id.
  __kindof id <NSCopying> kindof_NSCopying_obj;
  id <NSCopying> NSCopying_obj;
  kindof_NSString_obj = kindof_NSCopying_obj;
  kindof_NSString_obj = NSCopying_obj; // expected-warning{{from incompatible type 'id<NSCopying>'}}
  NSString_obj = kindof_NSCopying_obj;
  NSString_obj = NSCopying_obj; // expected-warning{{from incompatible type 'id<NSCopying>'}}
  kindof_NSObject_obj = kindof_NSCopying_obj;
  kindof_NSObject_obj = NSCopying_obj; // expected-warning{{from incompatible type 'id<NSCopying>'}}
  NSObject_obj = kindof_NSCopying_obj;
  NSObject_obj = NSCopying_obj; // expected-warning{{from incompatible type 'id<NSCopying>'}}
}

void test_crosscast_conversions(void) {
  __kindof NSString *kindof_NSString_obj;
  NSString *NSString_obj;
  __kindof NSNumber *kindof_NSNumber_obj;
  NSNumber *NSNumber_obj;

  NSString_obj = kindof_NSNumber_obj; // expected-warning{{from '__kindof NSNumber *'}}
}

@interface NSCell : NSObject
@end
@interface NSCellSub : NSCell
@end
@interface NSCellSub2 : NSCell
@end
@interface NSCellSubSub : NSCellSub
@end

typedef signed char BOOL;
void test_conditional(BOOL flag) {
  NSCellSubSub *result;
  __kindof NSCellSub *kindof_Sub;
  NSCell *cell;
  NSCellSub *sub;
  NSCellSub2 *sub2;
  NSCellSubSub *subsub;

  // LHS is kindof NSCellSub, RHS is NSCell --> kindof NSCell
  // LHS is kindof NSCellSub, RHS is NSCellSub --> kindof NSCellSub
  // LHS is kindof NSCellSub, RHS is NSCellSub2 --> kindof NSCell
  // LHS is kindof NSCellSub, RHS is NSCellSubSub --> kindof NSCellSub
  result = flag ? kindof_Sub : cell;
  result = flag ? kindof_Sub : sub;
  result = flag ? kindof_Sub : sub2;
  result = flag ? kindof_Sub : subsub;

  result = flag ? cell : kindof_Sub;
  result = flag ? sub : kindof_Sub;
  result = flag ? sub2 : kindof_Sub;
  result = flag ? subsub : kindof_Sub;
}

// ---------------------------------------------------------------------------
// Blocks
// ---------------------------------------------------------------------------
void test_block_conversions(void) {
  // Adding/removing __kindof from return type.
  __kindof NSString *(^kindof_NSString_void_block)(void);
  NSString *(^NSString_void_block)(void);
  kindof_NSString_void_block = NSString_void_block;
  NSString_void_block = kindof_NSString_void_block;

  // Covariant return type.
  __kindof NSMutableString *(^kindof_NSMutableString_void_block)(void);
  NSMutableString *(^NSMutableString_void_block)(void);
  kindof_NSString_void_block = NSMutableString_void_block;
  NSString_void_block = kindof_NSMutableString_void_block;
  kindof_NSString_void_block = NSMutableString_void_block;
  NSString_void_block = kindof_NSMutableString_void_block;

  // "Covariant" return type via downcasting rule.
  kindof_NSMutableString_void_block = NSString_void_block; // expected-error{{from 'NSString *(^)(void)'}}
  NSMutableString_void_block = kindof_NSString_void_block;
  kindof_NSMutableString_void_block = NSString_void_block; // expected-error{{from 'NSString *(^)(void)'}}
  NSMutableString_void_block = kindof_NSString_void_block;

  // Cross-casted return type.
  __kindof NSNumber *(^kindof_NSNumber_void_block)(void);
  NSNumber *(^NSNumber_void_block)(void);
  kindof_NSString_void_block = NSNumber_void_block; // expected-error{{from 'NSNumber *(^)(void)'}}
  NSString_void_block = kindof_NSNumber_void_block; // expected-error{{'__kindof NSNumber *(^)(void)'}}
  kindof_NSString_void_block = NSNumber_void_block; // expected-error{{from 'NSNumber *(^)(void)'}}
  NSString_void_block = kindof_NSNumber_void_block; // expected-error{{'__kindof NSNumber *(^)(void)'}}

  // Adding/removing __kindof from argument type.
  void (^void_kindof_NSString_block)(__kindof NSString *);
  void (^void_NSString_block)(NSString *);
  void_kindof_NSString_block = void_NSString_block;
  void_NSString_block = void_kindof_NSString_block;

  // Contravariant argument type.
  void (^void_kindof_NSMutableString_block)(__kindof NSMutableString *);
  void (^void_NSMutableString_block)(NSMutableString *);
  void_kindof_NSMutableString_block = void_kindof_NSString_block;
  void_kindof_NSMutableString_block = void_NSString_block;
  void_NSMutableString_block = void_kindof_NSString_block;
  void_NSMutableString_block = void_NSString_block;

  // "Contravariant" argument type via downcasting rule.
  void_kindof_NSString_block = void_kindof_NSMutableString_block;
  void_kindof_NSString_block = void_NSMutableString_block;
  void_NSString_block = void_kindof_NSMutableString_block; // expected-error{{from 'void (^)(__kindof NSMutableString *)'}}
  void_NSString_block = void_NSMutableString_block; // expected-error{{from 'void (^)(NSMutableString *)'}}
}

// ---------------------------------------------------------------------------
// Messaging __kindof types.
// ---------------------------------------------------------------------------
void message_kindof_object(__kindof NSString *kindof_NSString) {
  [kindof_NSString retain]; // in superclass
  [kindof_NSString stringByAppendingString:0]; // in class
  [kindof_NSString appendString:0]; // in subclass
  [kindof_NSString numberByAddingNumber: 0]; // expected-warning{{instance method '-numberByAddingNumber:' not found (return type defaults to 'id')}}
  [kindof_NSString randomMethod]; // in protocol
}

void message_kindof_qualified_id(__kindof id <NSCopying> kindof_NSCopying) {
  [kindof_NSCopying copy]; // in protocol
  [kindof_NSCopying stringByAppendingString:0]; // in some class
  [kindof_NSCopying randomMethod]; // in unrelated protocol
}

void message_kindof_qualified_class(
       __kindof Class <NSCopying> kindof_NSCopying) {
  [kindof_NSCopying classCopy]; // in protocol
  [kindof_NSCopying string]; // in some class
  [kindof_NSCopying randomClassMethod]; // in unrelated protocol
}

// Make sure we don't emit warning about multiple methods found.
typedef int NSInteger;
@interface Foo : NSObject
- (NSString*)test;
@end
@interface Bar : NSObject
- (NSInteger)test;
@end
void test(__kindof Bar *kBar) {
    [kBar test];
}

// Make sure we don't emit warning about no method found.
@interface A : NSObject
@property (readonly, getter=isActive) BOOL active;
@end
@interface B : NSObject
@property (getter=isActive, readonly) BOOL active;
@end
void foo() {
  __kindof B *NSApp;
  if ([NSApp isActive]) {
  }
}

typedef const struct CGPath *CGPathRef;
@interface C : NSObject
@property (copy) NSString *path;
@end
@interface D : NSObject
@property CGPathRef path __attribute__((availability(macosx,unavailable)));
@end
// Make sure we choose "NSString *path" for [s1 path].
void bar(id s1, id s2) {
  return [[s1 path] compare:[s2 path]];
}

// ---------------------------------------------------------------------------
// __kindof within specialized types
// ---------------------------------------------------------------------------
@interface NSArray<T> : NSObject
@end

void implicit_convert_array(NSArray<__kindof NSString *> *kindofStringsArray,
                            NSArray<NSString *> *stringsArray,
                            NSArray<__kindof NSMutableString *>
                              *kindofMutStringsArray,
                            NSArray<NSMutableString *> *mutStringsArray) {
  // Adding/removing __kindof is okay.
  kindofStringsArray = stringsArray;
  stringsArray = kindofStringsArray;

  // Other covariant and contravariant conversions still not permitted.
  kindofStringsArray = mutStringsArray; // expected-warning{{incompatible pointer types}}
  stringsArray = kindofMutStringsArray; // expected-warning{{incompatible pointer types}}
  mutStringsArray = kindofStringsArray; // expected-warning{{incompatible pointer types}}

  // Adding/removing nested __kindof is okay.
  NSArray<NSArray<__kindof NSString *> *> *kindofStringsArrayArray;
  NSArray<NSArray<NSString *> *> *stringsArrayArray;
  kindofStringsArrayArray = stringsArrayArray;
  stringsArrayArray = kindofStringsArrayArray;
}

// ---------------------------------------------------------------------------
// __kindof + nullability
// ---------------------------------------------------------------------------

void testNullability() {
  // The base type being a pointer type tickles the bug.
  extern __kindof id <NSCopying> _Nonnull getSomeCopyable();
  NSString *string = getSomeCopyable(); // no-warning

  void processCopyable(__typeof(getSomeCopyable()) string);
  processCopyable(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
}

// Make sure that we don't emit a warning about conflicting parameter types
// between __kindof id and id.
@interface A2 : NSObject
- (void)test:(__kindof id)T;
@end
@implementation A2
- (void)test:(id)T {
}
@end

// ---------------------------------------------------------------------------
// __kindof on type parameters
// ---------------------------------------------------------------------------

@interface NSGeneric<ObjectType> : NSObject
- (void)test:(__kindof ObjectType)T; // expected-note{{passing argument to parameter 'T' here}}
- (void)mapUsingBlock:(id (^)(__kindof ObjectType))block;
@property (copy) ObjectType object;
@property (copy) __kindof ObjectType kindof_object;

@property (copy) __kindof ObjectType _Nonnull nonnull_kindof_object;
@end
@implementation NSGeneric
- (void)test:(id)T {
}
- (void)mapUsingBlock:(id (^)(id))block {
}
@end

@interface NSDefaultGeneric<ObjectType : NSString *> : NSObject
@property (copy) ObjectType object;
@property (copy) __kindof ObjectType kindof_object;
@end

void testGeneric(NSGeneric<NSString*> *generic) {
  NSObject *NSObject_obj;
  // Assign from NSObject_obj to __kindof NSString*.
  [generic test:NSObject_obj]; // expected-warning{{incompatible pointer types sending 'NSObject *' to parameter of type '__kindof NSString *'}}
  NSString *NSString_str;
  [generic test:NSString_str];
}

void testGenericAssignment() {
  NSMutableString *NSMutableString_str;
  NSNumber *NSNumber_obj;

  NSGeneric<NSString*> *generic;
  NSMutableString_str = generic.object; // expected-warning{{incompatible pointer types}}
  NSNumber_obj = generic.object; // expected-warning{{incompatible pointer types}}
  NSMutableString_str = generic.kindof_object;
  NSNumber_obj = generic.kindof_object; // expected-warning{{incompatible pointer types assigning to 'NSNumber *' from '__kindof NSString *'}}

  NSGeneric<__kindof NSString*> *kindof_generic;
  NSMutableString_str = kindof_generic.object;
  NSNumber_obj = kindof_generic.object; // expected-warning{{incompatible pointer types assigning to 'NSNumber *' from '__kindof NSString *'}}
  NSMutableString_str = kindof_generic.kindof_object;
  NSNumber_obj = kindof_generic.kindof_object; // expected-warning{{incompatible pointer types assigning to 'NSNumber *' from '__kindof __kindof NSString *'}}

  NSDefaultGeneric *default_generic;
  NSMutableString_str = default_generic.object;
  NSNumber_obj = default_generic.object; // expected-warning{{incompatible pointer types}}
  NSMutableString_str = default_generic.kindof_object;
  NSNumber_obj = default_generic.kindof_object; // expected-warning{{incompatible pointer types assigning to 'NSNumber *' from '__kindof __kindof NSString *'}}

  typedef NSString *Typedef_NSString;
  NSGeneric<Typedef_NSString> *typedef_generic;
  NSMutableString_str = typedef_generic.object; // expected-warning{{incompatible pointer types}}
  NSNumber_obj = typedef_generic.object; // expected-warning{{incompatible pointer types}}
  NSMutableString_str = typedef_generic.kindof_object;
  NSNumber_obj = typedef_generic.kindof_object; // expected-warning{{incompatible pointer types assigning to 'NSNumber *' from '__kindof Typedef_NSString'}}
}

void testKindofNonObjectType() {
  typedef void (^BlockType)(int);
  NSGeneric<BlockType> *generic;
}

void testKindofNullability(NSGeneric<NSString*> *generic) {
  generic.nonnull_kindof_object = 0; // expected-warning{{null passed to a callee that requires a non-null argument}}
}

// Check that clang doesn't crash when a type parameter is illegal.
@interface Array1<T> : NSObject
@end

@interface I1 : NSObject
@end

@interface Array1<__kindof I1*>(extensions1) // expected-error{{expected type parameter name}}
@end

@interface Array2<T1, T2, T3> : NSObject
@end

@interface Array2<T, T, __kindof I1*>(extensions2) // expected-error{{expected type parameter name}} expected-error{{redeclaration of type parameter 'T'}}
@end