Skip to content

[Type checker] Warn about classes conforming to AnyObject in Swift 3 mode #9286

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 1 commit into from
May 4, 2017
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
3 changes: 2 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,8 @@ ERROR(inheritance_from_cf_class,none,
ERROR(inheritance_from_objc_runtime_visible_class,none,
"cannot inherit from class %0 because it is only visible via the "
"Objective-C runtime", (Identifier))

WARNING(class_inherits_anyobject,none,
"conformance of class %0 to 'AnyObject' is redundant", (Type))

// Enums
ERROR(enum_case_access,none,
Expand Down
92 changes: 67 additions & 25 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,55 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
}
}

// Retrieve the location of the start of the inheritance clause.
auto getStartLocOfInheritanceClause = [&] {
if (auto genericTypeDecl = dyn_cast<GenericTypeDecl>(decl)) {
if (auto genericParams = genericTypeDecl->getGenericParams())
return genericParams->getSourceRange().End;

return genericTypeDecl->getNameLoc();
}

if (auto typeDecl = dyn_cast<TypeDecl>(decl))
return typeDecl->getNameLoc();

if (auto ext = dyn_cast<ExtensionDecl>(decl))
return ext->getSourceRange().End;

return SourceLoc();
};

// Compute the source range to be used when removing something from an
// inheritance clause.
auto getRemovalRange = [&](unsigned i) {
// If there is just one entry, remove the entire inheritance clause.
if (inheritedClause.size() == 1) {
SourceLoc start = getStartLocOfInheritanceClause();
SourceLoc end = inheritedClause[i].getSourceRange().End;
return SourceRange(Lexer::getLocForEndOfToken(Context.SourceMgr, start),
Lexer::getLocForEndOfToken(Context.SourceMgr, end));
}

// If we're at the first entry, remove from the start of this entry to the
// start of the next entry.
if (i == 0) {
return SourceRange(inheritedClause[i].getSourceRange().Start,
inheritedClause[i+1].getSourceRange().Start);
}

// Otherwise, remove from the end of the previous entry to the end of this
// entry.
SourceLoc afterPriorLoc =
Lexer::getLocForEndOfToken(Context.SourceMgr,
inheritedClause[i-1].getSourceRange().End);

SourceLoc afterMyEndLoc =
Lexer::getLocForEndOfToken(Context.SourceMgr,
inheritedClause[i].getSourceRange().End);

return SourceRange(afterPriorLoc, afterMyEndLoc);
};

// Check all of the types listed in the inheritance clause.
Type superclassTy;
SourceRange superclassRange;
Expand Down Expand Up @@ -424,16 +473,10 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
CanType inheritedCanTy = inheritedTy->getCanonicalType();
auto knownType = inheritedTypes.find(inheritedCanTy);
if (knownType != inheritedTypes.end()) {
SourceLoc afterPriorLoc
= Lexer::getLocForEndOfToken(Context.SourceMgr,
inheritedClause[i-1].getSourceRange().End);
SourceLoc afterMyEndLoc
= Lexer::getLocForEndOfToken(Context.SourceMgr,
inherited.getSourceRange().Start);

auto removeRange = getRemovalRange(i);
diagnose(inherited.getSourceRange().Start,
diag::duplicate_inheritance, inheritedTy)
.fixItRemoveChars(afterPriorLoc, afterMyEndLoc)
.fixItRemoveChars(removeRange.Start, removeRange.End)
.highlight(knownType->second);
inherited.setInvalidType(Context);
continue;
Expand All @@ -457,6 +500,18 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
}
continue;
}

// Swift 3 compatibility:
if (Context.LangOpts.isSwiftVersion3() && isa<ClassDecl>(decl) &&
inheritedTy->isAnyObject()) {
auto classDecl = cast<ClassDecl>(decl);
auto removeRange = getRemovalRange(i);
diagnose(inherited.getSourceRange().Start,
diag::class_inherits_anyobject,
classDecl->getDeclaredInterfaceType())
.fixItRemoveChars(removeRange.Start, removeRange.End);
continue;
}
}

