Skip to content

Commit d18a2b7

Browse files
committed
Re-enable actor inheritance from NSObject.
For now, it's the only way to get NSObjectProtocol conformance. Fixes rdar://80476009.
1 parent df7656e commit d18a2b7

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,8 +4357,6 @@ ERROR(async_objc_dynamic_self,none,
43574357
ERROR(actor_inheritance,none,
43584358
"%select{actor|distributed actor}0 types do not support inheritance",
43594359
(bool))
4360-
NOTE(actor_inheritance_nsobject,none,
4361-
"use '@objc' to expose actor %0 to Objective-C", (DeclName))
43624360

43634361
ERROR(actor_protocol_illegal_inheritance,none,
43644362
"non-actor type %0 cannot conform to the 'Actor' protocol",

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,18 +2406,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24062406

24072407
if (auto superclass = CD->getSuperclassDecl()) {
24082408
// Actors cannot have superclasses, nor can they be superclasses.
2409-
if (CD->isActor()) {
2409+
if (CD->isActor() && !superclass->isNSObject())
24102410
CD->diagnose(diag::actor_inheritance,
24112411
/*distributed=*/CD->isDistributedActor());
2412-
if (superclass->isNSObject() && !CD->isDistributedActor()) {
2413-
CD->diagnose(diag::actor_inheritance_nsobject, CD->getName())
2414-
.fixItInsert(CD->getAttributeInsertionLoc(/*forModifier=*/false),
2415-
"@objc ");
2416-
}
2417-
} else if (superclass->isActor()) {
2412+
else if (superclass->isActor())
24182413
CD->diagnose(diag::actor_inheritance,
24192414
/*distributed=*/CD->isDistributedActor());
2420-
}
24212415
}
24222416

24232417
// Force lowering of stored properties.

test/Interpreter/actor_class_forbid_objc_assoc_objects.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// UNSUPPORTED: back_deployment_runtime
1111

1212
import ObjectiveC
13-
import Foundation
1413
import _Concurrency
1514
import StdlibUnittest
1615

@@ -71,7 +70,7 @@ if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) {
7170
}
7271

7372
@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *)
74-
@objc actor ActorNSObjectSubKlass {}
73+
actor ActorNSObjectSubKlass : NSObject {}
7574

7675
if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) {
7776
Tests.test("no crash when inherit from nsobject")
@@ -80,3 +79,17 @@ if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) {
8079
objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN)
8180
}
8281
}
82+
83+
@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *)
84+
actor ActorNSObjectSubKlassGeneric<T> : NSObject {
85+
var state: T
86+
init(state: T) { self.state = state }
87+
}
88+
89+
if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) {
90+
Tests.test("no crash when generic inherit from nsobject")
91+
.code {
92+
let x = ActorNSObjectSubKlassGeneric(state: 5)
93+
objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN)
94+
}
95+
}

test/ModuleInterface/actor_objc.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import Foundation
1414

15-
// CHECK-LABEL: @objc public actor SomeActor {
16-
// CHECK-NOT: @objc override public init()
17-
@objc public actor SomeActor {
15+
// CHECK-LABEL: @objc @_inheritsConvenienceInitializers public actor SomeActor : ObjectiveC.NSObject {
16+
// CHECK: @objc override public init()
17+
public actor SomeActor: NSObject {
1818
}

test/attr/attr_objc_async.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ actor class MyActor2 { }
5151
// expected-error@-1 {{keyword 'class' cannot be used as an identifier here}}
5252

5353
// CHECK: @objc actor MyObjCActor
54-
@objc actor MyObjCActor { }
54+
@objc actor MyObjCActor: NSObject { }
5555

56-
@objc actor class MyObjCActor2 {}
56+
@objc actor class MyObjCActor2: NSObject {}
5757
// expected-error@-1 {{keyword 'class' cannot be used as an identifier here}}
58-
59-
actor MyObjCActor3: NSObject { } // expected-error{{actor types do not support inheritance}}
60-
// expected-note@-1{{use '@objc' to expose actor 'MyObjCActor3' to Objective-C}}

0 commit comments

Comments
 (0)