When a category/extension doesn't repeat a type bound, corresponding
type parameter is substituted with `id` when used as a type argument. As
a result, in the added test case it was causing errors like
> type argument 'T' (aka 'id') does not satisfy the bound ('id<NSCopying>') of type parameter 'T'
We are already checking that type parameters should be consistent
everywhere (see `checkTypeParamListConsistency`) and update
`ObjCTypeParamDecl` to have correct underlying type. And when we use the
type parameter as a method return type or a method parameter type, it is
substituted to the bounded type. But when we use the type parameter as a
type argument, we check `ObjCTypeParamType` that wasn't updated and
remains `id`.
Fix by updating not only `ObjCTypeParamDecl` UnderlyingType but also
TypeForDecl as we use the underlying type to create a canonical type for
`ObjCTypeParamType` (see `ASTContext::getObjCTypeParamType`).
This is a different approach to fixing the issue. The previous one was
02c2ab3d88 which was reverted in
4c539e8da1. The problem with the previous
approach was that `ObjCTypeParamType::desugar` was returning underlying
type for `ObjCTypeParamDecl` without applying any protocols stored in
`ObjCTypeParamType`. It caused inconsistencies in comparing types before
and after desugaring.
Re-applying after fixing intermittent test failures.
rdar://problem/54329242
Reviewed By: erik.pilkington
Differential Revision: https://reviews.llvm.org/D72872
61 lines
2.1 KiB
Objective-C
61 lines
2.1 KiB
Objective-C
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
|
// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify %s
|
|
|
|
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
|
|
typedef unsigned long NSUInteger;
|
|
#else
|
|
typedef unsigned int NSUInteger;
|
|
#endif
|
|
|
|
@protocol NSObject
|
|
@end
|
|
|
|
@protocol NSCopying
|
|
@end
|
|
|
|
__attribute__((objc_root_class))
|
|
@interface NSObject <NSObject>
|
|
@end
|
|
|
|
@interface NSString : NSObject <NSCopying>
|
|
@end
|
|
|
|
@interface NSNumber : NSObject <NSCopying>
|
|
+ (NSNumber *)numberWithInt:(int)value;
|
|
@end
|
|
|
|
@interface NSArray<T> : NSObject <NSCopying>
|
|
+ (instancetype)arrayWithObjects:(const T [])objects count:(NSUInteger)cnt;
|
|
@end
|
|
|
|
@interface NSDictionary<K, V> : NSObject <NSCopying>
|
|
+ (instancetype)dictionaryWithObjects:(const V [])objects
|
|
forKeys:(const K <NSCopying> [])keys
|
|
count:(NSUInteger)cnt;
|
|
@end
|
|
|
|
void testArrayLiteral(void) {
|
|
NSArray<NSString *> *array1 = @[@"hello",
|
|
@1, // expected-warning{{of type 'NSNumber *' is not compatible with array element type 'NSString *'}}
|
|
@"world",
|
|
@[@1, @2]]; // expected-warning{{of type 'NSArray *' is not compatible with array element type 'NSString *'}}
|
|
|
|
NSArray<NSArray<NSString *> *> *array2 = @[@[@"hello", @"world"],
|
|
@"blah", // expected-warning{{object of type 'NSString *' is not compatible with array element type 'NSArray<NSString *> *'}}
|
|
@[@1]]; // expected-warning{{object of type 'NSNumber *' is not compatible with array element type 'NSString *'}}
|
|
}
|
|
|
|
void testDictionaryLiteral(void) {
|
|
NSDictionary<NSString *, NSNumber *> *dict1 = @{
|
|
@"hello" : @17,
|
|
@18 : @18, // expected-warning{{object of type 'NSNumber *' is not compatible with dictionary key type 'NSString *'}}
|
|
@"world" : @"blah" // expected-warning{{object of type 'NSString *' is not compatible with dictionary value type 'NSNumber *'}}
|
|
};
|
|
}
|
|
|
|
void testCastingInDictionaryLiteral(NSString *arg) {
|
|
NSDictionary *dict = @{
|
|
(id)arg : @"foo",
|
|
};
|
|
}
|