// If this is an enum inheritance clause, check for a raw type.
Expand All @@ -472,17 +527,11 @@ void TypeChecker::checkInheritanceClause(Decl *decl,

// If this is not the first entry in the inheritance clause, complain.
if (i > 0) {
SourceLoc afterPriorLoc
= Lexer::getLocForEndOfToken(
Context.SourceMgr,
inheritedClause[i-1].getSourceRange().End);
SourceLoc afterMyEndLoc
= Lexer::getLocForEndOfToken(Context.SourceMgr,
inherited.getSourceRange().End);
auto removeRange = getRemovalRange(i);

diagnose(inherited.getSourceRange().Start,
diag::raw_type_not_first, inheritedTy)
.fixItRemoveChars(afterPriorLoc, afterMyEndLoc)
.fixItRemoveChars(removeRange.Start, removeRange.End)
.fixItInsert(inheritedClause[0].getSourceRange().Start,
inheritedTy.getString() + ", ");

Expand Down Expand Up @@ -532,17 +581,10 @@ void TypeChecker::checkInheritanceClause(Decl *decl,

// If this is not the first entry in the inheritance clause, complain.
if (i > 0) {
SourceLoc afterPriorLoc
= Lexer::getLocForEndOfToken(
Context.SourceMgr,
inheritedClause[i-1].getSourceRange().End);
SourceLoc afterMyEndLoc
= Lexer::getLocForEndOfToken(Context.SourceMgr,
inherited.getSourceRange().End);

auto removeRange = getRemovalRange(i);
diagnose(inherited.getSourceRange().Start,
diag::superclass_not_first, inheritedTy)
.fixItRemoveChars(afterPriorLoc, afterMyEndLoc)
.fixItRemoveChars(removeRange.Start, removeRange.End)
.fixItInsert(inheritedClause[0].getSourceRange().Start,
inheritedTy.getString() + ", ");

Expand Down
10 changes: 10 additions & 0 deletions test/Compatibility/anyobject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift -swift-version 3

protocol P { }
protocol Q { }

class Foo: AnyObject { } // expected-warning{{conformance of class 'Foo' to 'AnyObject' is redundant}}{{10-21=}}

class Bar: AnyObject, P { } // expected-warning{{conformance of class 'Bar' to 'AnyObject' is redundant}}{{12-23=}}

class Wibble: Bar, AnyObject, Q { } // expected-warning{{conformance of class 'Wibble' to 'AnyObject' is redundant}}{{18-29=}}
4 changes: 2 additions & 2 deletions test/decl/protocol/conforms/placement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extension ExplicitSub1 : P1 { } // expected-error{{redundant conformance of 'Exp
// ---------------------------------------------------------------------------
// Suppression of synthesized conformances
// ---------------------------------------------------------------------------
class SynthesizedClass1 : AnyObject { } // expected-error{{inheritance from non-protocol, non-class type 'AnyObject'}}
class SynthesizedClass1 : AnyObject { } // expected-warning{{conformance of class 'SynthesizedClass1' to 'AnyObject' is redundant}}

class SynthesizedClass2 { }
extension SynthesizedClass2 : AnyObject { } // expected-error{{inheritance from non-protocol type 'AnyObject'}}
Expand All @@ -116,7 +116,7 @@ class SynthesizedClass3 : AnyObjectRefinement { }
class SynthesizedClass4 { }
extension SynthesizedClass4 : AnyObjectRefinement { }

class SynthesizedSubClass1 : SynthesizedClass1, AnyObject { } // expected-error{{inheritance from non-protocol, non-class type 'AnyObject'}}
class SynthesizedSubClass1 : SynthesizedClass1, AnyObject { } // expected-warning{{conformance of class 'SynthesizedSubClass1' to 'AnyObject' is redundant}}

class SynthesizedSubClass2 : SynthesizedClass2 { }
extension SynthesizedSubClass2 : AnyObject { } // expected-error{{inheritance from non-protocol type 'AnyObject'}}
Expand Down