Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fblocks %s -verify |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 3 | @protocol NSObject // expected-note{{'NSObject' declared here}} |
| 4 | @end |
| 5 | |
| 6 | @protocol NSCopying // expected-note{{'NSCopying' declared here}} |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7 | @end |
| 8 | |
| 9 | __attribute__((objc_root_class)) |
| 10 | @interface NSObject <NSObject> // expected-note{{'NSObject' defined here}} |
| 11 | @end |
| 12 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 13 | @interface NSString : NSObject <NSCopying> |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 14 | @end |
| 15 | |
| 16 | // -------------------------------------------------------------------------- |
| 17 | // Parsing parameterized classes. |
| 18 | // -------------------------------------------------------------------------- |
| 19 | |
| 20 | // Parse type parameters with a bound |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 21 | @interface PC1<T, U : NSObject*> : NSObject // expected-note{{'PC1' declared here}} |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 22 | // expected-note@-1{{type parameter 'T' declared here}} |
| 23 | // expected-note@-2{{type parameter 'U' declared here}} |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 24 | // expected-note@-3{{type parameter 'U' declared here}} |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 25 | @end |
| 26 | |
| 27 | // Parse a type parameter with a bound that terminates in '>>'. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 28 | @interface PC2<T : id<NSObject>> : NSObject |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 29 | @end |
| 30 | |
| 31 | // Parse multiple type parameters. |
| 32 | @interface PC3<T, U : id> : NSObject |
| 33 | @end |
| 34 | |
| 35 | // Parse multiple type parameters--grammatically ambiguous with protocol refs. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 36 | @interface PC4<T, U, V> : NSObject // expected-note 2{{'PC4' declared here}} |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 37 | @end |
| 38 | |
| 39 | // Parse a type parameter list without a superclass. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 40 | @interface PC5<T : id> |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 41 | @end |
| 42 | |
| 43 | // Parse a type parameter with name conflicts. |
| 44 | @interface PC6<T, U, |
| 45 | T> : NSObject // expected-error{{redeclaration of type parameter 'T'}} |
| 46 | @end |
| 47 | |
| 48 | // Parse Objective-C protocol references. |
| 49 | @interface PC7<T> // expected-error{{cannot find protocol declaration for 'T'}} |
| 50 | @end |
| 51 | |
| 52 | // Parse both type parameters and protocol references. |
| 53 | @interface PC8<T> : NSObject <NSObject> |
| 54 | @end |
| 55 | |
| 56 | // Type parameters with improper bounds. |
| 57 | @interface PC9<T : int, // expected-error{{type bound 'int' for type parameter 'T' is not an Objective-C pointer type}} |
| 58 | U : NSString> : NSObject // expected-error{{missing '*' in type bound 'NSString' for type parameter 'U'}} |
| 59 | @end |
| 60 | |
| 61 | // -------------------------------------------------------------------------- |
| 62 | // Parsing parameterized forward declarations classes. |
| 63 | // -------------------------------------------------------------------------- |
| 64 | |
| 65 | // Okay: forward declaration without type parameters. |
| 66 | @class PC10; |
| 67 | |
| 68 | // Okay: forward declarations with type parameters. |
| 69 | @class PC10<T, U : NSObject *>, PC11<T : NSObject *, U : id>; // expected-note{{type parameter 'T' declared here}} |
| 70 | |
| 71 | // Okay: forward declaration without type parameters following ones |
| 72 | // with type parameters. |
| 73 | @class PC10, PC11; |
| 74 | |
| 75 | // Okay: definition of class with type parameters that was formerly |
| 76 | // declared with the same type parameters. |
| 77 | @interface PC10<T, U : NSObject *> : NSObject |
| 78 | @end |
| 79 | |
| 80 | // Mismatched parameters in declaration of @interface following @class. |
| 81 | @interface PC11<T, U> : NSObject // expected-error{{missing type bound 'NSObject *' for type parameter 'T' in @interface}} |
| 82 | @end |
| 83 | |
| 84 | @interface PC12<T : NSObject *> : NSObject // expected-note{{type parameter 'T' declared here}} |
| 85 | @end |
| 86 | |
| 87 | @class PC12; |
| 88 | |
| 89 | // Mismatched parameters in subsequent forward declarations. |
| 90 | @class PC13<T : NSObject *>; // expected-note{{type parameter 'T' declared here}} |
| 91 | @class PC13; |
| 92 | @class PC13<U>; // expected-error{{missing type bound 'NSObject *' for type parameter 'U' in @class}} |
| 93 | |
| 94 | // Mismatch parameters in declaration of @class following @interface. |
| 95 | @class PC12<T>; // expected-error{{missing type bound 'NSObject *' for type parameter 'T' in @class}} |
| 96 | |
| 97 | // Parameterized forward declaration a class that is not parameterized. |
| 98 | @class NSObject<T>; // expected-error{{forward declaration of non-parameterized class 'NSObject' cannot have type parameters}} |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 99 | // expected-note@-1{{'NSObject' declared here}} |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 100 | |
| 101 | // Parameterized forward declaration preceding the definition (that is |
| 102 | // not parameterized). |
| 103 | @class NSNumber<T : NSObject *>; // expected-note{{'NSNumber' declared here}} |
| 104 | @interface NSNumber : NSObject // expected-error{{class 'NSNumber' previously declared with type parameters}} |
| 105 | @end |
| 106 | |
| 107 | @class PC14; |
| 108 | |
| 109 | // Okay: definition of class with type parameters that was formerly |
| 110 | // declared without type parameters. |
| 111 | @interface PC14<T, U : NSObject *> : NSObject |
| 112 | @end |
| 113 | |
| 114 | // -------------------------------------------------------------------------- |
| 115 | // Parsing parameterized categories and extensions. |
| 116 | // -------------------------------------------------------------------------- |
| 117 | |
| 118 | // Inferring type bounds |
| 119 | @interface PC1<T, U> (Cat1) <NSObject> |
| 120 | @end |
| 121 | |
| 122 | // Matching type bounds |
| 123 | @interface PC1<T : id, U : NSObject *> (Cat2) <NSObject> |
| 124 | @end |
| 125 | |
| 126 | // Inferring type bounds |
| 127 | @interface PC1<T, U> () <NSObject> |
| 128 | @end |
| 129 | |
| 130 | // Matching type bounds |
| 131 | @interface PC1<T : id, U : NSObject *> () <NSObject> |
| 132 | @end |
| 133 | |
| 134 | // Missing type parameters. |
| 135 | @interface PC1<T> () // expected-error{{extension has too few type parameters (expected 2, have 1)}} |
| 136 | @end |
| 137 | |
| 138 | // Extra type parameters. |
| 139 | @interface PC1<T, U, V> (Cat3) // expected-error{{category has too many type parameters (expected 2, have 3)}} |
| 140 | @end |
| 141 | |
| 142 | // Mismatched bounds. |
| 143 | @interface PC1<T : NSObject *, // expected-error{{type bound 'NSObject *' for type parameter 'T' conflicts with implicit bound 'id'}} |
| 144 | X : id> () // expected-error{{type bound 'id' for type parameter 'X' conflicts with previous bound 'NSObject *'for type parameter 'U'}} |
| 145 | @end |
| 146 | |
| 147 | // Parameterized category/extension of non-parameterized class. |
| 148 | @interface NSObject<T> (Cat1) // expected-error{{category of non-parameterized class 'NSObject' cannot have type parameters}} |
| 149 | @end |
| 150 | |
| 151 | @interface NSObject<T> () // expected-error{{extension of non-parameterized class 'NSObject' cannot have type parameters}} |
| 152 | @end |
| 153 | |
| 154 | // -------------------------------------------------------------------------- |
| 155 | // @implementations cannot have type parameters |
| 156 | // -------------------------------------------------------------------------- |
| 157 | @implementation PC1<T : id> // expected-error{{@implementation cannot have type parameters}} |
| 158 | @end |
| 159 | |
| 160 | @implementation PC2<T> // expected-error{{@implementation declaration cannot be protocol qualified}} |
| 161 | @end |
| 162 | |
| 163 | @implementation PC1<T> (Cat1) // expected-error{{@implementation cannot have type parameters}} |
| 164 | @end |
| 165 | |
| 166 | @implementation PC1<T : id> (Cat2) // expected-error{{@implementation cannot have type parameters}} |
| 167 | @end |
| 168 | |
| 169 | // -------------------------------------------------------------------------- |
| 170 | // Interfaces involving type parameters |
| 171 | // -------------------------------------------------------------------------- |
| 172 | @interface PC20<T : id, U : NSObject *, V : NSString *> : NSObject { |
| 173 | T object; |
| 174 | } |
| 175 | |
| 176 | - (U)method:(V)param; // expected-note{{passing argument to parameter 'param' here}} |
| 177 | @end |
| 178 | |
| 179 | @interface PC20<T, U, V> (Cat1) |
| 180 | - (U)catMethod:(V)param; // expected-note{{passing argument to parameter 'param' here}} |
| 181 | @end |
| 182 | |
| 183 | @interface PC20<X, Y, Z>() |
| 184 | - (X)extMethod:(Y)param; // expected-note{{passing argument to parameter 'param' here}} |
| 185 | @end |
| 186 | |
| 187 | void test_PC20_unspecialized(PC20 *pc20) { |
| 188 | // FIXME: replace type parameters with underlying types? |
| 189 | int *ip = [pc20 method: 0]; // expected-warning{{incompatible pointer types initializing 'int *' with an expression of type 'U' (aka 'NSObject *')}} |
| 190 | [pc20 method: ip]; // expected-warning{{incompatible pointer types sending 'int *' to parameter of type 'V' (aka 'NSString *')}} |
| 191 | |
| 192 | ip = [pc20 catMethod: 0]; // expected-warning{{incompatible pointer types assigning to 'int *' from 'U' (aka 'NSObject *')}} |
| 193 | [pc20 catMethod: ip]; // expected-warning{{incompatible pointer types sending 'int *' to parameter of type 'V' (aka 'NSString *')}} |
| 194 | |
| 195 | ip = [pc20 extMethod: 0]; // expected-warning{{incompatible pointer types assigning to 'int *' from 'X' (aka 'id')}} |
| 196 | [pc20 extMethod: ip]; // expected-warning{{incompatible pointer types sending 'int *' to parameter of type 'Y' (aka 'NSObject *')}} |
| 197 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame^] | 198 | |
| 199 | // -------------------------------------------------------------------------- |
| 200 | // Parsing type arguments. |
| 201 | // -------------------------------------------------------------------------- |
| 202 | |
| 203 | typedef NSString * ObjCStringRef; // expected-note{{'ObjCStringRef' declared here}} |
| 204 | |
| 205 | // Type arguments with a mix of identifiers and type-names. |
| 206 | typedef PC4<id, NSObject *, NSString *> typeArgs1; |
| 207 | |
| 208 | // Type arguments with only identifiers. |
| 209 | typedef PC4<id, id, id> typeArgs2; |
| 210 | |
| 211 | // Type arguments with only identifiers; one is ambiguous (resolved as |
| 212 | // types). |
| 213 | typedef PC4<NSObject, id, id> typeArgs3; // expected-error{{type argument 'NSObject' must be a pointer (requires a '*')}} |
| 214 | |
| 215 | // Type arguments with only identifiers; one is ambiguous (resolved as |
| 216 | // protocol qualifiers). |
| 217 | typedef PC4<NSObject, NSCopying> protocolQuals1; |
| 218 | |
| 219 | // Type arguments and protocol qualifiers. |
| 220 | typedef PC4<id, NSObject *, id><NSObject, NSCopying> typeArgsAndProtocolQuals1; |
| 221 | |
| 222 | // Type arguments and protocol qualifiers in the wrong order. |
| 223 | typedef PC4<NSObject, NSCopying><id, NSObject *, id> typeArgsAndProtocolQuals2; // expected-error{{protocol qualifiers must precede type arguments}} |
| 224 | |
| 225 | // Type arguments and protocol qualifiers (identifiers). |
| 226 | typedef PC4<id, NSObject, id><NSObject, NSCopying> typeArgsAndProtocolQuals3; // expected-error{{type argument 'NSObject' must be a pointer (requires a '*')}} |
| 227 | |
| 228 | // Typo correction: protocol bias. |
| 229 | typedef PC4<NSCopying, NSObjec> protocolQuals2; // expected-error{{cannot find protocol declaration for 'NSObjec'; did you mean 'NSObject'?}} |
| 230 | |
| 231 | // Typo correction: type bias. |
| 232 | typedef PC4<id, id, NSObjec> typeArgs4; // expected-error{{unknown class name 'NSObjec'; did you mean 'NSObject'?}} |
| 233 | // expected-error@-1{{type argument 'NSObject' must be a pointer (requires a '*')}} |
| 234 | |
| 235 | // Typo correction: bias set by correction itself to a protocol. |
| 236 | typedef PC4<NSObject, NSObject, NSCopyin> protocolQuals3; // expected-error{{cannot find protocol declaration for 'NSCopyin'; did you mean 'NSCopying'?}} |
| 237 | |
| 238 | // Typo correction: bias set by correction itself to a type. |
| 239 | typedef PC4<NSObject, NSObject, ObjCStringref> typeArgs5; // expected-error{{unknown type name 'ObjCStringref'; did you mean 'ObjCStringRef'?}} |
| 240 | // expected-error@-1{{type argument 'NSObject' must be a pointer (requires a '*')}} |
| 241 | // expected-error@-2{{type argument 'NSObject' must be a pointer (requires a '*')}} |
| 242 | |
| 243 | // Type/protocol conflict. |
| 244 | typedef PC4<NSCopying, ObjCStringRef> typeArgsProtocolQualsConflict1; // expected-error{{angle brackets contain both a type ('ObjCStringRef') and a protocol ('NSCopying')}} |
| 245 | |
| 246 | // Handling the '>>' in type argument lists. |
| 247 | typedef PC4<id<NSCopying>, NSObject *, id<NSObject>> typeArgs6; |
| 248 | |
| 249 | // -------------------------------------------------------------------------- |
| 250 | // Checking type arguments. |
| 251 | // -------------------------------------------------------------------------- |
| 252 | |
| 253 | @interface PC15<T : id, U : NSObject *, V : id<NSCopying>> : NSObject |
| 254 | // expected-note@-1{{type parameter 'V' declared here}} |
| 255 | // expected-note@-2{{type parameter 'V' declared here}} |
| 256 | // expected-note@-3{{type parameter 'U' declared here}} |
| 257 | @end |
| 258 | |
| 259 | typedef PC4<NSString *> tooFewTypeArgs1; // expected-error{{too few type arguments for class 'PC4' (have 1, expected 3)}} |
| 260 | |
| 261 | typedef PC4<NSString *, NSString *, NSString *, NSString *> tooManyTypeArgs1; // expected-error{{too many type arguments for class 'PC4' (have 4, expected 3)}} |
| 262 | |
| 263 | typedef PC15<int (^)(int, int), // block pointers as 'id' |
| 264 | NSString *, // subclass |
| 265 | NSString *> typeArgs7; // class that conforms to the protocol |
| 266 | |
| 267 | typedef PC15<NSObject *, NSObject *, id<NSCopying>> typeArgs8; |
| 268 | |
| 269 | typedef PC15<NSObject *, NSObject *, |
| 270 | NSObject *> typeArgs8b; // expected-error{{type argument 'NSObject *' does not satisy the bound ('id<NSCopying>') of type parameter 'V'}} |
| 271 | |
| 272 | typedef PC15<id, |
| 273 | id, // expected-error{{type argument 'id' does not satisy the bound ('NSObject *') of type parameter 'U'}} |
| 274 | id> typeArgs9; |
| 275 | |
| 276 | typedef PC15<id, NSObject *, |
| 277 | id> typeArgs10; // expected-error{{type argument 'id' does not satisy the bound ('id<NSCopying>') of type parameter 'V'}} |
| 278 | |
| 279 | typedef PC15<id, |
| 280 | int (^)(int, int), // okay |
| 281 | id<NSCopying, NSObject>> typeArgs11; |
| 282 | |
| 283 | typedef PC15<id, NSString *, int (^)(int, int)> typeArgs12; // okay |
| 284 | |
| 285 | typedef NSObject<id, id> typeArgs13; // expected-error{{type arguments cannot be applied to non-parameterized class 'NSObject'}} |
| 286 | |
| 287 | typedef id<id, id> typeArgs14; // expected-error{{type arguments cannot be applied to non-class type 'id'}} |
| 288 | |
| 289 | typedef PC1<NSObject *, NSString *> typeArgs15; |
| 290 | |
| 291 | typedef PC1<NSObject *, NSString *><NSCopying> typeArgsAndProtocolQuals4; |
| 292 | |
| 293 | typedef typeArgs15<NSCopying> typeArgsAndProtocolQuals5; |
| 294 | |
| 295 | typedef typeArgs15<NSObject *, NSString *> typeArgs16; // expected-error{{type arguments cannot be applied to already-specialized class type 'typeArgs15' (aka 'PC1<NSObject *,NSString *>')}} |
| 296 | |
| 297 | typedef typeArgs15<NSObject> typeArgsAndProtocolQuals6; |
| 298 | |
| 299 | void testSpecializedTypePrinting() { |
| 300 | int *ip; |
| 301 | |
| 302 | ip = (typeArgs15*)0; // expected-warning{{'typeArgs15 *' (aka 'PC1<NSObject *,NSString *> *')}} |
| 303 | ip = (typeArgsAndProtocolQuals4*)0; // expected-warning{{'typeArgsAndProtocolQuals4 *' (aka 'PC1<NSObject *,NSString *><NSCopying> *')}} |
| 304 | ip = (typeArgsAndProtocolQuals5*)0; // expected-warning{{'typeArgsAndProtocolQuals5 *' (aka 'typeArgs15<NSCopying> *')}} |
| 305 | ip = (typeArgsAndProtocolQuals6)0; // expected-error{{used type 'typeArgsAndProtocolQuals6' (aka 'typeArgs15<NSObject>')}} |
| 306 | ip = (typeArgsAndProtocolQuals6*)0;// expected-warning{{'typeArgsAndProtocolQuals6 *' (aka 'typeArgs15<NSObject> *')}} |
| 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------------- |
| 310 | // Specialized superclasses |
| 311 | // -------------------------------------------------------------------------- |
| 312 | @interface PC21<T : NSObject *> : PC1<T, T> |
| 313 | @end |
| 314 | |
| 315 | @interface PC22<T : NSObject *> : PC1<T> // expected-error{{too few type arguments for class 'PC1' (have 1, expected 2)}} |
| 316 | @end |
| 317 | |
| 318 | @interface PC23<T : NSObject *> : PC1<T, U> // expected-error{{unknown type name 'U'}} |
| 319 | @end |
| 320 | |
| 321 | @interface PC24<T> : PC1<T, T> // expected-error{{type argument 'T' (aka 'id') does not satisy the bound ('NSObject *') of type parameter 'U'}} |
| 322 | @end |
| 323 | |
| 324 | @interface NSFoo : PC1<NSObject *, NSObject *> // okay |
| 325 | @end |