Compiler Error on Swift for Linux 5.2.3 but works on Swift for macOS 10.15.5

This compiles on Swift/macOs but not Swift/Linux:

import Vapor

enum InternalError: Error {
    case noCurrentUnit

    var reason: String {
        switch self {
        case .noCurrentUnit:
            return "No..."
        }
    }
}

struct InternalErrorMiddleware: Middleware {
    func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
        let response: EventLoopFuture<Response>
        response = next.respond(to: request)
        return response.flatMapError { error in
            if let error = error as? InternalError {
                return request.eventLoop.makeSucceededFuture(request.redirect(to: "/events"))
            } else { return response }
        }
        .flatMap { response in
            return request.eventLoop.makeSucceededFuture(response)
        }
    }
}

The error is:

SIL verification failed: return locations are only allowed on branch and return instructions: InstKind == SILInstructionKind::BranchInst || InstKind == SILInstructionKind::ReturnInst || InstKind == SILInstructionKind::UnreachableInst
Verifying instruction:
%7 = copy_value %6 : $EventLoopFuture // users: %24, %28, %25
-> destroy_value %7 : $EventLoopFuture // id: %25
In function:
// closure #1 in InternalErrorMiddleware.respond(to:chainingTo:)
sil private [ossa] @s3App23InternalErrorMiddlewareV7respond2to10chainingTo3NIO15EventLoopFutureCy5Vapor8ResponseCGAJ7RequestC_AJ9Responder_ptFAMs0C0_pcfU_ : @convention(thin) (@guaranteed Error, @guaranteed Request, @guaranteed EventLoopFuture) -> @owned EventLoopFuture {
// %0 // users: %9, %3
// %1 // users: %43, %31, %4
// %2 // user: %6
bb0(%0 : @guaranteed $Error, %1 : @guaranteed $Request, %2 : @guaranteed $EventLoopFuture):
debug_value %0 : $Error, let, name "error", argno 1 // id: %3
debug_value %1 : $Request, let, name "request", argno 2 // id: %4
%5 = alloc_stack $EventLoopFuture, let, name "response", argno 3 // users: %64, %63, %8
%6 = copy_value %2 : $EventLoopFuture // users: %7, %8
%7 = copy_value %6 : EventLoopFuture<Response> // users: %24, %28, %25 store %6 to [init] %5 : *EventLoopFuture // id: %8
%9 = copy_value %0 : $Error // user: %11
%10 = alloc_stack Error // users: %30, %23, %13, %11 store %9 to [init] %10 : *Error // id: %11
%12 = alloc_stack InternalError // users: %19, %16, %14, %13 checked_cast_addr_br take_always Error in %10 : *Error to InternalError in %12 : $*InternalError, bb1, bb2 // id: %13

bb1: // Preds: bb0
%14 = load [trivial] %12 : $*InternalError // user: %15
%15 = enum $Optional, #Optional.some!enumelt.1, %14 : InternalError // user: %17 dealloc_stack %12 : *InternalError // id: %16
br bb3(%15 : $Optional) // id: %17

bb2: // Preds: bb0
%18 = enum Optional<InternalError>, #Optional.none!enumelt // user: %20 dealloc_stack %12 : *InternalError // id: %19
br bb3(%18 : $Optional) // id: %20

// %21 // user: %22
bb3(%21 : $Optional): // Preds: bb2 bb1
switch_enum %21 : $Optional, case #Optional.some!enumelt.1: bb5, case #Optional.none!enumelt: bb4 // id: %22

bb4: // Preds: bb3
dealloc_stack %10 : $*Error // id: %23
%24 = copy_value %7 : $EventLoopFuture // user: %26
destroy_value %7 : $EventLoopFuture // id: %25
br bb8(%24 : $EventLoopFuture) // id: %26

// %27 // user: %29
bb5(%27 : $InternalError): // Preds: bb3
destroy_value %7 : $EventLoopFuture // id: %28
debug_value %27 : InternalError, let, name "error" // id: %29 dealloc_stack %10 : *Error // id: %30
%31 = ref_element_addr %1 : Request, #Request.eventLoop // user: %32 %32 = load [copy] %31 : *EventLoop // user: %33
%33 = open_existential_ref %32 : EventLoop to @opened("388FF880-E4C2-11EA-A30C-B827EB39E918") EventLoop // users: %60, %57, %57
%34 = string_literal utf8 "/events" // user: %39
%35 = integer_literal $Builtin.Word, 7 // user: %39
%36 = integer_literal Builtin.Int1, -1 // user: %39 %37 = metatype @thin String.Type // user: %39
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%38 = function_ref @sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : @convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %39
%39 = apply %38(%34, %35, %36, %37) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // users: %44, %43
// function_ref default argument 1 of Request.redirect(to:type:)
%40 = function_ref @s5Vapor7RequestC8redirect2to4typeAA8ResponseCSS_AA12RedirectTypeOtFfA0_ : @convention(thin) () -> RedirectType // user: %41
%41 = apply %40() : $@convention(thin) () -> RedirectType // user: %43
// function_ref Request.redirect(to:type:)
%42 = function_ref @s5Vapor7RequestC8redirect2to4typeAA8ResponseCSS_AA12RedirectTypeOtF : @convention(method) (@guaranteed String, RedirectType, @guaranteed Request) -> @owned Response // user: %43
%43 = apply %42(%39, %41, %1) : $@convention(method) (@guaranteed String, RedirectType, @guaranteed Request) -> @owned Response // user: %46
destroy_value %39 : $String // id: %44
%45 = alloc_stack Response // users: %59, %58, %57, %46 store %43 to [init] %45 : *Response // id: %46
%47 = string_literal utf8 "/home/npr/bug/Sources/App/InternalErrorMiddleware.swift" // user: %49
%48 = integer_literal $Builtin.Word, 55 // user: %53
%49 = builtin "ptrtoint_Word"(%47 : $Builtin.RawPointer) : $Builtin.Word // user: %53
br bb6 // id: %50

bb6: // Preds: bb5
%51 = integer_literal $Builtin.Int8, 2 // user: %53
br bb7 // id: %52

bb7: // Preds: bb6
%53 = struct $StaticString (%49 : $Builtin.Word, %48 : $Builtin.Word, %51 : $Builtin.Int8) // user: %57
%54 = integer_literal $Builtin.Int64, 9 // user: %55
%55 = struct $UInt (%54 : $Builtin.Int64) // user: %57
// function_ref EventLoop.makeSucceededFuture(_:file:line:)
%56 = function_ref @s3NIO9EventLoopPAAE19makeSucceededFuture_4file4lineAA0bcF0Cyqd__Gqd___s12StaticStringVSutlF : @convention(method) <τ_0_0 where τ_0_0 : EventLoop><τ_1_0> (@in_guaranteed τ_1_0, StaticString, UInt, @guaranteed τ_0_0) -> @owned EventLoopFuture<τ_1_0> // user: %57
%57 = apply %56<@opened("388FF880-E4C2-11EA-A30C-B827EB39E918") EventLoop, Response>(%45, %53, %55, %33) : @convention(method) <τ_0_0 where τ_0_0 : EventLoop><τ_1_0> (@in_guaranteed τ_1_0, StaticString, UInt, @guaranteed τ_0_0) -> @owned EventLoopFuture<τ_1_0> // type-defs: %33; user: %61 destroy_addr %45 : *Response // id: %58
dealloc_stack %45 : *Response // id: %59 destroy_value %33 : @opened("388FF880-E4C2-11EA-A30C-B827EB39E918") EventLoop // id: %60
br bb8(%57 : $EventLoopFuture) // id: %61

// %62 // user: %65
bb8(%62 : @owned EventLoopFuture<Response>): // Preds: bb4 bb7 destroy_addr %5 : *EventLoopFuture // id: %63
dealloc_stack %5 : $*EventLoopFuture // id: %64
return %62 : $EventLoopFuture // id: %65
} // end sil function '$s3App23InternalErrorMiddlewareV7respond2to10chainingTo3NIO15EventLoopFutureCy5Vapor8ResponseCGAJ7RequestC_AJ9Responder_ptFAMs0C0_pcfU_'

