Skip to content

Commit 65f5887

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. Re-applying after fixing intermittent test failures. rdar://problem/54329242 Reviewed By: erik.pilkington Differential Revision: https://reviews.llvm.org/D72872
1 parent 57a7cd7 commit 65f5887

File tree

7 files changed

+43
-7
lines changed

7 files changed

+43
-7
lines changed

clang/include/clang/AST/ASTContext.h

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

14601460
QualType getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
14611461
ArrayRef<ObjCProtocolDecl *> protocols) const;
1462+
void adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig,
1463+
ObjCTypeParamDecl *New) const;
14621464

14631465
bool ObjCObjectAdoptsQTypeProtocols(QualType QT, ObjCInterfaceDecl *Decl);
14641466

clang/include/clang/AST/Type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5605,6 +5605,7 @@ class ObjCTypeParamType : public Type,
56055605
void Profile(llvm::FoldingSetNodeID &ID);
56065606
static void Profile(llvm::FoldingSetNodeID &ID,
56075607
const ObjCTypeParamDecl *OTPDecl,
5608+
QualType CanonicalType,
56085609
ArrayRef<ObjCProtocolDecl *> protocols);
56095610

56105611
ObjCTypeParamDecl *getDecl() const { return OTPDecl; }

clang/lib/AST/ASTContext.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4898,7 +4898,7 @@ ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
48984898
ArrayRef<ObjCProtocolDecl *> protocols) const {
48994899
// Look in the folding set for an existing type.
49004900
llvm::FoldingSetNodeID ID;
4901-
ObjCTypeParamType::Profile(ID, Decl, protocols);
4901+
ObjCTypeParamType::Profile(ID, Decl, Decl->getUnderlyingType(), protocols);
49024902
void *InsertPos = nullptr;
49034903
if (ObjCTypeParamType *TypeParam =
49044904
ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos))
@@ -4924,6 +4924,17 @@ ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
49244924
return QualType(newType, 0);
49254925
}
49264926

4927+
void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig,
4928+
ObjCTypeParamDecl *New) const {
4929+
New->setTypeSourceInfo(getTrivialTypeSourceInfo(Orig->getUnderlyingType()));
4930+
// Update TypeForDecl after updating TypeSourceInfo.
4931+
auto NewTypeParamTy = cast<ObjCTypeParamType>(New->getTypeForDecl());
4932+
SmallVector<ObjCProtocolDecl *, 8> protocols;
4933+
protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end());
4934+
QualType UpdatedTy = getObjCTypeParamType(New, protocols);
4935+
New->setTypeForDecl(UpdatedTy.getTypePtr());
4936+
}
4937+
49274938
/// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's
49284939
/// protocol list adopt all protocols in QT's qualified-id protocol
49294940
/// list.

clang/lib/AST/Type.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3585,15 +3585,17 @@ void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
35853585

35863586
void ObjCTypeParamType::Profile(llvm::FoldingSetNodeID &ID,
35873587
const ObjCTypeParamDecl *OTPDecl,
3588+
QualType CanonicalType,
35883589
ArrayRef<ObjCProtocolDecl *> protocols) {
35893590
ID.AddPointer(OTPDecl);
3591+
ID.AddPointer(CanonicalType.getAsOpaquePtr());
35903592
ID.AddInteger(protocols.size());
35913593
for (auto proto : protocols)
35923594
ID.AddPointer(proto);
35933595
}
35943596

35953597
void ObjCTypeParamType::Profile(llvm::FoldingSetNodeID &ID) {
3596-
Profile(ID, getDecl(),
3598+
Profile(ID, getDecl(), getCanonicalTypeInternal(),
35973599
llvm::makeArrayRef(qual_begin(), getNumProtocols()));
35983600
}
35993601

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)