swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.34k forks source link

Using P.self on private @objc protocol with custom ObjC name causing crash. #74546

Open WeZZard opened 2 months ago

WeZZard commented 2 months ago

Description

Running the following code on Xcode 15.0 and 16.0 causing a runtime crash. a can be evaluated in LLDB. However, it's 0x0 when evaluated in assembly mode.

@objc(MyName)
private protocol MyObjCProtocol {

    func myObjCProtocolMessage()

}

private let a = MyObjCProtocol.self
print(a)
Screenshot 2024-06-19 at 18 11 02 Screenshot 2024-06-19 at 18 11 36

Reproduction

@objc(MyName)
private protocol MyObjCProtocol {

    func myObjCProtocolMessage()

}

private let a = MyObjCProtocol.self
print(a)

Stack dump

Thread 1 Queue : com.apple.main-thread (serial)
#0  0x00000001acad98d0 in outlined init with copy of Any ()
#1  0x00000001ac932d5c in specialized _print<τ_0_0>(_:separator:terminator:to:) ()
#2  0x00000001ac931efc in print(_:separator:terminator:) ()
#3  0x00000001000032dc in main at main.swift:295
#4  0x000000019c73e0e0 in start ()

Expected behavior

Printing "MyObjCProtocol"

Environment

Xcode 15.0 Xcode 16.0

Additional information

No response

mikeash commented 2 months ago

Thanks for the report. The issue is that the compiler emits a mangled name with a private declaration on it, something like the encoding of __C.(MyName in _1AECFD08E409979057FF235F763A1E9D). The runtime doesn't expect this and the lookup fails. I'll get this to the right person. As a workaround, making the protocol internal avoids emitting the bad mangled name.