// RUN: %clang_cc1 -fblocks %s -verify #if !__has_feature(objc_generics) # error Compiler does not support Objective-C generics? #endif #if !__has_feature(objc_generics_variance) # error Compiler does not support co- and contr-variance? #endif @protocol NSObject // expected-note{{'NSObject' declared here}} @end @protocol NSCopying // expected-note{{'NSCopying' declared here}} @end __attribute__((objc_root_class)) @interface NSObject // expected-note{{'NSObject' defined here}} @end @interface NSString : NSObject @end // -------------------------------------------------------------------------- // Parsing parameterized classes. // -------------------------------------------------------------------------- // Parse type parameters with a bound @interface PC1 : NSObject // expected-note{{'PC1' declared here}} // expected-note@-1{{type parameter 'T' declared here}} // expected-note@-2{{type parameter 'U' declared here}} // expected-note@-3{{type parameter 'U' declared here}} @end // Parse a type parameter with a bound that terminates in '>>'. @interface PC2> : NSObject @end // Parse multiple type parameters. @interface PC3 : NSObject @end // Parse multiple type parameters--grammatically ambiguous with protocol refs. @interface PC4 : NSObject // expected-note 2{{'PC4' declared here}} @end // Parse a type parameter list without a superclass. @interface PC5 @end // Parse a type parameter with name conflicts. @interface PC6 : NSObject // expected-error{{redeclaration of type parameter 'T'}} @end // Parse Objective-C protocol references. @interface PC7 // expected-error{{cannot find protocol declaration for 'T'}} @end // Parse both type parameters and protocol references. @interface PC8 : NSObject @end // Type parameters with improper bounds. @interface PC9 : NSObject // expected-error{{missing '*' in type bound 'NSString' for type parameter 'U'}} @end // -------------------------------------------------------------------------- // Parsing parameterized forward declarations classes. // -------------------------------------------------------------------------- // Okay: forward declaration without type parameters. @class PC10; // Okay: forward declarations with type parameters. @class PC10, PC11; // expected-note{{type parameter 'T' declared here}} // Okay: forward declaration without type parameters following ones // with type parameters. @class PC10, PC11; // Okay: definition of class with type parameters that was formerly // declared with the same type parameters. @interface PC10 : NSObject @end // Mismatched parameters in declaration of @interface following @class. @interface PC11 : NSObject // expected-error{{missing type bound 'NSObject *' for type parameter 'T' in @interface}} @end @interface PC12 : NSObject // expected-note{{type parameter 'T' declared here}} @end @class PC12; // Mismatched parameters in subsequent forward declarations. @class PC13; // expected-note{{type parameter 'T' declared here}} @class PC13; @class PC13; // expected-error{{missing type bound 'NSObject *' for type parameter 'U' in @class}} // Mismatch parameters in declaration of @class following @interface. @class PC12; // expected-error{{missing type bound 'NSObject *' for type parameter 'T' in @class}} // Parameterized forward declaration a class that is not parameterized. @class NSObject; // expected-error{{forward declaration of non-parameterized class 'NSObject' cannot have type parameters}} // expected-note@-1{{'NSObject' declared here}} // Parameterized forward declaration preceding the definition (that is // not parameterized). @class NSNumber; // expected-note{{'NSNumber' declared here}} @interface NSNumber : NSObject // expected-error{{class 'NSNumber' previously declared with type parameters}} @end @class PC14; // Okay: definition of class with type parameters that was formerly // declared without type parameters. @interface PC14 : NSObject @end // -------------------------------------------------------------------------- // Parsing parameterized categories and extensions. // -------------------------------------------------------------------------- // Inferring type bounds @interface PC1 (Cat1) @end // Matching type bounds @interface PC1 (Cat2) @end // Inferring type bounds @interface PC1 () @end // Matching type bounds @interface PC1 () @end // Missing type parameters. @interface PC1 () // expected-error{{extension has too few type parameters (expected 2, have 1)}} @end // Extra type parameters. @interface PC1 (Cat3) // expected-error{{category has too many type parameters (expected 2, have 3)}} @end // Mismatched bounds. @interface PC1 () // expected-error{{type bound 'id' for type parameter 'X' conflicts with previous bound 'NSObject *'for type parameter 'U'}} @end // Parameterized category/extension of non-parameterized class. @interface NSObject (Cat1) // expected-error{{category of non-parameterized class 'NSObject' cannot have type parameters}} @end @interface NSObject () // expected-error{{extension of non-parameterized class 'NSObject' cannot have type parameters}} @end // -------------------------------------------------------------------------- // @implementations cannot have type parameters // -------------------------------------------------------------------------- @implementation PC1 // expected-error{{@implementation cannot have type parameters}} @end @implementation PC2 // expected-error{{@implementation declaration cannot be protocol qualified}} @end @implementation PC1 (Cat1) // expected-error{{@implementation cannot have type parameters}} @end @implementation PC1 (Cat2) // expected-error{{@implementation cannot have type parameters}} @end typedef T undeclaredT; // expected-error{{unknown type name 'T'}} // -------------------------------------------------------------------------- // Interfaces involving type parameters // -------------------------------------------------------------------------- @interface PC20 : NSObject { T object; } - (U)method:(V)param; @end @interface PC20 (Cat1) - (U)catMethod:(V)param; @end @interface PC20() - (X)extMethod:(Y)param; @end // -------------------------------------------------------------------------- // Parsing type arguments. // -------------------------------------------------------------------------- typedef NSString * ObjCStringRef; // expected-note{{'ObjCStringRef' declared here}} // Type arguments with a mix of identifiers and type-names. typedef PC4 typeArgs1; // Type arguments with only identifiers. typedef PC4 typeArgs2; // Type arguments with only identifiers; one is ambiguous (resolved as // types). typedef PC4 typeArgs3; // expected-error{{type argument 'NSObject' must be a pointer (requires a '*')}} // Type arguments with only identifiers; one is ambiguous (resolved as // protocol qualifiers). typedef PC4 protocolQuals1; // Type arguments and protocol qualifiers. typedef PC4 typeArgsAndProtocolQuals1; // Type arguments and protocol qualifiers in the wrong order. typedef PC4 typeArgsAndProtocolQuals2; // expected-error{{protocol qualifiers must precede type arguments}} // Type arguments and protocol qualifiers (identifiers). typedef PC4 typeArgsAndProtocolQuals3; // expected-error{{type argument 'NSObject' must be a pointer (requires a '*')}} // Typo correction: protocol bias. typedef PC4 protocolQuals2; // expected-error{{cannot find protocol declaration for 'NSObjec'; did you mean 'NSObject'?}} // Typo correction: type bias. typedef PC4 typeArgs4; // expected-error{{unknown class name 'NSObjec'; did you mean 'NSObject'?}} // expected-error@-1{{type argument 'NSObject' must be a pointer (requires a '*')}} // Typo correction: bias set by correction itself to a protocol. typedef PC4 protocolQuals3; // expected-error{{cannot find protocol declaration for 'NSCopyin'; did you mean 'NSCopying'?}} // Typo correction: bias set by correction itself to a type. typedef PC4 typeArgs5; // expected-error{{unknown type name 'ObjCStringref'; did you mean 'ObjCStringRef'?}} // expected-error@-1{{type argument 'NSObject' must be a pointer (requires a '*')}} // expected-error@-2{{type argument 'NSObject' must be a pointer (requires a '*')}} // Type/protocol conflict. typedef PC4 typeArgsProtocolQualsConflict1; // expected-error{{angle brackets contain both a type ('ObjCStringRef') and a protocol ('NSCopying')}} typedef PC4 typeArgsProtocolQualsConflict2; // expected-error{{angle brackets contain both a type ('NSString') and a protocol ('NSCopying')}} typedef PC4 typeArgsProtocolQualsConflict3; // expected-error{{angle brackets contain both a type ('NSString') and a protocol ('NSCopying')}} expected-error{{unknown type name 'UnknownType'}} typedef PC4 typeArgsProtocolQualsConflict4; // expected-error{{unknown type name 'UnknownType'}} typedef PC4 typeArgsProtocolQualsConflict5; // expected-error{{angle brackets contain both a type ('NSString') and a protocol ('NSCopying')}} // Handling the '>>' in type argument lists. typedef PC4, NSObject *, id> typeArgs6; // -------------------------------------------------------------------------- // Checking type arguments. // -------------------------------------------------------------------------- @interface PC15> : NSObject // expected-note@-1{{type parameter 'V' declared here}} // expected-note@-2{{type parameter 'V' declared here}} // expected-note@-3{{type parameter 'U' declared here}} @end typedef PC4 tooFewTypeArgs1; // expected-error{{too few type arguments for class 'PC4' (have 1, expected 3)}} typedef PC4 tooManyTypeArgs1; // expected-error{{too many type arguments for class 'PC4' (have 4, expected 3)}} typedef PC15 typeArgs7; // class that conforms to the protocol typedef PC15> typeArgs8; typedef PC15 typeArgs8b; // expected-error{{type argument 'NSObject *' does not satisfy the bound ('id') of type parameter 'V'}} typedef PC15 typeArgs9; typedef PC15 typeArgs10; // expected-error{{type argument 'id' does not satisfy the bound ('id') of type parameter 'V'}} typedef PC15> typeArgs11; typedef PC15 typeArgs12; // okay typedef NSObject typeArgs13; // expected-error{{type arguments cannot be applied to non-parameterized class 'NSObject'}} typedef id typeArgs14; // expected-error{{type arguments cannot be applied to non-class type 'id'}} typedef PC1 typeArgs15; typedef PC1 typeArgsAndProtocolQuals4; typedef typeArgs15 typeArgsAndProtocolQuals5; typedef typeArgs15 typeArgs16; // expected-error{{type arguments cannot be applied to already-specialized class type 'typeArgs15' (aka 'PC1')}} typedef typeArgs15 typeArgsAndProtocolQuals6; void testSpecializedTypePrinting() { int *ip; ip = (typeArgs15*)0; // expected-warning{{'typeArgs15 *' (aka 'PC1 *')}} ip = (typeArgsAndProtocolQuals4*)0; // expected-warning{{'typeArgsAndProtocolQuals4 *' (aka 'PC1 *')}} ip = (typeArgsAndProtocolQuals5*)0; // expected-warning{{'typeArgsAndProtocolQuals5 *' (aka 'typeArgs15 *')}} ip = (typeArgsAndProtocolQuals6)0; // expected-error{{used type 'typeArgsAndProtocolQuals6' (aka 'typeArgs15')}} ip = (typeArgsAndProtocolQuals6*)0;// expected-warning{{'typeArgsAndProtocolQuals6 *' (aka 'typeArgs15 *')}} } // -------------------------------------------------------------------------- // Specialized superclasses // -------------------------------------------------------------------------- @interface PC21 : PC1 @end @interface PC22 : PC1 // expected-error{{too few type arguments for class 'PC1' (have 1, expected 2)}} @end @interface PC23 : PC1 // expected-error{{unknown type name 'U'}} @end @interface PC24 : PC1 // expected-error{{type argument 'T' (aka 'id') does not satisfy the bound ('NSObject *') of type parameter 'U'}} @end @interface NSFoo : PC1 // okay @end // -------------------------------------------------------------------------- // Co- and contra-variance. // -------------------------------------------------------------------------- @class Variance1; @class Variance1<__covariant T, __contravariant U>; @interface Variance1<__covariant T, __contravariant U> : NSObject // expected-note 2{{declared here}} @end @interface Variance1 () // okay, inferred @end @interface Variance1 (Cat1) // okay, inferred @end @class Variance1; // okay, inferred @interface Variance1<__covariant T, __contravariant U> () // okay, matches @end @interface Variance1<__covariant T, __contravariant U> (Cat2) // okay, matches @end @class Variance1<__covariant T, __contravariant U>; // okay, matches @interface Variance1<__contravariant X, // expected-error{{contravariant type parameter 'X' conflicts with previous covariant type parameter 'T'}} __covariant Y> () // expected-error{{covariant type parameter 'Y' conflicts with previous contravariant type parameter 'U'}} @end @class Variance2<__covariant T, __contravariant U>; // expected-note 2{{declared here}} @interface Variance2<__contravariant T, // expected-error{{contravariant type parameter 'T' conflicts with previous covariant type parameter 'T'}} U> : NSObject // expected-error{{invariant type parameter 'U' conflicts with previous contravariant type parameter 'U'}} @end