Skip to content

Allow "protocol P : class, AnyObject..." for Swift 4 compatibility. #11987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,8 @@ WARNING(override_swift3_objc_inference,none,
// Inheritance
ERROR(duplicate_inheritance,none,
"duplicate inheritance from %0", (Type))
WARNING(duplicate_anyobject_class_inheritance,none,
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
ERROR(multiple_inheritance,none,
"multiple inheritance from classes %0 and %1", (Type, Type))
ERROR(non_class_inheritance,none,
Expand Down
24 changes: 21 additions & 3 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
Type superclassTy;
SourceRange superclassRange;
llvm::SmallSetVector<ProtocolDecl *, 4> allProtocols;
llvm::SmallDenseMap<CanType, SourceRange> inheritedTypes;
llvm::SmallDenseMap<CanType, std::pair<unsigned, SourceRange>> inheritedTypes;
addImplicitConformances(*this, decl, allProtocols);
for (unsigned i = 0, n = inheritedClause.size(); i != n; ++i) {
auto &inherited = inheritedClause[i];
Expand Down Expand Up @@ -475,15 +475,33 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
CanType inheritedCanTy = inheritedTy->getCanonicalType();
auto knownType = inheritedTypes.find(inheritedCanTy);
if (knownType != inheritedTypes.end()) {
// If the duplicated type is 'AnyObject', check whether the first was
// written as 'class'. Downgrade the error to a warning in such cases
// for backward compatibility with Swift <= 4.
if (!Context.LangOpts.isSwiftVersionAtLeast(5) &&
inheritedTy->isAnyObject() &&
(isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
Lexer::getTokenAtLocation(Context.SourceMgr,
knownType->second.second.Start)
.is(tok::kw_class)) {
SourceLoc classLoc = knownType->second.second.Start;
SourceRange removeRange = getRemovalRange(knownType->second.first);

diagnose(classLoc, diag::duplicate_anyobject_class_inheritance)
.fixItRemoveChars(removeRange.Start, removeRange.End);
inherited.setInvalidType(Context);
continue;
}

auto removeRange = getRemovalRange(i);
diagnose(inherited.getSourceRange().Start,
diag::duplicate_inheritance, inheritedTy)
.fixItRemoveChars(removeRange.Start, removeRange.End)
.highlight(knownType->second);
.highlight(knownType->second.second);
inherited.setInvalidType(Context);
continue;
}
inheritedTypes[inheritedCanTy] = inherited.getSourceRange();
inheritedTypes[inheritedCanTy] = { i, inherited.getSourceRange() };

// If this is a protocol or protocol composition type, record the
// protocols.
Expand Down
7 changes: 7 additions & 0 deletions test/Compatibility/anyobject_class.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift -swift-version 3
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: not %target-swift-frontend -tyepcheck -swift-version 5

protocol P : class, AnyObject { } // expected-warning{{redundant inheritance from 'AnyObject' and Swift 3 'class' keyword}}{{14-21=}}
// expected-warning@-1{{redundant layout constraint 'Self' : 'AnyObject'}}
// expected-note@-2{{layout constraint constraint 'Self' : 'AnyObject' written here}}