@@ -440,7 +440,7 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
440
440
Type superclassTy;
441
441
SourceRange superclassRange;
442
442
llvm::SmallSetVector<ProtocolDecl *, 4 > allProtocols;
443
- llvm::SmallDenseMap<CanType, SourceRange> inheritedTypes;
443
+ llvm::SmallDenseMap<CanType, std::pair< unsigned , SourceRange> > inheritedTypes;
444
444
addImplicitConformances (*this , decl, allProtocols);
445
445
for (unsigned i = 0 , n = inheritedClause.size (); i != n; ++i) {
446
446
auto &inherited = inheritedClause[i];
@@ -475,15 +475,32 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
475
475
CanType inheritedCanTy = inheritedTy->getCanonicalType ();
476
476
auto knownType = inheritedTypes.find (inheritedCanTy);
477
477
if (knownType != inheritedTypes.end ()) {
478
+ // If the duplicated type is 'AnyObject', check whether the first was
479
+ // written as 'class'. Downgrade the error to a warning in such cases
480
+ // for backward compatibility.
481
+ if (inheritedTy->isAnyObject () &&
482
+ (isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
483
+ Lexer::getTokenAtLocation (Context.SourceMgr ,
484
+ knownType->second .second .Start )
485
+ .is (tok::kw_class)) {
486
+ SourceLoc classLoc = knownType->second .second .Start ;
487
+ SourceRange removeRange = getRemovalRange (knownType->second .first );
488
+
489
+ diagnose (classLoc, diag::duplicate_anyobject_class_inheritance)
490
+ .fixItRemoveChars (removeRange.Start , removeRange.End );
491
+ inherited.setInvalidType (Context);
492
+ continue ;
493
+ }
494
+
478
495
auto removeRange = getRemovalRange (i);
479
496
diagnose (inherited.getSourceRange ().Start ,
480
497
diag::duplicate_inheritance, inheritedTy)
481
498
.fixItRemoveChars (removeRange.Start , removeRange.End )
482
- .highlight (knownType->second );
499
+ .highlight (knownType->second . second );
483
500
inherited.setInvalidType (Context);
484
501
continue ;
485
502
}
486
- inheritedTypes[inheritedCanTy] = inherited.getSourceRange ();
503
+ inheritedTypes[inheritedCanTy] = { i, inherited.getSourceRange () } ;
487
504
488
505
// If this is a protocol or protocol composition type, record the
489
506
// protocols.
0 commit comments