Swift 5.2 Compiler crash while extending a protocol with generics

I was working on a solution using protocols, generics and inheritance. and I probably found a Swift Compiler Bug (Segmentation Fault 11 - stack trace at the bottom).

The code crashing the compiler is:

import Foundation

protocol ProtocolA {}

class DummySuper {}

protocol Loadable {
    associatedtype SomeType: ProtocolA

    func load()
}

class LoadingObject<T: ProtocolA>: DummySuper, Loadable {
    typealias SomeType = T
    
     // Uncomment this, no crash
     // func load() {}
}

extension Loadable where Self: DummySuper {
    // Crash because of this
    func load() {
    }
}

While extending the Loadable protocol, the compiler crashes with:

1.	Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
2.	While generating SIL witness table protocol conformance to 'Loadable' (at /Users/****/Temp/CodableTest/CodableTest/main.swift:15:1) for type 'LoadingObject<T>' (declared at [/Users/****/Temp/CodableTest/CodableTest/main.swift:21:1 - line:26:1] RangeText="class LoadingObject<T: ProtocolA>: DummySuper, Loadable {
typealias SomeType = T

 // Uncomment this, no crash
 // func load() {}
")
3.	While generating protocol witness thunk SIL function "@$s11CodableTest13LoadingObjectCyqd__GAA8LoadableA2aEP4loadyyFTW".
 for 'load()' (at /Users/***/Temp/CodableTest/CodableTest/main.swift:30:5)
0  swift                    0x000000010a8004ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000010a7ffcc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff71d9c5fd _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffee974fe50 _sigtramp + 2006661232
4  swift                    0x0000000106a0604d swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) + 173
5  swift                    0x0000000106a07d2d swift::Lowering::SILGenModule::emitLazyConformancesForFunction(swift::SILFunction*) + 1277
6  swift                    0x0000000106a5c6ce swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::IsSerialized_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 2702
7  swift                    0x0000000106a5bbc6 (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 902
8  swift                    0x0000000106a5b17d swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 1805
9  swift                    0x0000000106a5a726 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) + 550
10 swift                    0x0000000106a60308 (anonymous namespace)::SILGenType::emitType() + 6840
11 swift                    0x0000000106970d82 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 82
12 swift                    0x000000010696ff4c swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 1356
13 swift                    0x0000000106971fce swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 1438
14 swift                    0x0000000106551d51 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48065
15 swift                    0x00000001064c9b73 main + 1283
16 libdyld.dylib            0x00007fff71ba3cc9 start + 1
error: Segmentation fault: 11 (in target 'CodableTest' from project 'CodableTest')

Am I missing something?

Tested using Xcode 11.6 and Swift 5.2

1 Like

I can reproduce the crash on Xcode 12 beta 1, but not on master, so I think it's been fixed. Could you try using the latest trunk snapshot from Swift.org - Download Swift?

In the meantime, you can also stop the compiler from crashing by making LoadingObject a final class.

1 Like

Is there a reason you're still on beta 1? I believe this was fixed by GSB: Teach 'derived via concrete' computation about superclass constraints by slavapestov · Pull Request #32488 · apple/swift · GitHub, which should be in beta 3.

1 Like

Not really, just forgot to update it to newest beta :)

In the meantime, you can also stop the compiler from crashing by making LoadingObject a final class .

That would be a workaround, but in my case, I need the LoadingObject to be subclassable.