Skip to content

[AST] Treat actors inheriting from NSObject as SwiftNativeNSObjects. #38803

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
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
9 changes: 7 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8491,8 +8491,13 @@ bool ClassDecl::isRootDefaultActor(ModuleDecl *M,

bool ClassDecl::isNativeNSObjectSubclass() const {
// @objc actors implicitly inherit from NSObject.
if (isActor() && getAttrs().hasAttribute<ObjCAttr>())
return true;
if (isActor()) {
if (getAttrs().hasAttribute<ObjCAttr>()) {
return true;
}
ClassDecl *superclass = getSuperclassDecl();
return superclass && superclass->isNSObject();
}

// For now, non-actor classes cannot use the native NSObject subclass.
// Eventually we should roll this out to more classes that directly
Expand Down
34 changes: 34 additions & 0 deletions test/SILGen/objc_actor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-swift-frontend -emit-silgen %s -swift-version 5 -disable-availability-checking | %FileCheck %s
// REQUIRES: concurrency
// REQUIRES: objc_interop

// rdar://80863853 - For an actor inheriting from NSObject and using '@objc'
// should have the same effect: the effective superclass is SwiftNativeNSObject
// (see 945011d39f8b271b8906bd509aac3aa954f4fc57) not NSObject.
// Check that we don't treat any case as an ObjC class.

import Foundation

public actor MyClass1: NSObject {
public var x: Int
public init(_ x: Int) { self.x = x }
}

// CHECK: alloc_ref $MyClass1
// CHECK-NOT: alloc_ref [objc] $MyClass1

@objc public actor MyClass2 {
public var x: Int
public init(_ x: Int) { self.x = x }
}

// CHECK: alloc_ref $MyClass2
// CHECK-NOT: alloc_ref [objc] $MyClass2

@objc public actor MyClass3: NSObject {
public var x: Int
public init(_ x: Int) { self.x = x }
}

// CHECK: alloc_ref $MyClass3
// CHECK-NOT: alloc_ref [objc] $MyClass3