Skip to content

Commit a8c8b62

Browse files
committed
[ObjC generics] Fix not inheriting type bounds in categories/extensions.
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 02c2ab3 which was reverted in 4c539e8. 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. rdar://problem/54329242 Reviewed By: erik.pilkington Differential Revision: https://reviews.llvm.org/D72872
1 parent ba8b305 commit a8c8b62

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
14421442

14431443
QualType getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
14441444
ArrayRef<ObjCProtocolDecl *> protocols) const;
1445+
void adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig,
1446+
ObjCTypeParamDecl *New) const;
14451447

14461448
bool ObjCObjectAdoptsQTypeProtocols(QualType QT, ObjCInterfaceDecl *Decl);
14471449

clang/lib/AST/ASTContext.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,6 +4874,17 @@ ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
48744874
return QualType(newType, 0);
48754875
}
48764876

4877+
void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig,
4878+
ObjCTypeParamDecl *New) const {
4879+
New->setTypeSourceInfo(getTrivialTypeSourceInfo(Orig->getUnderlyingType()));
4880+
// Update TypeForDecl after updating TypeSourceInfo.
4881+
auto NewTypeParamTy = cast<ObjCTypeParamType>(New->getTypeForDecl());
4882+
SmallVector<ObjCProtocolDecl *, 8> protocols;
4883+
protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end());
4884+
QualType UpdatedTy = getObjCTypeParamType(New, protocols);
4885+
New->setTypeForDecl(UpdatedTy.getTypePtr());
4886+
}
4887+
48774888
/// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's
48784889
/// protocol list adopt all protocols in QT's qualified-id protocol
48794890
/// list.

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,7 @@ void ObjCTypeParamType::Profile(llvm::FoldingSetNodeID &ID,
35343534
const ObjCTypeParamDecl *OTPDecl,
35353535
ArrayRef<ObjCProtocolDecl *> protocols) {
35363536
ID.AddPointer(OTPDecl);
3537+
ID.AddPointer(OTPDecl->getUnderlyingType().getAsOpaquePtr());
35373538
ID.AddInteger(protocols.size());
35383539
for (auto proto : protocols)
35393540
ID.AddPointer(proto);

clang/lib/Sema/SemaDeclObjC.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,7 @@ static bool checkTypeParamListConsistency(Sema &S,
938938

939939
// Override the new type parameter's bound type with the previous type,
940940
// so that it's consistent.
941-
newTypeParam->setTypeSourceInfo(
942-
S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType()));
941+
S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam);
943942
continue;
944943
}
945944

@@ -966,8 +965,7 @@ static bool checkTypeParamListConsistency(Sema &S,
966965
}
967966

968967
// Update the new type parameter's bound to match the previous one.
969-
newTypeParam->setTypeSourceInfo(
970-
S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType()));
968+
S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam);
971969
}
972970

973971
return false;

clang/test/SemaObjC/parameterized_classes_collection_literal.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ + (instancetype)arrayWithObjects:(const T [])objects count:(NSUInteger)cnt;
2929
@end
3030

3131
@interface NSDictionary<K, V> : NSObject <NSCopying>
32-
+ (instancetype)dictionaryWithObjects:(const V [])objects forKeys:(const K [])keys count:(NSUInteger)cnt;
32+
+ (instancetype)dictionaryWithObjects:(const V [])objects
33+
forKeys:(const K <NSCopying> [])keys
34+
count:(NSUInteger)cnt;
3335
@end
3436

3537
void testArrayLiteral(void) {
@@ -50,3 +52,9 @@ void testDictionaryLiteral(void) {
5052
@"world" : @"blah" // expected-warning{{object of type 'NSString *' is not compatible with dictionary value type 'NSNumber *'}}
5153
};
5254
}
55+
56+
void testCastingInDictionaryLiteral(NSString *arg) {
57+
NSDictionary *dict = @{
58+
(id)arg : @"foo",
59+
};
60+
}

clang/test/SemaObjC/parameterized_classes_subst.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,17 @@ - (void)mapUsingBlock:(id (^)(id))block {
467467
- (void)mapUsingBlock2:(id)block { // expected-warning{{conflicting parameter types in implementation}}
468468
}
469469
@end
470+
471+
// --------------------------------------------------------------------------
472+
// Use a type parameter as a type argument.
473+
// --------------------------------------------------------------------------
474+
// Type bounds in a category/extension are omitted. rdar://problem/54329242
475+
@interface ParameterizedContainer<T : id<NSCopying>>
476+
- (ParameterizedContainer<T> *)inInterface;
477+
@end
478+
@interface ParameterizedContainer<T> (Cat)
479+
- (ParameterizedContainer<T> *)inCategory;
480+
@end
481+
@interface ParameterizedContainer<U> ()
482+
- (ParameterizedContainer<U> *)inExtension;
483+
@end

0 commit comments

Comments
 (0)