Skip to content

Instantly share code, notes, and snippets.

@omochi
Created May 20, 2023 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/fb0ea297ee16de7f61c92d47a1a44b08 to your computer and use it in GitHub Desktop.
Save omochi/fb0ea297ee16de7f61c92d47a1a44b08 to your computer and use it in GitHub Desktop.
protocol Middleware<Input, Output, NextInput, NextOutput> {
associatedtype Input
associatedtype Output
associatedtype NextInput
associatedtype NextOutput
}
protocol Routes<Input, Output> {
associatedtype Input
associatedtype Output
}
extension Routes {
func compose<M>(_ middleware: M) -> any Routes<M.NextInput, M.NextOutput> where
M: Middleware,
M.Input == Input,
M.Output == Output
{
fatalError()
}
func mapInput<NewInput>(
_ transform: @escaping (Input) -> NewInput
) -> any Routes<NewInput, Output> {
fatalError()
}
}
struct Request {}
struct Response {}
struct User {}
struct AuthMiddleware<Input, Output>: Middleware {
typealias NextInput = (Input, login: User)
typealias NextOutput = Output
}
func main(routes: any Routes<Request, Response>) {
let routes = routes.compose(AuthMiddleware())
.mapInput {
(request: $0.0, login: $0.login)
}
}
@omochi
Copy link
Author

omochi commented May 20, 2023

[omochi@omochi-iMacPro r]$ swift routes.swift
routes.swift:39:9: warning: initialization of immutable value 'routes' was never used; consider replacing with assignment to '_' or removing it
    let routes = routes.compose(AuthMiddleware())
    ~~~~^~~~~~
    _
Stack dump:
0.	Program arguments: /Applications/Xcode14.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret routes.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode14.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -color-diagnostics -new-driver-path /Applications/Xcode14.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode14.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name routes -disable-clang-spi -target-sdk-version 13.3 -target-sdk-name macosx13.3
1.	Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module routes)
4.	While silgen emitFunction SIL function "@$s6routes4mainAAyAA6Routes_pAA7RequestV5InputAaCPRts_AA8ResponseV6OutputAGRtsXP_tF".
 for 'main(routes:)' (at routes.swift:38:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000011073dc27 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000011073cad8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000011073e200 SignalHandler(int) + 272
3  libsystem_platform.dylib 0x00007ff80d3725ed _sigtramp + 29
4  libsystem_platform.dylib 0x00007f8ad205ea30 _sigtramp + 18446743604565034080
5  swift-frontend           0x000000010c6c7d3c swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 7036
6  swift-frontend           0x000000010c6c642d swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 621
7  swift-frontend           0x000000010c6c3b93 substType(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::SubstOptions) + 339
8  swift-frontend           0x000000010c6d1ac7 swift::Type llvm::function_ref<swift::Type (swift::Type)>::callback_fn<swift::GenericFunctionType::substGenericArgs(swift::SubstitutionMap, swift::SubstOptions)::$_15>(long, swift::Type) + 295
9  swift-frontend           0x000000010c6c2e40 swift::GenericFunctionType::substGenericArgs(llvm::function_ref<swift::Type (swift::Type)>) const + 128
10 swift-frontend           0x000000010b337f00 (anonymous namespace)::Callee::getSubstFormalInterfaceType(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SubstitutionMap) + 112
11 swift-frontend           0x000000010b334c3d (anonymous namespace)::SILGenApply::visitDeclRefExpr(swift::DeclRefExpr*) + 3805
12 swift-frontend           0x000000010b31d088 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 264
13 swift-frontend           0x000000010b3a38ba swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 746
14 swift-frontend           0x000000010b3bb206 void llvm::function_ref<void (swift::Expr*)>::callback_fn<swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_16>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_16)::'lambda'(swift::Expr*)>(long, swift::Expr*) + 38
15 swift-frontend           0x000000010b3a15f4 swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 788
16 swift-frontend           0x000000010b3ac56d (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext) + 349
17 swift-frontend           0x000000010b3a3897 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 711
18 swift-frontend           0x000000010b3936ac swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 124
19 swift-frontend           0x000000010b37b964 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int)::$_0::operator()(swift::Expr*, swift::VarDecl*, bool, std::__1::unique_ptr<swift::Lowering::Initialization, std::__1::default_delete<swift::Lowering::Initialization> >&) const + 532
20 swift-frontend           0x000000010b37af5c swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 1132
21 swift-frontend           0x000000010b30ce8d swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 125
22 swift-frontend           0x000000010b41ff77 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 3831
23 swift-frontend           0x000000010b3c45a1 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 833
24 swift-frontend           0x000000010b308a76 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 7350
25 swift-frontend           0x000000010b30a795 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef) + 181
26 swift-frontend           0x000000010b306daa swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 282
27 swift-frontend           0x000000010b30db60 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 3152
28 swift-frontend           0x000000010b41ed4e swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 190
29 swift-frontend           0x000000010b31237d llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 637
30 swift-frontend           0x000000010aceee7a performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 7338
31 swift-frontend           0x000000010aceba27 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5495
32 swift-frontend           0x000000010aca7358 swift::mainEntry(int, char const**) + 3912
33 dyld                     0x00007ff80cfeb41f start + 1903
Segmentation fault: 11

@omochi
Copy link
Author

omochi commented May 20, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment