Swift 5 Compiler Crashes on when attmepting to build specific file

Hey, I'm getting this error: Segmentation fault: 11 and I'm hoping to get some insight on to resolve it or if it's a compiler bug. The below code is from John Sundell's site, it worked fine in Swift 4, but it crashes when attempting to build this code using Swift 5. I've updated the Result type to use the one included in the new Standard Lib, which is the only change I've made it. My existing project returns the Segmentation fault: 11 error and complains about this line init(_ value: NewValue? = nil) in the Promise class.

To test, simply create a new project, create a new file and stick this the code below. The examples projects fail to compile, they just sit.

class Future<Value> {
  fileprivate var result: Result<Value, Error>? {
    didSet { result.map(report) }
  }
  
  private lazy var callbacks = [(Result<Value, Error>) -> Void]()
  
  
  func observe(with completion: @escaping (Result<Value, Error>) -> Void) {
    callbacks.append(completion)
    result.map(completion)
  }
  
  private func report(result: Result<Value, Error>) {
    callbacks.forEach({ $0(result) })
  }
  
  @discardableResult
  func transform<NextValue>(_ callback: @escaping (Value) throws -> NextValue) -> Future<NextValue> {
    return chain({ (value) -> Future<NextValue> in
      return try Promise(callback(value))
    })
  }
  
  @discardableResult
  func chain<NextValue>(_ closure: @escaping (Value) throws -> Future<NextValue>) -> Future<NextValue> {
    let promise = Promise<NextValue>()
    
    observe { (result) in
      switch result {
        
        
        
        
      case .success(let value):
        do {
          // Attempt to construct a new future given
          // the value from the first one
          let future = try closure(value)
          
          // Observe the "nested" future, and once it
          // completes, resolve/reject the "wrapper" future
          future.observe(with: { result in
            switch result {
            case .success(let value):
              promise.resolve(with: value)
            case .failure(let error):
              promise.reject(with: error)
            }
          })
        } catch {
          promise.reject(with: error)
        }
      case .failure(let error):
        promise.reject(with: error)
      }
    }
    return promise
  }
  
  
}

class Promise<NewValue>: Future<NewValue> {
  init(_ value: NewValue? = nil) {
    super.init()
    result = value.map(Result.success)
  }
  
  func resolve(with value: NewValue) {
    result = .success(value)
  }
  
  func reject(with error: Error) {
    result = .failure(error)
  }
}

Any idea what going on?

That's a compiler bug:

$ jw-swift-5.0 swiftc /tmp/test.swift
Assertion failed: (isa<ArchetypeType>(type)), function addProtocolConformance, file /Users/buildnode/jenkins/workspace/oss-swift-5.0-package-osx/swift/lib/IRGen/GenProto.cpp, line 2870.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-5.0-RELEASE.xctoolchain/usr/bin/swift -frontend -c -primary-file /tmp/test.swift -target x86_64-apple-darwin18.5.0 -enable-objc-interop -sdk /Volumes/Xcode11A314a_m19A441_i17A461_t17J456z_w17R461a_b17P436a_32bitSupport_FastSim_Boost_39GB/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name test -o /var/folders/lj/qfjqv9_x6b972r7l6f49lvmh0000gn/T/test-deeb51.o 
1.	While emitting IR SIL function "@$s4test7PromiseCyACyxGxSgcfc".
 for 'init(_:)' (at /tmp/test.swift:65:3)
0  swift                    0x000000010be16245 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010be154e5 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010be16832 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff65365b5d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000003e98 _sigtramp + 2596922200
5  libsystem_c.dylib        0x00007fff652256a6 abort + 127
6  libsystem_c.dylib        0x00007fff651ee20d basename_r + 0
7  swift                    0x000000010836bacb swift::irgen::NecessaryBindings::addProtocolConformance(swift::CanType, swift::ProtocolConformanceRef) + 235
8  swift                    0x0000000108373dfa void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<swift::irgen::NecessaryBindings::forFunctionInvocations(swift::irgen::IRGenModule&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap)::$_6>(long, swift::irgen::GenericRequirement) + 154
9  swift                    0x0000000108372151 void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<(anonymous namespace)::PolymorphicConvention::enumerateUnfulfilledRequirements(llvm::function_ref<void (swift::irgen::GenericRequirement)> const&)::$_12>(long, swift::irgen::GenericRequirement) + 209
10 swift                    0x0000000108364069 swift::irgen::enumerateGenericSignatureRequirements(swift::CanGenericSignature, llvm::function_ref<void (swift::irgen::GenericRequirement)> const&) + 281
11 swift                    0x000000010836c38e swift::irgen::NecessaryBindings::forFunctionInvocations(swift::irgen::IRGenModule&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap) + 238
12 swift                    0x0000000108321187 swift::irgen::emitFunctionPartialApplication(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::FunctionPointer const&, llvm::Value*, swift::irgen::Explosion&, llvm::ArrayRef<swift::SILParameterInfo>, swift::SubstitutionMap, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::Explosion&, bool) + 151
13 swift                    0x00000001083de4ee swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 48350
14 swift                    0x00000001083cee93 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8131
15 swift                    0x00000001082d5a4e swift::irgen::IRGenerator::emitGlobalTopLevel(bool) + 590
16 swift                    0x00000001083a8240 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1152
17 swift                    0x00000001083a8776 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::GlobalVariable**) + 86
18 swift                    0x0000000108259155 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13429
19 swift                    0x0000000108254c78 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3016
20 swift                    0x0000000108206d52 main + 690
21 libdyld.dylib            0x00007fff651803d5 start + 1
22 libdyld.dylib            0x000000000000000f start + 2598894651
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

@rollinggoron This seems to be already known as SR-10015.

Thanks!