Swift: Crash when printing `self.dynamicType` in a protocol extension constrained on Objective-C subclass

Originator:jp
Number:rdar://24173309 Date Originated:13-Jan-2016 11:56 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-7.2 (7C68)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
Accessing `self.dynamicType` in a protocol extension constrained on Objective-C subclass in Swift causes a bad memory access.

Steps to Reproduce:
Run the following code:

```swift
import Foundation
// doesn't crash when subclassing a non-Objective-C class, or not subclassing
class Class1: NSObject, Protocol1 {}

protocol Protocol1 {}
// doesn't crash without the type constraint, or with a different constraint ('Self: Hashable')
extension Protocol1 where Self: Class1 {
    func printType() {
        print(self.dynamicType)
        // also crashes:
        // _ = String(self.dynamicType)
        // self.dynamicType.description()
    }
}

Class1().printType() // EXC_BAD_ACCESS
```

Expected Results:
Accessing `self.dynamicType` in a protocol extension constrained on Objective-C subclass in Swift should return the calling type.

Actual Results:
Accessing `self.dynamicType` in a protocol extension constrained on Objective-C subclass in Swift causes a bad memory access.

Regression:
N/A

Notes:
N/A

Comments

31-Jan-2017 10:19 AM

Interestingly, this appears to be fixed in swiftc but not when JITing it with swift with Xcode 8.3 beta 1:

` $ cat file.swift import Foundation

class Class1: NSObject, Protocol1 {}

protocol Protocol1 {} extension Protocol1 where Self: Class1 { func printType() { print(type(of: self)) } }

Class1().printType() $ /Applications/Xcode-8.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift file.swift 0 swift 0x00000001060a2677 PrintStackTraceSignalHandler(void*) + 39 1 swift 0x00000001060a1b26 SignalHandler(int) + 646 2 libsystem_platform.dylib 0x00007fffcf6e6bba sigtramp + 26 3 libsystemplatform.dylib 0x0000000100000001 sigtramp + 814847073 4 libswiftCore.dylib 0x000000010b6ccb31 swiftgetTypeName + 401 5 libswiftCore.dylib 0x000000010b58939c TTSgq5PGVs10TeeStreamSSVs7_Stdout_GS_SSS0s16TextOutputStreamsTFFs20adHocPrint_unlockedu0_R_s16TextOutputStreamrFTxVs6MirrorRq_12isDebugPrintSb_T_L_13printTypeNameu0_R_S_rfPMP_T_ + 28 6 libswiftCore.dylib 0x000000010b65d26b TTSfq4ngs_n_n_TTSgq5PGVs10_TeeStreamSSVs7_Stdout_GS_SSS0s16TextOutputStreamsTFs20adHocPrint_unlockedu0_R_s16TextOutputStreamrFTxVs6MirrorRq_12isDebugPrintSb_T_ + 907 7 libswiftCore.dylib 0x000000010b521168 TTSgq5PGVs10TeeStreamSSVs7_Stdout_GS_SSS0s16TextOutputStreamsTFs15print_unlockedu0_R_s16TextOutputStreamrFTxRq__T_ + 1624 8 libswiftCore.dylib 0x000000010b668ed1 TTSfq4ggs_gs_n___TTSgq5GVs10_TeeStreamSSVs7_Stdout_GS_SSS0s16TextOutputStreamsTFs6printuRxs16TextOutputStreamrFTGSaP__9separatorSS10terminatorSS2toRx_T__merged + 353 9 libswiftCore.dylib 0x000000010b668cca TTSfq4ggs_gs_n___TTSgq5GVs10_TeeStreamSSVs7_Stdout_GS_SSS0s16TextOutputStreamsTFs6printuRxs16TextOutputStreamrFTGSaP__9separatorSS10terminatorSS2toRx_T_ + 42 10 libswiftCore.dylib 0x000000010b6692fd TTSfq4ggs_gs_TFs5printFTGSaP9separatorSS10terminatorSS_T__merged + 189 11 libswiftCore.dylib 0x000000010b668d2d TTSfq4ggs_gs_TFs5printFTGSaP9separatorSS10terminatorSS_T_ + 45 12 libswiftCore.dylib 0x000000010b591bad TFs5printFTGSaP9separatorSS10terminatorSST_ + 29 13 libswiftCore.dylib 0x0000000107106206 TFs5printFTGSaP9separatorSS10terminatorSST_ + 4223092342 14 libswiftCore.dylib 0x0000000107106046 TFs5printFTGSaP9separatorSS10terminatorSST_ + 4223091894 15 swift 0x00000001034db0ff llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef) + 655 16 swift 0x00000001034e0d43 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::1::vector<std::1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::1::allocator<std::1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > const&, char const const) + 707 17 swift 0x0000000102fefa49 swift::RunImmediately(swift::CompilerInstance&, std::1::vector<std::1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::1::allocator<std::1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 3385 18 swift 0x0000000102fc5c4c swift::performFrontend(llvm::ArrayRef, char const*, void, swift::FrontendObserver) + 50572 19 swift 0x0000000102f734bc main + 9052 20 libdyld.dylib 0x00007fffcf4d9255 start + 1 Stack dump: 0. Program arguments: /Applications/Xcode-8.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret file.swift -enable-objc-interop -sdk /Applications/Xcode-8.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name file [1] 31014 segmentation fault file.swift $ /Applications/Xcode-8.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc file.swift -sdk /Applications/Xcode-8.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk $ ./file Class1 `

Did we ever get a workaround or response on this issue?

By AnthonyMDev at Feb. 22, 2016, 7:58 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!