diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -1448,6 +1448,8 @@ QualType getObjCTypeParamType(const ObjCTypeParamDecl *Decl, ArrayRef protocols) const; + void adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig, + ObjCTypeParamDecl *New) const; bool ObjCObjectAdoptsQTypeProtocols(QualType QT, ObjCInterfaceDecl *Decl); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -4808,6 +4808,17 @@ return QualType(newType, 0); } +void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig, + ObjCTypeParamDecl *New) const { + New->setTypeSourceInfo(getTrivialTypeSourceInfo(Orig->getUnderlyingType())); + // Update TypeForDecl after updating TypeSourceInfo. + auto NewTypeParamTy = cast(New->getTypeForDecl()); + SmallVector protocols; + protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end()); + QualType UpdatedTy = getObjCTypeParamType(New, protocols); + New->setTypeForDecl(UpdatedTy.getTypePtr()); +} + /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's /// protocol list adopt all protocols in QT's qualified-id protocol /// list. diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -3537,6 +3537,7 @@ const ObjCTypeParamDecl *OTPDecl, ArrayRef protocols) { ID.AddPointer(OTPDecl); + ID.AddPointer(OTPDecl->getUnderlyingType().getAsOpaquePtr()); ID.AddInteger(protocols.size()); for (auto proto : protocols) ID.AddPointer(proto); diff --git a/clang/lib/Sema/SemaDeclObjC.cpp b/clang/lib/Sema/SemaDeclObjC.cpp --- a/clang/lib/Sema/SemaDeclObjC.cpp +++ b/clang/lib/Sema/SemaDeclObjC.cpp @@ -937,8 +937,7 @@ // Override the new type parameter's bound type with the previous type, // so that it's consistent. - newTypeParam->setTypeSourceInfo( - S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); + S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam); continue; } @@ -965,8 +964,7 @@ } // Update the new type parameter's bound to match the previous one. - newTypeParam->setTypeSourceInfo( - S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); + S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam); } return false; diff --git a/clang/test/SemaObjC/parameterized_classes_collection_literal.m b/clang/test/SemaObjC/parameterized_classes_collection_literal.m --- a/clang/test/SemaObjC/parameterized_classes_collection_literal.m +++ b/clang/test/SemaObjC/parameterized_classes_collection_literal.m @@ -29,7 +29,9 @@ @end @interface NSDictionary : NSObject -+ (instancetype)dictionaryWithObjects:(const V [])objects forKeys:(const K [])keys count:(NSUInteger)cnt; ++ (instancetype)dictionaryWithObjects:(const V [])objects + forKeys:(const K [])keys + count:(NSUInteger)cnt; @end void testArrayLiteral(void) { @@ -50,3 +52,9 @@ @"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", + }; +} diff --git a/clang/test/SemaObjC/parameterized_classes_subst.m b/clang/test/SemaObjC/parameterized_classes_subst.m --- a/clang/test/SemaObjC/parameterized_classes_subst.m +++ b/clang/test/SemaObjC/parameterized_classes_subst.m @@ -467,3 +467,17 @@ - (void)mapUsingBlock2:(id)block { // expected-warning{{conflicting parameter types in implementation}} } @end + +// -------------------------------------------------------------------------- +// Use a type parameter as a type argument. +// -------------------------------------------------------------------------- +// Type bounds in a category/extension are omitted. rdar://problem/54329242 +@interface ParameterizedContainer> +- (ParameterizedContainer *)inInterface; +@end +@interface ParameterizedContainer (Cat) +- (ParameterizedContainer *)inCategory; +@end +@interface ParameterizedContainer () +- (ParameterizedContainer *)inExtension; +@end