Stack dump:
0. Program arguments: /usr/bin/swift -frontend -c /home/npr/bug/Sources/App/Enums.swift -primary-file /home/npr/bug/Sources/App/InternalErrorMiddleware.swift /home/npr/bug/Sources/App/configure.swift -emit-module-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware~partial.swiftmodule -emit-module-doc-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware~partial.swiftdoc -emit-module-source-info-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware~partial.swiftsourceinfo -emit-dependencies-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware.d -emit-reference-dependencies-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware.swiftdeps -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -I /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug -I /home/npr/bug/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSLShims/include -I /home/npr/bug/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSL/include -I /home/npr/bug/.build/checkouts/swift-backtrace/Sources/CBacktrace/include -I /home/npr/bug/.build/checkouts/swift-nio-extras/Sources/CNIOExtrasZlib/include -I /home/npr/bug/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSLShims/include -I /home/npr/bug/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSL/include -I /home/npr/bug/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/include -I /home/npr/bug/.build/checkouts/swift-nio/Sources/CNIOSHA1/include -I /home/npr/bug/.build/checkouts/swift-nio/Sources/CNIOAtomics/include -I /home/npr/bug/.build/checkouts/swift-nio/Sources/CNIODarwin/include -I /home/npr/bug/.build/checkouts/swift-nio/Sources/CNIOLinux/include -I /home/npr/bug/.build/checkouts/vapor/Sources/CURLParser/include -I /home/npr/bug/.build/checkouts/vapor/Sources/CMultipartParser/include -I /home/npr/bug/.build/checkouts/vapor/Sources/CBcrypt/include -I /home/npr/bug/.build/checkouts/vapor/Sources/CBase32/include -color-diagnostics -enable-testing -g -module-cache-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CCryptoBoringSSLShims.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CCryptoBoringSSL.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CBacktrace.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOExtrasZlib.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOBoringSSLShims.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOBoringSSL.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOHTTPParser.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOSHA1.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOAtomics.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIODarwin.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CNIOLinux.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CURLParser.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/CMultipartParser.build/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/checkouts/vapor/Sources/CBcrypt/include/module.modulemap -Xcc -fmodule-map-file=/home/npr/bug/.build/checkouts/vapor/Sources/CBase32/include/module.modulemap -parse-as-library -module-name App -o /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/App.build/InternalErrorMiddleware.swift.o -index-store-path /home/npr/bug/.build/aarch64-unknown-linux-gnu/debug/index/store -index-system-modules

  1. Swift version 5.2.3 (swift-5.2.3-RELEASE)
  2. While verifying SIL function "@$s3App23InternalErrorMiddlewareV7respond2to10chainingTo3NIO15EventLoopFutureCy5Vapor8ResponseCGAJ7RequestC_AJ9Responder_ptFAMs0C0_pcfU_".
    for expression at [/home/npr/bug/Sources/App/InternalErrorMiddleware.swift:6:38 - line:11:9] RangeText="{ error in
    if let error = error as? InternalError {
    // setStatus(error.reason, on: request)
    return request.eventLoop.makeSucceededFuture(request.redirect(to: "/events"))
    } else { return response }
    "
    /usr/bin/swift[0x4460524]

Could you please file a bug report on bugs.swift.org? Might also be worthwhile checking with a recent trunk development snapshot if the issue has been fixed.

Terms of Service

Privacy Policy

Cookie Policy