Skip to content
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: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,9 @@ WARNING(duplicate_anyobject_class_inheritance,none,
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
ERROR(inheritance_from_protocol_with_superclass,none,
"inheritance from class-constrained protocol composition type %0", (Type))
WARNING(anyobject_class_inheritance_deprecated,none,
"using 'class' keyword for protocol inheritance is deprecated; "
"use 'AnyObject' instead", ())
ERROR(multiple_inheritance,none,
"multiple inheritance from classes %0 and %1", (Type, Type))
ERROR(inheritance_from_non_protocol_or_class,none,
Expand Down
14 changes: 14 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ static void checkInheritanceClause(
// GenericSignatureBuilder (for protocol inheritance) or the
// ConformanceLookupTable (for protocol conformance).
if (inheritedTy->isAnyObject()) {
// Warn inherited AnyObject written as 'class' as deprecated
// for Swift >= 5.
auto sourceRange = inherited.getSourceRange();
bool isWrittenAsClass =
(isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
Lexer::getTokenAtLocation(ctx.SourceMgr, sourceRange.Start)
.is(tok::kw_class);
if (ctx.LangOpts.isSwiftVersionAtLeast(5) && isWrittenAsClass) {
diags
.diagnose(sourceRange.Start,
diag::anyobject_class_inheritance_deprecated)
.fixItReplace(sourceRange, "AnyObject");
}

if (inheritedAnyObject) {
// If the first occurrence was written as 'class', downgrade the error
// to a warning in such case for backward compatibility with
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/CoreFoundation/CoreFoundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@_exported import CoreFoundation

public protocol _CFObject: class, Hashable {}
public protocol _CFObject: AnyObject, Hashable {}
extension _CFObject {
public var hashValue: Int {
return Int(bitPattern: CFHash(self))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/BridgeObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeabl
}

public // SPI(Foundation)
protocol _NSSwiftValue: class {
protocol _NSSwiftValue: AnyObject {
init(_ value: Any)
var value: Any { get }
static var null: AnyObject { get }
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ExistentialCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ extension AnyRandomAccessCollection {
//===----------------------------------------------------------------------===//

@usableFromInline
internal protocol _AnyIndexBox: class {
internal protocol _AnyIndexBox: AnyObject {
var _typeID: ObjectIdentifier { get }

func _unbox<T: Comparable>() -> T?
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/try_swift5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ let _: Int??? = try? producer.produceDoubleOptionalInt() // good
let _: String = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}

// rdar://problem/46742002
protocol Dummy : class {}
protocol Dummy : AnyObject {}

class F<T> {
func wait() throws -> T { fatalError() }
Expand Down
2 changes: 1 addition & 1 deletion test/decl/func/dynamic_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension P0 {
}
}

protocol P1: class {
protocol P1: AnyObject {
func f() -> Self // okay

func g(_ ds: Self) // okay
Expand Down
7 changes: 7 additions & 0 deletions test/decl/inherit/inherit_anyobject_protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

protocol P: class {}
protocol P1: AnyObject {}
protocol P2 {}
protocol P3: class, P2 {}
protocol P4: P2, class {} // expected-error {{'class' must come first in the requirement list}}
10 changes: 10 additions & 0 deletions test/decl/inherit/inherit_anyobject_protocol_swift5.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift -swift-version 5

protocol P: class {}
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{13-18=AnyObject}}
protocol P1: AnyObject {}
protocol P2 {}
protocol P3: class, P2 {}
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{14-19=AnyObject}}
protocol P4: P2, class {} // expected-error {{'class' must come first in the requirement list}}
// expected-warning@-1 {{using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead}} {{18-23=AnyObject}}
2 changes: 1 addition & 1 deletion test/stdlib/Builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ tests.test("array value witnesses") {
expectEqual(NoisyLifeCount, NoisyDeathCount)
}

protocol Classy : class {}
protocol Classy : AnyObject {}
class A : Classy {}
class B : A {}
class C : B {}
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protocol OtherProtocol {
var otherProperty: String { get }
}

protocol OtherClassProtocol : class {
protocol OtherClassProtocol : AnyObject {
var otherClassProperty: String { get }
}

Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protocol OtherProtocol {
var otherProperty: String { get }
}

protocol OtherClassProtocol : class {
protocol OtherClassProtocol : AnyObject {
var otherClassProperty: String { get }
}

Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ mirrors.test("struct/WrapNSArray") {
// Check that Mirror correctly reflects weak/unowned refs to both
// Swift and ObjC objects from Swift structs and classes.

protocol WeakUnownedTestsP1: class {
protocol WeakUnownedTestsP1: AnyObject {
func f1() -> Int
}

Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/Reflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ print("Fooable double:")
fooable = 2.5
dump(fooable)

protocol Barrable : class {}
protocol Barrable : AnyObject {}
extension Best: Barrable {}

// CHECK-LABEL: Barrable class:
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ Runtime.test("abstraction barrier on casting generic param bound to existential"
class Malkovich: Malkovichable {
var malkovich: String { return "malkovich" }
}
protocol Malkovichable: class {
protocol Malkovichable: AnyObject {
var malkovich: String { get }
}

Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/WeakMirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NativeSwiftClass : NativeClassBoundExistential {
}
}

protocol NativeClassBoundExistential : class {
protocol NativeClassBoundExistential : AnyObject {
var x: Int { get }
}
class NativeSwiftClassHasWeak {
Expand Down Expand Up @@ -123,7 +123,7 @@ mirrors.test("class/NativeSwiftClassHasNativeWeakReferenceNoLeak") {

import Foundation

@objc protocol ObjCClassExistential : class {
@objc protocol ObjCClassExistential : AnyObject {
var weakProperty: AnyObject? { get set }
var x: Int { get }
}
Expand Down