@@ -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,33 @@ 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 with Swift <= 4.
481
+ if (!Context.LangOpts .isSwiftVersionAtLeast (5 ) &&
482
+ inheritedTy->isAnyObject () &&
483
+ (isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
484
+ Lexer::getTokenAtLocation (Context.SourceMgr ,
485
+ knownType->second .second .Start )
486
+ .is (tok::kw_class)) {
487
+ SourceLoc classLoc = knownType->second .second .Start ;
488
+ SourceRange removeRange = getRemovalRange (knownType->second .first );
489
+
490
+ diagnose (classLoc, diag::duplicate_anyobject_class_inheritance)
491
+ .fixItRemoveChars (removeRange.Start , removeRange.End );
492
+ inherited.setInvalidType (Context);
493
+ continue ;
494
+ }
495
+
478
496
auto removeRange = getRemovalRange (i);
479
497
diagnose (inherited.getSourceRange ().Start ,
480
498
diag::duplicate_inheritance, inheritedTy)
481
499
.fixItRemoveChars (removeRange.Start , removeRange.End )
482
- .highlight (knownType->second );
500
+ .highlight (knownType->second . second );
483
501
inherited.setInvalidType (Context);
484
502
continue ;
485
503
}
486
- inheritedTypes[inheritedCanTy] = inherited.getSourceRange ();
504
+ inheritedTypes[inheritedCanTy] = { i, inherited.getSourceRange () } ;
487
505
488
506
// If this is a protocol or protocol composition type, record the
489
507
// protocols.
0 commit comments