Skip to content

Commit e3373a9

Browse files
[Sema] Adding deprecation warning for protocol inheritance class keyword syntax
1 parent 887464b commit e3373a9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,9 @@ WARNING(duplicate_anyobject_class_inheritance,none,
26112611
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
26122612
ERROR(inheritance_from_protocol_with_superclass,none,
26132613
"inheritance from class-constrained protocol composition type %0", (Type))
2614+
WARNING(anyobject_class_inheritance_deprecated,none,
2615+
"using 'class' keyword for protocol inheritance is deprecated; "
2616+
"use 'AnyObject' instead", ())
26142617
ERROR(multiple_inheritance,none,
26152618
"multiple inheritance from classes %0 and %1", (Type, Type))
26162619
ERROR(inheritance_from_non_protocol_or_class,none,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ static void checkInheritanceClause(
179179
// GenericSignatureBuilder (for protocol inheritance) or the
180180
// ConformanceLookupTable (for protocol conformance).
181181
if (inheritedTy->isAnyObject()) {
182+
// Warn inherited AnyObject written as 'class' as deprecated
183+
// for Swift >= 5.
184+
auto sourceRange = inherited.getSourceRange();
185+
bool isWrittenAsClass =
186+
(isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
187+
Lexer::getTokenAtLocation(ctx.SourceMgr, sourceRange.Start)
188+
.is(tok::kw_class);
189+
if (ctx.LangOpts.isSwiftVersionAtLeast(5) && isWrittenAsClass) {
190+
diags
191+
.diagnose(sourceRange.Start,
192+
diag::anyobject_class_inheritance_deprecated)
193+
.fixItReplace(sourceRange, "AnyObject");
194+
}
195+
182196
if (inheritedAnyObject) {
183197
// If the first occurrence was written as 'class', downgrade the error
184198
// to a warning in such case for backward compatibility with

0 commit comments

Comments
 (0)