mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
override checking for `NSObject.init()` Overriding `NSObject.init()` within a `@MainActor`-isolated type is difficult-to-impossible, especially if you need to call an initializer from an intermediate superclass that is also `@MainActor`-isolated. This won't admit a runtime data-race safety hole, because dynamic isolation checks will be inserted in the @objc thunks under `DynamicActorIsolation`, and direct calls will enforce `@MainActor` as usual.
59 lines
1.7 KiB
Swift
59 lines
1.7 KiB
Swift
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/custom-modules %s -verify -strict-concurrency=complete -parse-as-library
|
|
|
|
// REQUIRES: objc_interop
|
|
// REQUIRES: concurrency
|
|
// REQUIRES: asserts
|
|
|
|
import Foundation
|
|
import ObjCConcurrency
|
|
|
|
// expected-note@+1 2{{add '@MainActor' to make global function 'unsatisfiedPreconcurrencyIsolation(view:)' part of global actor 'MainActor'}}
|
|
func unsatisfiedPreconcurrencyIsolation(view: MyView) {
|
|
// expected-warning@+1 {{call to main actor-isolated instance method 'display()' in a synchronous nonisolated context}}
|
|
view.display()
|
|
|
|
// expected-warning@+1 {{main actor-isolated property 'isVisible' can not be referenced from a nonisolated context}}
|
|
_ = view.isVisible
|
|
}
|
|
|
|
@preconcurrency @MainActor
|
|
class IsolatedSub: NXSender {
|
|
var mainActorState = 0 // expected-note {{property declared here}}
|
|
override func sendAny(_: any Sendable) -> any Sendable {
|
|
return mainActorState
|
|
// expected-warning@-1 {{main actor-isolated property 'mainActorState' can not be referenced from a nonisolated context}}
|
|
}
|
|
|
|
@MainActor
|
|
override func sendOptionalAny(_: (any Sendable)?) -> (any Sendable)? {
|
|
// expected-warning@-1 {{main actor-isolated instance method 'sendOptionalAny' has different actor isolation from nonisolated overridden declaration; this is an error in the Swift 6 language mode}}
|
|
|
|
return mainActorState
|
|
}
|
|
}
|
|
|
|
class NotSendable {}
|
|
|
|
@MainActor
|
|
class NSObjectInitOverride: NSObject {
|
|
var ns: NotSendable
|
|
|
|
override init() {
|
|
self.ns = NotSendable()
|
|
super.init()
|
|
}
|
|
}
|
|
|
|
|
|
@objc
|
|
@MainActor
|
|
class Test : NSObject {
|
|
static var shared: Test?
|
|
|
|
override init() {
|
|
super.init()
|
|
|
|
Self.shared = self
|
|
}
|
|
}
|