Passing class-constrained type as AnyObject instance behavior is not matched on Darwin and non-Darwin platform

The following code compiles fine on macOS with Xcode 15.0.

class X {
    func test1(_ o: AnyObject) {}
    func test12() { test1(Self.self) }
}

But it will emit a fatalError when compiling on Linux.

swift build
Building for debugging...
/Users/kyle/Downloads/DemoKit/Sources/DemoKit/DemoKit.swift:9:27: error: argument type 'Self.Type' expected to be an instance of a class or class-constrained type
    func test12() { test1(Self.self) }
                          ^
error: fatalError
swift --version
Swift version 5.9.2 (swift-5.9.2-RELEASE)
Target: aarch64-unknown-linux-gnu

Anyone knows how to fix the issue?

Related Build Error Log

Related post:

At first, I thought it was something due to we have implicit base class on Darwin only.

But I was wrong. Adding as! will give us hint that this cast will always success

/Users/kyle/Downloads/DemoKit/Sources/DemoKit/DemoKit.swift:9:37: warning: forced cast from 'Self.Type' to 'AnyObject' always succeeds; did you mean to use 'as'?
    func test2() { test1(Self.self as! AnyObject) }
                                    ^~~
                                    as

So the final workaround is just insert "as AnyObject" manually here.

Still wondering why this is not needed on Darwin platform. And maybe we can improve this mismatch behavior upstream.

Trackd by #70645

2 Likes