Can't use try? after custom infix operator in result builder

I made a result builder that turns key-value pairs into a dictionary. I'm getting a compiler exception when I build a test that uses that result builder in combination with a custom infix operator and a try? statement. Not an error message, just a long cryptic stack trace that is not my code. I'll add that at the end, so maybe someone can help me catch an error in my work from the syntax alone.

The Culprit

The result builder works on a closure if I remove this line:

"number" => try? Int(bytes: [0, 0, 0])

Context

The => Operator

This is an operator that turns a String and TopLevelCodableValue into a DocumentKeyValuePair.

infix operator => : AssignmentPrecedence

extension String {
    public static func => (key: String, value: TopLevelCodableValue) -> DocumentKeyValuePair {
        DocumentKeyValuePair(key: key, value: value)
    }
}

This works great in other tests for this result builder.

TopLevelCodableValue & init(bytes:)

This is a protocol used for a custom document format. Int conforms to this protocol, which means I can initialize it from its raw bytes. The initializer you saw in the problematic code snippet has the following signature:

init(bytes: [UInt8]) throws

This has been tested and works wonderfully.

The try? Keyword

I'm initializing with only three bytes, which won't work. That's why I've prepended the initializer with try?, because I want the document to be encoded with a nil value. Int? conforms to TopLevelCodableValue protocol and optional values were encoded successfully in other tests.

The Exception

This is what I'm returned:

Assertion failed: (!concreteBuffer && "concrete buffer already formed?!"), function getAddressForInPlaceInitialization, file SILGenConvert.cpp, line 558.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.      Program arguments: /Users/crichez/Library/Developer/Toolchains/swift-5.5-RELEASE.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/crichez/Documents/BSONKit/Tests/DocumentBuilderTests/DocumentBuilderTests.swift -emit-module-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests~partial.swiftmodule -emit-module-doc-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests~partial.swiftdoc -emit-module-source-info-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests~partial.swiftsourceinfo -emit-dependencies-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests.d -emit-reference-dependencies-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests.swiftdeps -target x86_64-apple-macosx11.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -I /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Users/crichez/Library/Developer/Toolchains/swift-5.5-RELEASE.xctoolchain/usr/bin/swift-driver -resource-dir /Users/crichez/Library/Developer/Toolchains/swift-5.5-RELEASE.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name DocumentBuilderTests -target-sdk-version 11.3.0 -parse-as-library -o /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/DocumentBuilderTests.build/DocumentBuilderTests.swift.o -index-store-path /Users/crichez/Documents/BSONKit/.build/x86_64-apple-macosx/debug/index/store -index-system-modules
1.      Apple Swift version 5.5 (swift-5.5-RELEASE)
2.
3.      While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/crichez/Documents/BSONKit/Tests/DocumentBuilderTests/DocumentBuilderTests.swift")
4.      While silgen emitFunction SIL function "@$s20DocumentBuilderTestsAAC15testCompositionyyKF".
 for 'testComposition()' (at /Users/crichez/Documents/BSONKit/Tests/DocumentBuilderTests/DocumentBuilderTests.swift:79:5)
