// RUN: %clang_cc1 -fblocks -fsyntax-only -std=c++11 %s -verify // // Test the substitution of type arguments for type parameters when // using parameterized classes in Objective-C. __attribute__((objc_root_class)) @interface NSObject + (instancetype)alloc; - (instancetype)init; @end @protocol NSCopying @end @interface NSString : NSObject @end @interface NSMutableString : NSString @end @interface NSNumber : NSObject @end @interface NSArray : NSObject { @public T *data; // don't try this at home } - (T)objectAtIndexedSubscript:(int)index; + (NSArray *)array; @property (copy,nonatomic) T lastObject; @end @interface NSMutableArray : NSArray -(instancetype)initWithArray:(NSArray *)array; // expected-note{{passing argument}} - (void)setObject:(T)object atIndexedSubscript:(int)index; // expected-note 2{{passing argument to parameter 'object' here}} @end @interface NSStringArray : NSArray @end @interface NSSet : NSObject - (T)firstObject; @property (nonatomic, copy) NSArray *allObjects; @end // Parameterized inheritance (simple case) @interface NSMutableSet> : NSSet - (void)addObject:(U)object; // expected-note 7{{passing argument to parameter 'object' here}} @end @interface Widget : NSObject @end // Non-parameterized class inheriting from a specialization of a // parameterized class. @interface WidgetSet : NSMutableSet @end // Parameterized inheritance with a more interesting transformation in // the specialization. @interface MutableSetOfArrays : NSMutableSet*> @end // Inheriting from an unspecialized form of a parameterized type. @interface UntypedMutableSet : NSMutableSet @end @interface Window : NSObject @end @interface NSDictionary : NSObject - (V)objectForKeyedSubscript:(K)key; // expected-note 2{{parameter 'key'}} @end @interface NSMutableDictionary, V> : NSDictionary // expected-note 2{{type parameter 'K' declared here}} \ // expected-note 2{{'NSMutableDictionary' declared here}} - (void)setObject:(V)object forKeyedSubscript:(K)key; // expected-note@-1 {{parameter 'object' here}} // expected-note@-2 {{parameter 'object' here}} // expected-note@-3 {{parameter 'key' here}} // expected-note@-4 {{parameter 'key' here}} @property (strong) K someRandomKey; @end @interface WindowArray : NSArray @end @interface NSSet (Searching) - (T)findObject:(T)object; @end // -------------------------------------------------------------------------- // Message sends. // -------------------------------------------------------------------------- void test_message_send_result( NSSet *stringSet, NSMutableSet *mutStringSet, WidgetSet *widgetSet, UntypedMutableSet *untypedMutSet, MutableSetOfArrays *mutStringArraySet, NSSet *set, NSMutableSet *mutSet, MutableSetOfArrays *mutArraySet, NSArray *stringArray, void (^block)(void)) { int *ip; ip = [stringSet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} ip = [mutStringSet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} ip = [widgetSet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'Widget *'}} ip = [untypedMutSet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = [mutStringArraySet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = [set firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = [mutSet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = [mutArraySet firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = [block firstObject]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = [stringSet findObject:@"blah"]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} // Class messages. ip = [NSSet alloc]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSSet *'}} ip = [NSSet alloc]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSSet *'}} ip = [MutableSetOfArrays alloc]; // expected-error{{incompatible pointer types assigning to 'int *' from 'MutableSetOfArrays *'}} ip = [MutableSetOfArrays alloc]; // expected-error{{incompatible pointer types assigning to 'int *' from 'MutableSetOfArrays *'}} ip = [NSArray array]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = [NSArray array]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = [[NSMutableArray alloc] init]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSMutableArray *'}} [[NSMutableArray alloc] initWithArray: stringArray]; // okay [[NSMutableArray alloc] initWithArray: stringArray]; // okay [[NSMutableArray alloc] initWithArray: stringArray]; // expected-error{{parameter of type 'NSArray *' with an lvalue of type 'NSArray *'}} } void test_message_send_param( NSMutableSet *mutStringSet, WidgetSet *widgetSet, UntypedMutableSet *untypedMutSet, MutableSetOfArrays *mutStringArraySet, NSMutableSet *mutSet, MutableSetOfArrays *mutArraySet, void (^block)(void)) { Window *window; [mutStringSet addObject: window]; // expected-error{{parameter of type 'NSString *'}} [widgetSet addObject: window]; // expected-error{{parameter of type 'Widget *'}} [untypedMutSet addObject: window]; // expected-error{{parameter of type 'id'}} [mutStringArraySet addObject: window]; // expected-error{{parameter of type 'NSArray *'}} [mutSet addObject: window]; // expected-error{{parameter of type 'id'}} [mutArraySet addObject: window]; // expected-error{{parameter of type 'id'}} [block addObject: window]; // expected-error{{parameter of type 'id'}} } // -------------------------------------------------------------------------- // Property accesses. // -------------------------------------------------------------------------- void test_property_read( NSSet *stringSet, NSMutableSet *mutStringSet, WidgetSet *widgetSet, UntypedMutableSet *untypedMutSet, MutableSetOfArrays *mutStringArraySet, NSSet *set, NSMutableSet *mutSet, MutableSetOfArrays *mutArraySet, NSMutableDictionary *mutDict) { int *ip; ip = stringSet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = mutStringSet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = widgetSet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = untypedMutSet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = mutStringArraySet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *> *'}} ip = set.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = mutSet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = mutArraySet.allObjects; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = mutDict.someRandomKey; // expected-error{{incompatible pointer types assigning to 'int *' from '__kindof id'}} } void test_property_write( NSMutableSet *mutStringSet, WidgetSet *widgetSet, UntypedMutableSet *untypedMutSet, MutableSetOfArrays *mutStringArraySet, NSMutableSet *mutSet, MutableSetOfArrays *mutArraySet, NSMutableDictionary *mutDict) { int *ip; mutStringSet.allObjects = ip; // expected-error{{to 'NSArray *'}} widgetSet.allObjects = ip; // expected-error{{to 'NSArray *'}} untypedMutSet.allObjects = ip; // expected-error{{to 'NSArray *'}} mutStringArraySet.allObjects = ip; // expected-error{{to 'NSArray *> *'}} mutSet.allObjects = ip; // expected-error{{to 'NSArray *'}} mutArraySet.allObjects = ip; // expected-error{{to 'NSArray *'}} mutDict.someRandomKey = ip; // expected-error{{to 'id'}} } // -------------------------------------------------------------------------- // Subscripting // -------------------------------------------------------------------------- void test_subscripting( NSArray *stringArray, NSMutableArray *mutStringArray, NSArray *array, NSMutableArray *mutArray, NSDictionary *stringWidgetDict, NSMutableDictionary *mutStringWidgetDict, NSDictionary *dict, NSMutableDictionary *mutDict) { int *ip; NSString *string; Widget *widget; Window *window; ip = stringArray[0]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} ip = mutStringArray[0]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} mutStringArray[0] = ip; // expected-error{{parameter of type 'NSString *'}} ip = array[0]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = mutArray[0]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} mutArray[0] = ip; // expected-error{{parameter of type 'id'}} ip = stringWidgetDict[string]; // expected-error{{incompatible pointer types assigning to 'int *' from 'Widget *'}} widget = stringWidgetDict[widget]; // expected-error{{parameter of type 'NSString *'}} ip = mutStringWidgetDict[string]; // expected-error{{incompatible pointer types assigning to 'int *' from 'Widget *'}} widget = mutStringWidgetDict[widget]; // expected-error{{parameter of type 'NSString *'}} mutStringWidgetDict[string] = ip; // expected-error{{parameter of type 'Widget *'}} mutStringWidgetDict[widget] = widget; // expected-error{{parameter of type 'NSString *'}} ip = dict[string]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} ip = mutDict[string]; // expected-error{{incompatible pointer types assigning to 'int *' from 'id'}} mutDict[string] = ip; // expected-error{{parameter of type 'id'}} widget = mutDict[window]; mutDict[window] = widget; // expected-error{{parameter of type 'id'}} } // -------------------------------------------------------------------------- // Instance variable access. // -------------------------------------------------------------------------- void test_instance_variable(NSArray *stringArray, NSArray *array) { int *ip; ip = stringArray->data; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString **'}} ip = array->data; // expected-error{{incompatible pointer types assigning to 'int *' from 'id *'}} } @implementation WindowArray - (void)testInstanceVariable { int *ip; ip = data; // expected-error{{incompatible pointer types assigning to 'int *' from 'Window **'}} } @end // -------------------------------------------------------------------------- // Implicit conversions. // -------------------------------------------------------------------------- void test_implicit_conversions(NSArray *stringArray, NSArray *numberArray, NSMutableArray *mutStringArray, NSArray *array, NSMutableArray *mutArray) { // Specialized -> unspecialized (same level) array = stringArray; // Unspecialized -> specialized (same level) stringArray = array; // Specialized -> specialized failure (same level). stringArray = numberArray; // expected-error{{incompatible pointer types assigning to 'NSArray *' from 'NSArray *'}} // Specialized -> specialized (different levels). stringArray = mutStringArray; // Specialized -> specialized failure (different levels). numberArray = mutStringArray; // expected-error{{incompatible pointer types assigning to 'NSArray *' from 'NSMutableArray *'}} // Unspecialized -> specialized (different levels). stringArray = mutArray; // Specialized -> unspecialized (different levels). array = mutStringArray; } @interface NSCovariant1<__covariant T> @end @interface NSContravariant1<__contravariant T> @end void test_variance(NSCovariant1 *covariant1, NSCovariant1 *covariant2, NSCovariant1 *covariant3, NSCovariant1 *covariant4, NSCovariant1 *covariant5, NSCovariant1> *covariant6, NSContravariant1 *contravariant1, NSContravariant1 *contravariant2) { covariant1 = covariant2; // okay covariant2 = covariant1; // expected-warning{{incompatible pointer types assigning to 'NSCovariant1 *' from 'NSCovariant1 *'}} covariant3 = covariant4; // okay covariant4 = covariant3; // expected-warning{{incompatible pointer types assigning to 'NSCovariant1 *' from 'NSCovariant1 *'}} covariant5 = covariant1; // okay covariant1 = covariant5; // okay: id is promiscuous covariant5 = covariant3; // okay covariant3 = covariant5; // okay contravariant1 = contravariant2; // expected-warning{{incompatible pointer types assigning to 'NSContravariant1 *' from 'NSContravariant1 *'}} contravariant2 = contravariant1; // okay } // -------------------------------------------------------------------------- // Ternary operator // -------------------------------------------------------------------------- void test_ternary_operator(NSArray *stringArray, NSArray *numberArray, NSMutableArray *mutStringArray, NSStringArray *stringArray2, NSArray *array, NSMutableArray *mutArray, int cond) { int *ip; id object; ip = cond ? stringArray : mutStringArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? mutStringArray : stringArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? stringArray2 : mutStringArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? mutStringArray : stringArray2; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? stringArray : mutArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? stringArray2 : mutArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? mutArray : stringArray; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = cond ? mutArray : stringArray2; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} object = cond ? stringArray : numberArray; // expected-warning{{incompatible operand types ('NSArray *' and 'NSArray *')}} } // -------------------------------------------------------------------------- // super // -------------------------------------------------------------------------- @implementation NSStringArray - (void)useSuperMethod { int *ip; ip = super.lastObject; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} ip = [super objectAtIndexedSubscript:0]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSString *'}} } + (void)useSuperMethod { int *ip; ip = super.array; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} ip = [super array]; // expected-error{{incompatible pointer types assigning to 'int *' from 'NSArray *'}} } @end // -------------------------------------------------------------------------- // Template instantiation // -------------------------------------------------------------------------- template struct NSMutableDictionaryOf { typedef NSMutableDictionary *type; // expected-error{{type argument 'NSObject *' does not satisfy the bound ('id') of type parameter 'K'}} }; template struct VariadicNSMutableDictionaryOf { typedef NSMutableDictionary *type; // expected-error{{type argument 'NSObject *' does not satisfy the bound ('id') of type parameter 'K'}} // expected-error@-1{{too many type arguments for class 'NSMutableDictionary' (have 3, expected 2)}} // expected-error@-2{{too few type arguments for class 'NSMutableDictionary' (have 1, expected 2)}} }; void testInstantiation() { int *ip; typedef NSMutableDictionaryOf::type Dict1; Dict1 d1 = ip; // expected-error{{cannot initialize a variable of type 'Dict1' (aka 'NSMutableDictionary *')}} typedef NSMutableDictionaryOf::type Dict2; // expected-note{{in instantiation of template}} } void testVariadicInstantiation() { int *ip; typedef VariadicNSMutableDictionaryOf::type Dict1; Dict1 d1 = ip; // expected-error{{cannot initialize a variable of type 'Dict1' (aka 'NSMutableDictionary *')}} typedef VariadicNSMutableDictionaryOf::type Dict2; // expected-note{{in instantiation of template}} typedef VariadicNSMutableDictionaryOf::type Dict3; // expected-note{{in instantiation of template}} typedef VariadicNSMutableDictionaryOf::type Dict3; // expected-note{{in instantiation of template}} } // -------------------------------------------------------------------------- // Parameterized classes are not templates // -------------------------------------------------------------------------- template class TT> struct AcceptsTemplateTemplate { }; typedef AcceptsTemplateTemplate TemplateTemplateFail1; // expected-error{{template argument for template template parameter must be a class template or type alias template}} template struct DependentTemplate { typedef typename T::template apply type; // expected-error{{'apply' following the 'template' keyword does not refer to a template}} }; struct NSMutableDictionaryBuilder { typedef NSMutableDictionary apply; // expected-note 2{{declared as a non-template here}} }; typedef DependentTemplate::type DependentTemplateFail1; // expected-note{{in instantiation of template class}} template struct NonDependentTemplate { typedef NSMutableDictionaryBuilder::template apply type; // expected-error{{'apply' following the 'template' keyword does not refer to a template}} }; // However, one can use an alias template to turn a parameterized // class into a template. template using NSMutableDictionaryAlias = NSMutableDictionary; typedef AcceptsTemplateTemplate TemplateTemplateAlias1; // okay