Skip to content

Commit 4861441

Browse files
authored
Merge pull request swiftlang#34885 from LucianoPAlmeida/warn-class-protocol-inheritance
[Sema] Adding deprecation warning for protocol inheritance 'class' syntax
2 parents 2b0ff64 + 338faab commit 4861441

16 files changed

+47
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,9 @@ WARNING(duplicate_anyobject_class_inheritance,none,
26152615
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
26162616
ERROR(inheritance_from_protocol_with_superclass,none,
26172617
"inheritance from class-constrained protocol composition type %0", (Type))
2618+
WARNING(anyobject_class_inheritance_deprecated,none,
2619+
"using 'class' keyword for protocol inheritance is deprecated; "
2620+
"use 'AnyObject' instead", ())
26182621
ERROR(multiple_inheritance,none,
26192622
"multiple inheritance from classes %0 and %1", (Type, Type))
26202623
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

stdlib/public/Darwin/CoreFoundation/CoreFoundation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@_exported import CoreFoundation
1414

15-
public protocol _CFObject: class, Hashable {}
15+
public protocol _CFObject: AnyObject, Hashable {}
1616
extension _CFObject {
1717
public var hashValue: Int {
1818
return Int(bitPattern: CFHash(self))

stdlib/public/core/BridgeObjectiveC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeabl
681681
}
682682

683683
public // SPI(Foundation)
684-
protocol _NSSwiftValue: class {
684+
protocol _NSSwiftValue: AnyObject {
685685
init(_ value: Any)
686686
var value: Any { get }
687687
static var null: AnyObject { get }

stdlib/public/core/ExistentialCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ extension AnyRandomAccessCollection {
16411641
//===----------------------------------------------------------------------===//
16421642

16431643
@usableFromInline
1644-
internal protocol _AnyIndexBox: class {
1644+
internal protocol _AnyIndexBox: AnyObject {
16451645
var _typeID: ObjectIdentifier { get }
16461646

16471647
func _unbox<T: Comparable>() -> T?

test/Parse/try_swift5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ let _: Int??? = try? producer.produceDoubleOptionalInt() // good
263263
let _: String = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
264264

265265
// rdar://problem/46742002
266-
protocol Dummy : class {}
266+
protocol Dummy : AnyObject {}
267267

268268
class F<T> {
269269
func wait() throws -> T { fatalError() }

test/decl/func/dynamic_self.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension P0 {
4444
}
4545
}
4646

47-
protocol P1: class {
47+
protocol P1: AnyObject {
4848
func f() -> Self // okay
4949

5050
func g(_ ds: Self) // okay
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol P: class {}
4+
protocol P1: AnyObject {}
5+
protocol P2 {}
6+
protocol P3: class, P2 {}
7+
protocol P4: P2, class {} // expected-error {{'class' must come first in the requirement list}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5
2+
3+
protocol P: class {}
4+
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{13-18=AnyObject}}
5+
protocol P1: AnyObject {}
6+
protocol P2 {}
7+
protocol P3: class, P2 {}
8+
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{14-19=AnyObject}}
9+
protocol P4: P2, class {} // expected-error {{'class' must come first in the requirement list}}
10+
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{18-23=AnyObject}}

test/stdlib/Builtins.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ tests.test("array value witnesses") {
165165
expectEqual(NoisyLifeCount, NoisyDeathCount)
166166
}
167167

168-
protocol Classy : class {}
168+
protocol Classy : AnyObject {}
169169
class A : Classy {}
170170
class B : A {}
171171
class C : B {}

0 commit comments

Comments
 (0)