5.      While silgen closureexpr SIL function "@$s20DocumentBuilderTestsAAC15testCompositionyyKFSay0aB00A12KeyValuePairVGycfU_".
 for expression at [/Users/crichez/Documents/BSONKit/Tests/DocumentBuilderTests/DocumentBuilderTests.swift:80:33 - line:89:9] RangeText="{
            "firstValue" => 12
            "number" => (try? Int(bytes: [0, 0, 0]))
            for i in 1 ... 5 {
                String(describing: i) => i * 2
                for i in 6 ... 10 {
                    String(describing: i) => i * 3
                }
            }
        "
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           0x0000000107145037 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000107144255 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000107145666 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007fff2041bd7d _sigtramp + 29
4  swift-frontend           0x0000000103ac16c6 llvm::Optional<swift::Type> llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>::callback_fn<substType(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::SubstOptions)::$_26>(long, swift::TypeBase*) + 1334
5  libsystem_c.dylib        0x00007fff2032b406 abort + 125
6  libsystem_c.dylib        0x00007fff2032a7d8 err + 0
7  swift-frontend           0x00000001073568d3 (anonymous namespace)::ExistentialInitialization::getAddressForInPlaceInitialization(swift::Lowering::SILGenFunction&, swift::SILLocation) (.cold.8) + 35
8  swift-frontend           0x0000000102ead31e (anonymous namespace)::ExistentialInitialization::getAddressForInPlaceInitialization(swift::Lowering::SILGenFunction&, swift::SILLocation) + 958
9  swift-frontend           0x0000000102ece546 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 28070
10 swift-frontend           0x0000000102ec77fe swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 94
11 swift-frontend           0x0000000102ebe667 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 55
12 swift-frontend           0x0000000102ed663c swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>::callback_fn<(anonymous namespace)::RValueEmitter::visitErasureExpr(swift::ErasureExpr*, swift::Lowering::SGFContext)::$_9>(long, swift::Lowering::SGFContext) + 76
13 swift-frontend           0x0000000102ead4db void llvm::function_ref<void (swift::SILValue)>::callback_fn<swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool)::$_6>(long, swift::SILValue) + 139
14 swift-frontend           0x0000000102e93c98 swift::Lowering::SILGenBuilder::bufferForExpr(swift::SILLocation, swift::SILType, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext, llvm::function_ref<void (swift::SILValue)>) + 184
15 swift-frontend           0x0000000102ea9471 swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool) + 1281
16 swift-frontend           0x0000000102ecca54 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 21172
17 swift-frontend           0x0000000102ebe667 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 55
18 swift-frontend           0x0000000102e7bf52 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3090
19 swift-frontend           0x0000000102e6957b (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 667
20 swift-frontend           0x0000000102e7aeef (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 143
21 swift-frontend           0x0000000102e8469e (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::Lowering::CalleeTypeInfo::ForeignInfo const&) && + 910
22 swift-frontend           0x0000000102e83e5d (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::CalleeTypeInfo::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1549
23 swift-frontend           0x0000000102e6d945 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2677
24 swift-frontend           0x0000000102e6b3a6 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2006
25 swift-frontend           0x0000000102ec7815 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 117
26 swift-frontend           0x0000000102ebe667 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 55
27 swift-frontend           0x0000000102e7bf52 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3090
28 swift-frontend           0x0000000102e6957b (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 667
29 swift-frontend           0x0000000102e7aeef (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 143
30 swift-frontend           0x0000000102e8469e (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::Lowering::CalleeTypeInfo::ForeignInfo const&) && + 910
31 swift-frontend           0x0000000102e83e5d (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::CalleeTypeInfo::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1549
32 swift-frontend           0x0000000102e6d945 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2677
33 swift-frontend           0x0000000102e6b3a6 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2006
34 swift-frontend           0x0000000102ec7815 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 117
35 swift-frontend           0x0000000102ebbf12 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 258
36 swift-frontend           0x0000000102eaf2a2 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 1842
37 swift-frontend           0x0000000102eaf4cd swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 45
38 swift-frontend           0x0000000102f29e37 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 455
39 swift-frontend           0x0000000102f29c65 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
40 swift-frontend           0x0000000102ede59a swift::Lowering::SILGenFunction::emitClosure(swift::AbstractClosureExpr*) + 410
41 swift-frontend           0x0000000102e59fc2 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 1746
42 swift-frontend           0x0000000102e5d464 swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*) + 116
43 swift-frontend           0x0000000102ed423c (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 124
44 swift-frontend           0x0000000102ec7851 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 177
45 swift-frontend           0x0000000102ebe667 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 55
46 swift-frontend           0x0000000102e7bafc (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1980
47 swift-frontend           0x0000000102e6957b (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 667
48 swift-frontend           0x0000000102e7aeef (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 143
49 swift-frontend           0x0000000102e8469e (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::Lowering::CalleeTypeInfo::ForeignInfo const&) && + 910
50 swift-frontend           0x0000000102e83e5d (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::CalleeTypeInfo::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1549
51 swift-frontend           0x0000000102e6d945 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2677
52 swift-frontend           0x0000000102e6b3a6 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2006
53 swift-frontend           0x0000000102ec7815 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 117
54 swift-frontend           0x0000000102ebbf12 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 258
55 swift-frontend           0x0000000102eaf2a2 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 1842
56 swift-frontend           0x0000000102eaf4cd swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 45
57 swift-frontend           0x0000000102f29e37 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 455
58 swift-frontend           0x0000000102f29c65 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
59 swift-frontend           0x0000000102edd7d9 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 457
60 swift-frontend           0x0000000102e5b3c9 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6873
61 swift-frontend           0x0000000102e5d1c0 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 400
62 swift-frontend           0x0000000102e598da swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 122
63 swift-frontend           0x0000000102f3b2a2 swift::ASTVisitor<(anonymous namespace)::SILGenType, void, void, void, void, void, void>::visit(swift::Decl*) + 434
64 swift-frontend           0x0000000102f383ec (anonymous namespace)::SILGenType::emitType() + 124
65 swift-frontend           0x0000000102f38369 swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 25
66 swift-frontend           0x0000000102e5f677 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1975
67 swift-frontend           0x0000000102f297b6 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&) + 134
68 swift-frontend           0x0000000102e6381d llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 413
69 swift-frontend           0x0000000102e60844 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 116
70 swift-frontend           0x000000010284ce12 performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 226
71 swift-frontend           0x0000000102841972 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4850
72 swift-frontend           0x0000000102794922 main + 546
73 libdyld.dylib            0x00007fff203f1f3d start + 1
The terminal process "swift 'build', '--build-tests', '--toolchain', '/Users/crichez/Library/Developer/Toolchains/swift-5.5-RELEASE.xctoolchain'" terminated with exit code: 1.

It says to submit a bug report, but I would love a sanity check first!

The compiler should never crash, even if code is malformed, so please do submit a bug report!

6 Likes
Terms of Service

Privacy Policy

Cookie Policy